本文主要是介绍Mysql+javafx+sence bulider实现图书借阅系统,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
通过E-R图来了解系统
实机演示
登入界面
注册界面一
注册界面二(出现重复用户名)
注册界面三(未出现重复用户名)
用户登入后界面
图书信息查询
用户借阅查询
管理员账号登入
管理员登入后的界面
图书信息查询(管理员)
用户信息管理(所有用户的借阅记录和密码)
MVC架构
实现代码
登入界面代码
登入界面判断代码
用户(管理员)登入后界面代码
用户(管理员)图书查询(增删改)界面
用户还书界面代码
查询用户借阅信息界面(管理员)代码
JDBC
关于管理员和用户的操作
密码验证和注册验证以及管理员和用户登入验证的代码
实体类代码
管理员类
书类
用户类
fxml(javafx)
图书信息查询
借书信息查询(用户)
注册界面
登入界面
登入后选择界面
借书信息查询(管理员)
开发工具:idea2022, datagrip, javafx sence builder。
通过E-R图来了解系统
通过E-R图我们可以清晰的抓住借阅系统中实体与实体之间的关联,不难看出
管理员管理用户和图书,是一对多的关系。
用户借阅图书也是一对多的关系。
并且各个实体的属性我们也是一目了然。
实机演示
登入界面
注册界面一
注册界面二(出现重复用户名)
注册界面三(未出现重复用户名)
用户登入后界面
图书信息查询
用户借阅查询
管理员账号登入
管理员登入后的界面
图书信息查询(管理员)
用户信息管理(所有用户的借阅记录和密码)
点击行再点击按钮进行操作
MVC架构
经典MVC模式中,M是指业务模型,V是指用户界面,C则是控制器.
实体类包(模型)
界面包(javafx)
控制器包
jdbc包
实现代码
登入界面代码
package com.example.lib;import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;public class LibApplication extends Application {//舞台封装public static Stage stage;public void start(Stage stage) throws IOException {FXMLLoader fxmlLoader = new FXMLLoader(LibApplication.class.getResource("hello-view.fxml"));this.stage = stage;Scene scene = new Scene(fxmlLoader.load(), 600, 400);stage.setTitle("图书馆管理系统");stage.setScene(scene);stage.show();}public static void main(String[] args) throws Exception{launch();}
}
登入界面判断代码
package com.example.lib;import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import JDBC.*;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
import static com.example.lib.LibApplication.stage;public class Controller {@FXMLprivate Button exit;@FXMLprivate PasswordField password;@FXMLprivate Button enroll;@FXMLprivate Button login;@FXMLprivate TextField user;@FXMLpublic static String userid;public static int pd=0;@FXMLvoid userOnAction(ActionEvent event) {TextField user =new TextField();this.user=user;}@FXMLvoid passwordOnAction(ActionEvent event) {PasswordField password=new PasswordField();this.password=password;}public void start(Stage stage){URL url = getClass().getResource("login.fxml");Parent root=null;try {root = FXMLLoader.load(url);} catch (IOException e) {e.printStackTrace();}Scene scene = new Scene(root, 600, 400);stage.setTitle("图书馆管理系统");stage.setScene(scene);stage.show();}@FXMLvoid admOnAction(ActionEvent event) throws Exception {pd=1;}@FXMLpublic void loginOnAction(ActionEvent event) throws Exception {SqlUtil a=new SqlUtil();boolean pd=false;if(this.pd==0){pd=a.verify(user.getText(),password.getText());System.out.println("用户登入中");}else{pd=a.admverify(user.getText(),password.getText());System.out.println("管理员登入中");}SqlUtil su=new SqlUtil();userid=su.Getid(user.getText());
// System.out.println(userid);if (pd){start(stage);System.out.println("登入成功");}else {System.out.println("登入失败");}}@FXMLvoid enrollOnAction(ActionEvent event) {URL url = getClass().getResource("enroll.fxml");Parent root=null;try {root = FXMLLoader.load(url);} catch (IOException e) {e.printStackTrace();}Scene scene = new Scene(root, 600, 400);stage.setTitle("图书馆管理系统");stage.setScene(scene);stage.show();}@FXMLvoid exitOnAction(ActionEvent event) {stage.close();}}
用户(管理员)登入后界面代码
package com.example.lib;import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
import java.io.IOException;
import static com.example.lib.Controller.pd;
import static com.example.lib.LibApplication.stage;public class LoginCon {@FXMLprivate Button Inquire;@FXMLprivate Button userRead;@FXMLprivate Button exitbutton;@FXMLprivate Button userin;@FXML//普通用户和管理员界面区别public void initialize(){if (pd==0){userin.setVisible(false);}else{userRead.setVisible(false);}}//封装图书查询界面void Start(Stage stage) throws IOException {FXMLLoader fxmlLoader = new FXMLLoader(LibApplication.class.getResource("Bookinquiries.fxml"));Scene scene = new Scene(fxmlLoader.load(), 600, 400);stage.setTitle("图书馆管理系统");stage.setScene(scene);stage.show();}@FXMLvoid InquireOnAction(ActionEvent event) throws Exception {Start(stage);}//封装图书归还界面void Start1(Stage stage) throws IOException {FXMLLoader fxmlLoader = new FXMLLoader(LibApplication.class.getResource("Borrow-Inquiries.fxml"));Scene scene = new Scene(fxmlLoader.load(), 600, 400);stage.setTitle("图书馆管理系统");stage.setScene(scene);stage.show();}@FXMLvoid userReadOnAction(ActionEvent event) throws IOException {Start1(stage);}@FXMLvoid exitbuttonOnAction(ActionEvent event) throws IOException {pd=0;LibApplication lib = new LibApplication();lib.start(stage);}//封装用户信息查询界面void Start2(Stage stage) throws IOException {FXMLLoader fxmlLoader = new FXMLLoader(LibApplication.class.getResource("userin.fxml"));Scene scene = new Scene(fxmlLoader.load(), 600, 400);stage.setTitle("图书馆管理系统");stage.setScene(scene);stage.show();}@FXMLvoid userinOnAction(ActionEvent event) throws IOException {Start2(stage);}
}
用户(管理员)图书查询(增删改)界面
package com.example.lib;import JDBC.Bookinquiries;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import role.Book;import java.io.IOException;import static com.example.lib.Controller.pd;
import static com.example.lib.LibApplication.stage;public class BookCon {@FXMLprivate Button clear;@FXMLprivate Button add;@FXMLprivate TextField textAuthor;@FXMLprivate TextField textquiries;@FXMLprivate Button update;@FXMLprivate Button delete;@FXMLprivate Button borrow;@FXMLprivate TextField textPrice;@FXMLprivate TextField textId;@FXMLprivate TextField textName;@FXMLprivate TextField textState;@FXMLprivate TableColumn<Book, String> Price;@FXMLprivate TableColumn<Book, String> author;@FXMLprivate TableColumn<Book, String> State;@FXMLprivate TableView<Book> booktable;@FXMLprivate TableColumn<Book, String> bookname;@FXMLprivate Button quiries;@FXMLprivate Button exit1;@FXMLprivate TableColumn<Book, String> bookid;@FXMLpublic static Book book;@FXMLprivate TextField textquiries2;@FXMLprivate void initialize() throws Exception {//管理员和用户的部分功能隐藏if (pd == 0) {textId.setVisible(false);textAuthor.setVisible(false);textName.setVisible(false);textState.setVisible(false);textPrice.setVisible(false);add.setVisible(false);delete.setVisible(false);update.setVisible(false);clear.setVisible(false);} else {borrow.setVisible(false);}Bookinquiries s = new Bookinquiries();//获取图书信息//cellvalueFactory设置单元格(定义列,然后通过book集合填入信息)bookid.setCellValueFactory(cellData -> cellData.getValue().getId());bookname.setCellValueFactory(cellData -> cellData.getValue().getName());author.setCellValueFactory(cellData -> cellData.getValue().getAuthor());Price.setCellValueFactory(cellData -> cellData.getValue().getPrice());State.setCellValueFactory(cellData -> cellData.getValue().getState());booktable.getItems().addAll(s.quiries());System.out.println("%%%%%%%%%%%%%%%");//鼠标点击行事件booktable.setRowFactory(a -> {TableRow<Book> row = new TableRow<Book>();row.setOnMouseClicked(event -> {if (event.getClickCount() == 1 && (!row.isEmpty())) {book = row.getItem();System.out.println(book);//获取book对象的属性(以book.getName().getValue().toString()为例)textName.setText(book.getName().getValue().toString());textId.setText(book.getId().getValue().toString());textAuthor.setText(book.getAuthor().getValue().toString());textPrice.setText(book.getPrice().getValue().toString());textState.setText(book.getState().getValue().toString());}});return row;});}//借阅@FXMLvoid borrowOnAction(ActionEvent event) throws Exception {Bookinquiries bq = new Bookinquiries();Controller c = new Controller();System.out.println(c.userid);bq.borrowbook(c.userid);quiriesOnAction(event);bq.borrowbook1(c.userid);}//查询@FXMLvoid quiriesOnAction(ActionEvent event) throws Exception {Bookinquiries s = new Bookinquiries();booktable.getItems().clear();booktable.getItems().addAll(s.quiries(textquiries.getText(),textquiries2.getText()));System.out.println("&&&&&&&&&&&&&&&&&&&");}//增加@FXMLvoid addOnAction(ActionEvent event) throws Exception {Bookinquiries a = new Bookinquiries();a.addBook(textName.getText(), textAuthor.getText(), textPrice.getText(), textState.getText());quiriesOnAction(event);}//更新@FXMLvoid updateOnAction(ActionEvent event) throws Exception {Bookinquiries a = new Bookinquiries();a.updateBook(textName.getText(), textId.getId(), textAuthor.getText(), textPrice.getText(), textState.getText());quiriesOnAction(event);}//删除@FXMLvoid deleteOnAction(ActionEvent event) throws Exception {Bookinquiries a = new Bookinquiries();a.deleteBook(textName.getText());quiriesOnAction(event);}//退出@FXMLvoid exit1OnAction(ActionEvent actionEvent) throws IOException {Controller cn = new Controller();cn.start(stage);}//清除@FXMLvoid clearbuttonOnAction(ActionEvent event) throws Exception {textName.setText("");textId.setText("");textAuthor.setText("");textPrice.setText("");textState.setText("");quiriesOnAction(event);}@FXMLvoid booktableOnSort(ActionEvent event) {}@FXMLvoid booknameOnEdit(ActionEvent event) {}@FXMLvoid bookidOnEdit(ActionEvent event) {}@FXMLvoid authorOnEdit(ActionEvent event) {}@FXMLvoid PriceOnEdit(ActionEvent event) {}@FXMLvoid StateOnEdit(ActionEvent event) {}@FXMLvoid textNameOnAction(ActionEvent event) {}@FXMLvoid textIdOnAction(ActionEvent event) {}@FXMLvoid textAuthorOnAction(ActionEvent event) {}@FXMLvoid textPriceOnAction(ActionEvent event) {}@FXMLvoid textStateOnAction(ActionEvent event) {}@FXMLvoid textquiriesOnAction(ActionEvent actionEvent) {}}
用户还书界面代码
package com.example.lib;import JDBC.Bookinquiries;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import role.Book;
import java.io.IOException;import static com.example.lib.LibApplication.stage;public class BorrowInquiries {@FXMLprivate TableColumn<Book, String> returndate;@FXMLprivate Button ruturnbutton;@FXMLprivate TableColumn<Book, String> borrowdate;@FXMLprivate TableColumn<Book, String> bookname;@FXMLprivate TableView<Book> booktable;@FXMLprivate Button inquiriesbutton;@FXMLprivate TableColumn<Book, String> bookid;@FXMLprivate TextField get;@FXMLprivate Button exitbutton;@FXMLpublic static Book book1;@FXMLprivate void initialize() throws Exception {Bookinquiries s = new Bookinquiries();Controller cn=new Controller();//获取图书信息bookid.setCellValueFactory(cellData -> cellData.getValue().getId());bookname.setCellValueFactory(cellData -> cellData.getValue().getName());borrowdate.setCellValueFactory(cellData -> cellData.getValue().getBorrowingData());returndate.setCellValueFactory(cellData -> cellData.getValue().getReturnData());booktable.getItems().addAll(s.quiries1(cn.userid));System.out.println("%%%%%%%%%%%%%%%");booktable.setRowFactory( a -> {TableRow<Book> row = new TableRow<Book>();row.setOnMouseClicked(event -> {if (event.getClickCount() == 1 && (! row.isEmpty()) ) {book1=row.getItem();System.out.println(book1);}});return row;});}//查询@FXMLvoid inquiriesbuttonOnAction(ActionEvent event) throws Exception {Controller cn=new Controller();Bookinquiries s = new Bookinquiries();booktable.getItems().clear();booktable.getItems().addAll(s.quiries2(cn.userid,get.getText()));System.out.println("&&&&&&&&&&&&&&&&&&&");}//还书@FXMLvoid ruturnbuttonOnAction(ActionEvent event) throws Exception {Controller cn=new Controller();Bookinquiries b=new Bookinquiries();b.returnbook(cn.userid);b.returnbook1();inquiriesbuttonOnAction(event);}//退出@FXMLvoid exitbuttonOnAction(ActionEvent event) throws Exception {Controller cn=new Controller();cn.start(stage);}}
查询用户借阅信息界面(管理员)代码
package com.example.lib;import JDBC.userinquiries;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import role.User;
import static com.example.lib.LibApplication.stage;public class userinCon {@FXMLprivate Button pwbutton;@FXMLprivate TableColumn<User, String> returndate;@FXMLprivate TableColumn<User, String> pw;@FXMLprivate TextField get;@FXMLprivate Button deletebutton;@FXMLprivate TableColumn<User, String> borrowdate;@FXMLprivate TableColumn<User, String> un;@FXMLprivate TableColumn<User, String> bookname;@FXMLprivate TableView<User> booktable;@FXMLprivate Button inquiriesbutton;@FXMLprivate Button exitbutton;@FXMLprivate TextField get1;@FXMLpublic static User user;@FXMLpublic void initialize() throws Exception {userinquiries s = new userinquiries();//获取图书信息un.setCellValueFactory(cellData -> cellData.getValue().getName());pw.setCellValueFactory(cellData -> cellData.getValue().getPassword());bookname.setCellValueFactory(cellData -> cellData.getValue().getBookname());borrowdate.setCellValueFactory(cellData -> cellData.getValue().getBorrowingData());returndate.setCellValueFactory(cellData -> cellData.getValue().getReturnData());booktable.getItems().addAll(s.quiries());System.out.println("%%%%%%%%%%%%%%%");//鼠标点击行事件booktable.setRowFactory(a -> {TableRow<User> row = new TableRow<User>();row.setOnMouseClicked(event -> {if (event.getClickCount() == 1 && (!row.isEmpty())) {user = row.getItem();System.out.println(user);}});return row;});}@FXMLvoid inquiriesbuttonOnAction(ActionEvent event) throws Exception {userinquiries s=new userinquiries();booktable.getItems().clear();System.out.println("-----"+get.getText()+"--------");booktable.getItems().addAll(s.quiries(get.getText()));}@FXMLvoid deleteOnAction(ActionEvent event) throws Exception {userinquiries s=new userinquiries();s.delete();inquiriesbuttonOnAction(event);}@FXMLvoid exitbuttonOnAction(ActionEvent event) {Controller cn=new Controller();cn.start(stage);}@FXMLvoid pwbuttonOnAction(ActionEvent event) throws Exception {userinquiries s =new userinquiries();System.out.println(get1.getText());s.update(get1.getText());inquiriesbuttonOnAction(event);}}
JDBC
关于管理员和用户的操作
package JDBC;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import role.Book;
import java.sql.*;import static com.example.lib.BookCon.book;
import static com.example.lib.BorrowInquiries.book1;public class Bookinquiries {private static String url = "jdbc:mysql:///lib";private static String username = "root";//填数据库密码private static String password = "********";private static String bookid=null;private static String bookstate=null;private static String name=null;private static String id=null;private static String price=null;private static String state=null;private static String author=null;private static String user_id=null;public Bookinquiries() throws Exception {}public Connection getConn() throws Exception {Class.forName("com.mysql.jdbc.Driver");Connection conn = DriverManager.getConnection(url, username, password);return conn;}//搜索表数据,封装表数据public ObservableList quiries() throws Exception {ObservableList<Book> data = FXCollections.observableArrayList();String sql = "SELECT * from book";PreparedStatement ps = getConn().prepareStatement(sql);ResultSet rs = ps.executeQuery();int pd = 0;while (rs.next()) {data.add(new Book(rs.getString("id"),rs.getString("name"),rs.getString("author"),rs.getString("price"),rs.getString("state")));System.out.println("--------");System.out.println("搜索成功");pd = 1;}if (pd == 0) {System.out.println("搜索失败");}ps.close();return data;}//搜索表数据,封装表数据public ObservableList quiries1(String user_id) throws Exception {ObservableList<Book> data = FXCollections.observableArrayList();String sql = "select book.name,book.id,record.borrowdate,record.returndate from user,book,record where userid=user.id and bookid=book.id and user_id = ?";PreparedStatement ps = getConn().prepareStatement(sql);ps.setString(1,user_id);ResultSet rs = ps.executeQuery();int pd = 0;while (rs.next()) {data.add(new Book(rs.getString("name"),rs.getString("id"),rs.getString("borrowdate"),rs.getString("returndate")));System.out.println("--------");System.out.println("搜索成功");pd = 1;}if (pd == 0) {System.out.println("搜索失败");}ps.close();return data;}//用户借阅查询public ObservableList quiries2(String user_id,String name) throws Exception {ObservableList<Book> data = FXCollections.observableArrayList();String sql = "select book.name,book.id,record.borrowdate,record.returndate from user,book,record where userid=user.id and bookid=book.id and user_id = ? and book.name like ?";PreparedStatement ps = getConn().prepareStatement(sql);ps.setString(1,user_id);ps.setString(2, "%"+name+"%");ResultSet rs = ps.executeQuery();int pd = 0;while (rs.next()) {data.add(new Book(rs.getString("name"),rs.getString("id"),rs.getString("borrowdate"),rs.getString("returndate")));
// bookid=rs.getString("id");
// bookstate=rs.getString("state");
// this.user_id =rs.getString("user.id");System.out.println("--------");System.out.println("搜索成功");pd = 1;}if (pd == 0) {System.out.println("搜索失败");}ps.close();return data;}//sqlpublic ObservableList quiries(String name,String author) throws Exception {ObservableList<Book>data= FXCollections.observableArrayList();String sql;PreparedStatement ps;if (name==""){sql = "SELECT * from book where author like ?";ps = getConn().prepareStatement(sql);ps.setString(1,"%"+author+"%");} else if (author=="") {sql = "SELECT * from book where name like ?";ps = getConn().prepareStatement(sql);ps.setString(1,"%"+name+"%");}else {sql = "SELECT * from book where name like ? and author like ?";ps = getConn().prepareStatement(sql);ps.setString(1,"%"+name+"%");ps.setString(2,"%"+author+"%");}ResultSet rs = ps.executeQuery();int pd = 0;int count=0;while (rs.next()) {data.add(new Book(rs.getString("id"),rs.getString("name"),rs.getString("author"),rs.getString("price"),rs.getString("state")));System.out.println("搜索成功了");this.id=rs.getString("id");this.name=rs.getString("name");this.author=rs.getString("author");this.price=rs.getString("price");this.state=rs.getString("state");this.user_id=rs.getString("user_id");pd = 1;}if (pd == 0) {System.out.println("搜索失败了");}ps.close();return data;}public void updateBook(String name,String id,String author,String price,String state) throws Exception {
// if (name==""){
// name=this.name;
// }
// if (id==""){
// id=this.id;
// }
// if (author==""){
// author=this.author;
// }
// if (price==""){
// price=this.price;
// }
// if (state==""){
// state=this.state;
// }String sql="UPDATE book set name=REPLACE(name, ?, ?)where id=?";PreparedStatement ps = getConn().prepareStatement(sql);ps.setString(1,book.getName().getValue().toString());ps.setString(2,name);ps.setString(3,book.getId().getValue().toString());ps.executeUpdate();// String sql1="UPDATE book set id=REPLACE(id, ?, ?)where id=?";
// PreparedStatement ps1 = getConn().prepareStatement(sql1);
// ps1.setString(1,book.getId().getValue().toString());
// ps1.setString(2,id);
// ps1.setString(3,book.getId().getValue().toString());
// ps1.executeUpdate();String sql2="UPDATE book set author=REPLACE(author, ?, ?)where id=?";PreparedStatement ps2 = getConn().prepareStatement(sql2);ps2.setString(1,book.getAuthor().getValue().toString());ps2.setString(2,author);ps2.setString(3,book.getId().getValue().toString());ps2.executeUpdate();String sql3="UPDATE book set price=REPLACE(price, ?, ?)where id=?";PreparedStatement ps3 = getConn().prepareStatement(sql3);ps3.setString(1,book.getPrice().getValue().toString());ps3.setString(2,price);ps3.setString(3,book.getId().getValue().toString());System.out.println(this.price+" "+price+" "+this.id+" "+ps3.executeUpdate());ps3.executeUpdate();String sql4="UPDATE book set state=REPLACE(state, ?, ?)where id=?";PreparedStatement ps4 = getConn().prepareStatement(sql4);ps4.setString(1,book.getState().getValue().toString());ps4.setString(2,state);ps4.setString(3,book.getId().getValue().toString());ps4.executeUpdate();
// System.out.println(this.state+" "+state+" "+this.id+" "+ps4.executeUpdate());}public void addBook(String name, String author, String price, String state) throws Exception {String sql="INSERT INTO book (name,author,price,state )\n" +" VALUES\n" +" (?,?,?,?)";PreparedStatement ps=getConn().prepareStatement(sql);ps.setString(1,name);ps.setString(2,author);ps.setString(3,price);ps.setString(4,state);ps.executeUpdate();}public void deleteBook(String name) throws Exception {String sql="DELETE from book where name = ?";PreparedStatement ps=getConn().prepareStatement(sql);ps.setString(1,name);ps.executeUpdate();}public void borrowbook(String id)throws Exception{String sql="UPDATE book set state=REPLACE(state, ?, ?)where id=?";String sql1="UPDATE book set user_id=?where id=?";PreparedStatement ps = getConn().prepareStatement(sql);System.out.println(this.state+" "+state+"+++++++++++++++");PreparedStatement ps1 = getConn().prepareStatement(sql1);if (book.getState().getValue().toString().equals("0")){ps.setString(1,book.getState().getValue().toString());System.out.println(id+""+this.id+"=========");ps.setString(2,"1");ps.setString(3,book.getId().getValue().toString());System.out.println(book.getId().getValue().toString()+"===========");ps.executeUpdate();ps1.setString(1,id);ps1.setString(2,book.getId().getValue().toString());ps1.executeUpdate();System.out.println("借阅成功");}else{System.out.println("书籍已被借阅");}}public void borrowbook1(String id)throws Exception{String sql="INSERT INTO record (userid,borrowdate,returndate,bookid)\n" +" VALUES\n" +" (?,?,?,?)";PreparedStatement ps=getConn().prepareStatement(sql);Date date = new Date();SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");ps.setString(1,id);ps.setString(2,ft.format(date));ps.setString(3,ft.format(new Date(date.getTime()+3 * 24 * 60 * 60 * 1000)));ps.setString(4,book.getId().getValue().toString());ps.executeUpdate();}public void returnbook(String id)throws Exception{String sql="UPDATE book set state=REPLACE(state,?, ?)where id=?";String sql1="UPDATE book set user_id=REPLACE(user_id,?, ?)where id=?";PreparedStatement ps = getConn().prepareStatement(sql);ps.setString(1,"1");ps.setString(2,"0");ps.setString(3,book1.getId().getValue().toString());ps.executeUpdate();PreparedStatement ps1 = getConn().prepareStatement(sql1);ps1.setString(1,user_id);ps1.setString(2,null);ps1.setString(3,book1.getId().getValue().toString());ps1.executeUpdate();System.out.println("归还成功");}public void returnbook1()throws Exception{String sql="DELETE from record where bookid = ? ";PreparedStatement ps = getConn().prepareStatement(sql);System.out.println();ps.setString(1,book1.getId().getValue().toString());ps.executeUpdate();System.out.println("归还成功");}}
密码验证和注册验证以及管理员和用户登入验证的代码
package JDBC;
import java.sql.*;public class SqlUtil {private static String url = "jdbc:mysql:///lib";private static String username = "root";//填数据库密码private static String password = "********";public Connection getConn() throws Exception {Class.forName("com.mysql.jdbc.Driver");Connection conn = DriverManager.getConnection(url, username, password);return conn;}public boolean verify(String a,String b) throws Exception{//执行SQL语句,并接收结果String sql = "select * from user where name= ? and password = ?; ";PreparedStatement ps = getConn().prepareStatement(sql);ps.setString(1,a);ps.setString(2,b);ResultSet rs = ps.executeQuery();boolean pd;if(rs.next()){pd = true;}else{pd = false;}ps.close();return pd;}public boolean admverify(String a,String b) throws Exception{//执行SQL语句,并接收结果String sql = "select * from adm where name= ? and password = ?; ";PreparedStatement ps = getConn().prepareStatement(sql);ps.setString(1,a);ps.setString(2,b);ResultSet rs = ps.executeQuery();boolean pd;if(rs.next()){pd = true;}else{pd = false;}ps.close();return pd;}public String Getid(String a) throws Exception{//执行SQL语句,并接收结果String sql = "select id from user where name= ?; ";PreparedStatement ps = getConn().prepareStatement(sql);ps.setString(1,a);ResultSet rs = ps.executeQuery();String id = null;while(rs.next()){id= rs.getString("id");System.out.println(id);}
// System.out.println(id);return id;}public void enroll(String un,String pw) throws Exception {String sql="INSERT INTO user ( name, password) VALUES (?,?)";PreparedStatement ps =getConn().prepareStatement(sql);ps.setString(1,un);ps.setString(2,pw);ps.executeUpdate();System.out.println("注册成功");}public boolean enroll1(String un) throws Exception {boolean pd =true;String sql = "select * from user where name = ?";PreparedStatement ps = getConn().prepareStatement(sql);ps.setString(1, un);ResultSet rs = ps.executeQuery();while (rs.next()){pd=false;System.out.println("用户名重复,请更改!");}return pd;}}
实体类代码
管理员类
package role;public class Adm {private Integer id;private String name;private Integer password;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getPassword() {return password;}public void setPassword(Integer password) {this.password = password;}@Overridepublic String toString() {return "Adm{" +"id=" + id +", name='" + name + '\'' +", password=" + password +'}';}}
书类
package role;import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ObservableValue;import java.util.Set;public class Book {private ObservableValue id;private ObservableValue name;private ObservableValue author;private ObservableValue price;private ObservableValue state;private ObservableValue borrowingData;private ObservableValue returnData;@Overridepublic String toString() {return "Book{" +"id=" + id +", name=" + name +", author=" + author +", price=" + price +", state=" + state +", borrowingData=" + borrowingData +", returnData=" + returnData +'}';}public ObservableValue getBorrowingData() {return borrowingData;}public void setBorrowingData(ObservableValue borrowingData) {this.borrowingData = borrowingData;}public ObservableValue getReturnData() {return returnData;}public void setReturnData(ObservableValue returnData) {this.returnData = returnData;}public Book(){}public Book(String name,String id,String borrowingData,String returnData){this.name=new SimpleStringProperty(name);this.id=new SimpleStringProperty(id);this.borrowingData=new SimpleStringProperty(borrowingData);this.returnData=new SimpleStringProperty(returnData);}public Book(String id,String name,String author,String price,String state){this.id=new SimpleStringProperty(id);this.name=new SimpleStringProperty(name);this.author=new SimpleStringProperty(author);this.price=new SimpleStringProperty(price);this.state=new SimpleStringProperty(state);}public ObservableValue getId() {return id;}public void setId(ObservableValue id) {this.id = id;}public ObservableValue getName() {return name;}public void setName(ObservableValue name) {this.name = name;}public ObservableValue getAuthor() {return author;}public void setAuthor(ObservableValue author) {this.author = author;}public ObservableValue getPrice() {return price;}public void setPrice(ObservableValue price) {this.price = price;}public ObservableValue getState() {return state;}public void setState(ObservableValue state) {this.state = state;}}
用户类
package role;import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;public class User {private ObservableValue id;private ObservableValue name;private ObservableValue borrowingData;private ObservableValue returnData;private ObservableValue password;private ObservableValue bookname;public User (String name,String password,String bookname,String borrowingData,String returnData){this.name=new SimpleStringProperty(name);this.bookname=new SimpleStringProperty(bookname);this.password=new SimpleStringProperty(password);this.borrowingData=new SimpleStringProperty(borrowingData);this.returnData=new SimpleStringProperty(returnData);}public ObservableValue getBookname() {return bookname;}public void setBookname(ObservableValue bookname) {this.bookname = bookname;}public ObservableValue getId() {return id;}public void setId(ObservableValue id) {this.id = id;}public ObservableValue getName() {return name;}public void setName(ObservableValue name) {this.name = name;}public ObservableValue getBorrowingData() {return borrowingData;}public void setBorrowingData(ObservableValue borrowingData) {this.borrowingData = borrowingData;}public ObservableValue getReturnData() {return returnData;}public void setReturnData(ObservableValue returnData) {this.returnData = returnData;}public ObservableValue getPassword() {return password;}public void setPassword(ObservableValue password) {this.password = password;}@Overridepublic String toString() {return "User{" +"id=" + id +", name='" + name + '\'' +", borrowingData=" + borrowingData +", returnData=" + returnData +", password=" + password +'}';}}
fxml(javafx)
图书信息查询
<?xml version="1.0" encoding="UTF-8"?><?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?><AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.lib.BookCon"><children><TableView fx:id="booktable" layoutY="61.0" onSort="#booktableOnSort" prefHeight="218.0" prefWidth="600.0"><columns><TableColumn fx:id="bookname" onEditStart="#booknameOnEdit" prefWidth="120.0" text="书名" /><TableColumn fx:id="bookid" onEditStart="#bookidOnEdit" prefWidth="120.0" text="编号" /><TableColumn fx:id="author" onEditStart="#authorOnEdit" prefWidth="120.0" text="作者" /><TableColumn fx:id="Price" onEditStart="#PriceOnEdit" prefWidth="120.0" text="价格" /><TableColumn fx:id="State" onEditStart="#StateOnEdit" prefWidth="120.0" text="状态" /></columns></TableView><Button fx:id="quiries" layoutX="276.0" layoutY="20.0" mnemonicParsing="false" onAction="#quiriesOnAction" prefHeight="30.0" prefWidth="93.0" text="查询" /><TextField fx:id="textquiries" layoutX="14.0" layoutY="20.0" onAction="#textquiriesOnAction" prefHeight="30.0" prefWidth="122.0" promptText="书名" /><Button fx:id="borrow" layoutX="387.0" layoutY="20.0" mnemonicParsing="false" onAction="#borrowOnAction" prefHeight="30.0" prefWidth="93.0" text="借阅" /><Button fx:id="exit1" layoutX="509.0" layoutY="20.0" mnemonicParsing="false" onAction="#exit1OnAction" prefHeight="30.0" prefWidth="77.0" text="退出" /><TextField fx:id="textName" layoutX="9.0" layoutY="294.0" onAction="#textNameOnAction" prefHeight="30.0" prefWidth="107.0" promptText="书名" /><Button fx:id="add" layoutX="24.0" layoutY="341.0" mnemonicParsing="false" onAction="#addOnAction" prefHeight="30.0" prefWidth="77.0" text="增加" /><TextField fx:id="textId" layoutX="131.0" layoutY="294.0" onAction="#textIdOnAction" prefHeight="30.0" prefWidth="107.0" promptText="编号" /><TextField fx:id="textAuthor" layoutX="251.0" layoutY="294.0" onAction="#textAuthorOnAction" prefHeight="30.0" prefWidth="107.0" promptText="作者" /><Button fx:id="update" layoutX="146.0" layoutY="341.0" mnemonicParsing="false" onAction="#updateOnAction" prefHeight="30.0" prefWidth="77.0" text="修改" /><Button fx:id="delete" layoutX="266.0" layoutY="341.0" mnemonicParsing="false" onAction="#deleteOnAction" prefHeight="30.0" prefWidth="77.0" text="删除" /><TextField fx:id="textPrice" layoutX="369.0" layoutY="294.0" onAction="#textPriceOnAction" prefHeight="30.0" prefWidth="107.0" promptText="价格" /><TextField fx:id="textState" layoutX="485.0" layoutY="294.0" onAction="#textStateOnAction" prefHeight="30.0" prefWidth="107.0" promptText="状态" /><Button fx:id="clear" layoutX="485.0" layoutY="341.0" mnemonicParsing="false" onAction="#clearbuttonOnAction" prefHeight="30.0" prefWidth="77.0" text="清除" /><TextField fx:id="textquiries2" layoutX="146.0" layoutY="20.0" prefHeight="30.0" prefWidth="122.0" promptText="作者" /></children>
</AnchorPane>
借书信息查询(用户)
<?xml version="1.0" encoding="UTF-8"?><?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?><AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.lib.BorrowInquiries"><children><TableView fx:id="booktable" layoutY="69.0" prefHeight="262.0" prefWidth="606.0"><columns><TableColumn fx:id="bookname" prefWidth="145.0" text="书名" /><TableColumn fx:id="bookid" prefWidth="152.0" text="编号" /><TableColumn fx:id="borrowdate" prefWidth="158.0" text="借阅日期" /><TableColumn fx:id="returndate" prefWidth="150.0" text="归还期限" /></columns></TableView><TextField fx:id="get" layoutX="76.0" layoutY="25.0" prefHeight="30.0" prefWidth="170.0" /><Button fx:id="inquiriesbutton" layoutX="283.0" layoutY="25.0" mnemonicParsing="false" onAction="#inquiriesbuttonOnAction" prefHeight="30.0" prefWidth="73.0" text="查询" /><Button fx:id="ruturnbutton" layoutX="395.0" layoutY="25.0" mnemonicParsing="false" onAction="#ruturnbuttonOnAction" prefHeight="30.0" prefWidth="73.0" text="归还" /><Button fx:id="exitbutton" layoutX="523.0" layoutY="25.0" mnemonicParsing="false" onAction="#exitbuttonOnAction" prefHeight="30.0" prefWidth="55.0" text="退出" /></children>
</AnchorPane>
注册界面
<?xml version="1.0" encoding="UTF-8"?><?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?><AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.lib.enrollCon"><children><TextField fx:id="un" layoutX="238.0" layoutY="140.0" onAction="#unOnAction" prefHeight="30.0" prefWidth="202.0" /><PasswordField fx:id="pw" layoutX="238.0" layoutY="200.0" onAction="#pwOnAction" prefHeight="30.0" prefWidth="202.0" /><Button fx:id="enroll" layoutX="235.0" layoutY="272.0" mnemonicParsing="false" onAction="#enrollOnAction" prefHeight="30.0" prefWidth="122.0" text="提交注册" /><Label layoutX="148.0" layoutY="132.0" prefHeight="40.0" prefWidth="90.0" text="新用户名:"><font><Font size="18.0" /></font></Label><Label layoutX="151.0" layoutY="196.0" prefHeight="40.0" prefWidth="90.0" text="密 码:"><font><Font size="18.0" /></font></Label><Label layoutX="171.0" layoutY="32.0" prefHeight="51.0" prefWidth="296.0" text="图书馆用户注册"><font><Font size="40.0" /></font></Label></children>
</AnchorPane>
登入界面
<?xml version="1.0" encoding="UTF-8"?><?import java.lang.*?>
<?import java.net.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import java.net.URL?><AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.lib.Controller"><children><TextField fx:id="user" layoutX="233.0" layoutY="186.0" onAction="#userOnAction" prefHeight="30.0" prefWidth="230.0" /><PasswordField fx:id="password" layoutX="233.0" layoutY="242.0" onAction="#passwordOnAction" prefHeight="30.0" prefWidth="230.0" /><Text layoutX="169.0" layoutY="117.0" strokeType="OUTSIDE" strokeWidth="0.0" text="图书馆管理系统"><font><Font size="47.0" /></font></Text><Text layoutX="152.0" layoutY="204.0" strokeType="OUTSIDE" strokeWidth="0.0" text="用户名:"><font><Font name="System Italic" size="19.0" /></font></Text><Text layoutX="153.0" layoutY="260.0" strokeType="OUTSIDE" strokeWidth="0.0" text="密 码:"><font><Font name="System Italic" size="19.0" /></font></Text><Button fx:id="login" layoutX="160.0" layoutY="318.0" mnemonicParsing="false" onAction="#loginOnAction" prefHeight="30.0" prefWidth="60.0" text="登录" /><Button fx:id="enroll" layoutX="424.0" layoutY="318.0" mnemonicParsing="false" onAction="#enrollOnAction" prefHeight="30.0" prefWidth="60.0" text="注册" /><RadioButton fx:id="adm" layoutX="152.0" layoutY="280.0" mnemonicParsing="false" onAction="#admOnAction" prefHeight="8.0" prefWidth="76.0" text="管理员" /><Button fx:id="exit" layoutX="556.0" layoutY="14.0" mnemonicParsing="false" onAction="#exitOnAction" prefHeight="30.0" prefWidth="30.0" text="X" /></children>
</AnchorPane>
登入后选择界面
<?xml version="1.0" encoding="UTF-8"?><?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?><AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.lib.LoginCon"><children><Label layoutX="215.0" layoutY="57.0"><font><Font name="Arial Bold" size="39.0" /></font></Label><Label layoutX="213.0" layoutY="38.0" prefHeight="49.0" prefWidth="174.0" text="欢迎回来"><font><Font name="System Italic" size="42.0" /></font></Label><Button fx:id="Inquire" layoutX="176.0" layoutY="123.0" mnemonicParsing="false" onAction="#InquireOnAction" prefHeight="41.0" prefWidth="249.0" text="图书信息查询"><font><Font size="20.0" /></font></Button><Button fx:id="userRead" layoutX="176.0" layoutY="234.0" mnemonicParsing="false" onAction="#userReadOnAction" prefHeight="41.0" prefWidth="249.0" text="用户借阅查询"><font><Font size="20.0" /></font></Button><Button fx:id="exitbutton" layoutX="472.0" layoutY="310.0" mnemonicParsing="false" onAction="#exitbuttonOnAction" prefHeight="35.0" prefWidth="68.0" text="退出" /><Button fx:id="userin" layoutX="176.0" layoutY="234.0" mnemonicParsing="false" onAction="#userinOnAction" prefHeight="41.0" prefWidth="249.0" text="用户信息管理"><font><Font size="20.0" /></font></Button></children>
</AnchorPane>
借书信息查询(管理员)
<?xml version="1.0" encoding="UTF-8"?><?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?><AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.lib.userinCon"><children><TableView fx:id="booktable" layoutY="69.0" prefHeight="281.0" prefWidth="606.0"><columns><TableColumn fx:id="un" prefWidth="119.0" text="用户" /><TableColumn fx:id="pw" prefWidth="125.0" text="密码" /><TableColumn fx:id="bookname" prefWidth="115.0" text="书名" /><TableColumn fx:id="borrowdate" prefWidth="114.0" text="借阅日期" /><TableColumn fx:id="returndate" prefWidth="132.0" text="归还期限" /></columns></TableView><TextField fx:id="get" layoutX="31.0" layoutY="25.0" prefHeight="30.0" prefWidth="137.0" /><Button fx:id="inquiriesbutton" layoutX="191.0" layoutY="25.0" mnemonicParsing="false" onAction="#inquiriesbuttonOnAction" prefHeight="30.0" prefWidth="55.0" text="查询" /><Button fx:id="deletebutton" layoutX="512.0" layoutY="25.0" mnemonicParsing="false" onAction="#deleteOnAction" prefHeight="30.0" prefWidth="81.0" text="删除用户" /><Button fx:id="exitbutton" layoutX="538.0" layoutY="356.0" mnemonicParsing="false" onAction="#exitbuttonOnAction" prefHeight="30.0" prefWidth="55.0" text="退出" /><Button fx:id="pwbutton" layoutX="423.0" layoutY="25.0" mnemonicParsing="false" onAction="#pwbuttonOnAction" prefHeight="30.0" prefWidth="81.0" text="修改密码" /><TextField fx:id="get1" layoutX="271.0" layoutY="25.0" prefHeight="30.0" prefWidth="137.0" /></children>
</AnchorPane>
这篇关于Mysql+javafx+sence bulider实现图书借阅系统的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!