JavaFX+MySQL实现图书管理系统(包含完整代码)

07-03 1140阅读

目录

一、功能介绍

1.ER图 

2.学生管理员登陆页面

3.图书馆首页

4.学生图书馆搜索界面

5.学生注册

6.管理员后台

增加书籍

查看借阅记录

​编辑

修改图书

二、功能分解

1.界面展示

2.点击按钮切换页面

普通按钮点击

复杂按钮点击

3.登录注册

创建登陆注册的页面(图为登录)

设置两个输入框的id

带有sql语句的方法

写登录页面登录按钮的点击方法

在FXML文件里设置控件点击事件

注册功能的sql语句方法

4.表格控件的绑定

实体类

表格控件的Controller类

  FXML文件

给整个表格设置id

给各列设置id

5.增、删、改

从表格选择图书并删除

修改

6.模糊查询

7.左外连接查询

三、总代码

页面类

控制器类

HelloController类

图书馆控制类

搜索页面控制类

管理员后台控制类

增加图书页面控制类

修改图书控制类

管理员查看图书借阅记录控制类

实体类

图书馆

管理员后台类

type对应type-name类

JDBC封装以及一个带有sql语句的方法的类

SQL语句方法类

JDBC封装类

FXML文件

登录

登陆失败

注册

注册失败

管理员登陆

图书馆

搜索

借书成功

借书失败

还书成功

还书失败

管理员后台

查询借阅记录

修改图书

增加图书

增加成功


一、功能介绍

1.ER图 

JavaFX+MySQL实现图书管理系统(包含完整代码)

2.学生管理员登陆页面

JavaFX+MySQL实现图书管理系统(包含完整代码)

JavaFX+MySQL实现图书管理系统(包含完整代码)

3.图书馆首页

JavaFX+MySQL实现图书管理系统(包含完整代码)

JavaFX+MySQL实现图书管理系统(包含完整代码)

其他几个按扭点击均会出现不同类型的书

4.学生图书馆搜索界面

JavaFX+MySQL实现图书管理系统(包含完整代码)

JavaFX+MySQL实现图书管理系统(包含完整代码)

JavaFX+MySQL实现图书管理系统(包含完整代码)

5.学生注册

JavaFX+MySQL实现图书管理系统(包含完整代码)

6.管理员后台

JavaFX+MySQL实现图书管理系统(包含完整代码)

增加书籍

JavaFX+MySQL实现图书管理系统(包含完整代码)

查看借阅记录
JavaFX+MySQL实现图书管理系统(包含完整代码)
修改图书

JavaFX+MySQL实现图书管理系统(包含完整代码)

二、功能分解

1.界面展示

在HelloApplication类里,首先定义一个公共舞台 

    public static Stage primaryStage;

然后创建不同的场景,定义一个开始方法展示程序运行时展示的第一个页面

   public void start(Stage primaryStage) throws IOException {
        this.primaryStage = primaryStage;
        starScene();
    }

然后创建其他场景(这里举个例子):

  //学生登录
    public static void starScene() {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
            Scene scene = new Scene(fxmlLoader.load(), 693,460);
            primaryStage.setResizable(false);//设置页面大小固定
            primaryStage.setTitle("欢迎来到网络图书馆");
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

2.点击按钮切换页面

这个功能实现运用到了控制类 ,JavaFX项目在刚开始创建时有两个类,一个是HelloApplication另一个是HelloController中HelloApplication用于写各种方法并绑定不同的FXML文件,HelloController控制类处理用于控制FXML文件中的UI元素和处理逻辑。

在HelloController类中创建按钮点击时调用的HelloApplication的不同场景来实现页面转换,展示代码如下:

普通按钮点击
 @FXML
    public void back5(MouseEvent mouseEvent){HelloApplication.BackScene();}

这是普通按钮点击,比如关闭按钮,完成按钮;复杂一点的按钮点击事件需要获取页面数值并传入实现对应功能的包含sql语句的方法中比如:

复杂按钮点击
    //管理员的登录按钮
    @FXML
    public void Glogin(MouseEvent mouseEvent) throws SQLException{
        String y = usernames.getText();//获取id为usernames的textfield的数值
        String m = passwords.getText();
        Boolean ok = Student.glogin(y,m);//使用boolean类型的变量去判断sql语句方法执行的结果
        if(ok){
            HelloApplication.BackScene();//调用成功之后出现的场景
        }else {
            if (!errorDisplayed) {
                HelloApplication.exit();//失败后调用的页面
                errorDisplayed = true;
            }
        }
    }

失败之后应出现小弹窗类型的窗口去提醒客户输入信息有误,所以这时候不能去用HelloApplication的公共舞台,需要自己搭建一个舞台。但是此时会出现一个问题就是当信息有误的情况下,一直点击按钮这个方法不断被调用会出现很多个弹窗窗口导致程序变卡也不美观,解决办法就是在HelloController类里创建一个布尔类型的成员变量为false,如果用户输入消息是错误的情况下曾加一个if判断,用于判断成员变量的值是否不为false,如果为true则调用窗口,同时设置它的值为true(注意if语句里面是!成员变量),然后再次点击时就不会多次调用弹窗方法。

3.登录注册

登陆注册功能实现需要连接数据库

创建登陆注册的页面(图为登录)

JavaFX+MySQL实现图书管理系统(包含完整代码)

设置两个输入框的id

JavaFX+MySQL实现图书管理系统(包含完整代码)

JavaFX+MySQL实现图书管理系统(包含完整代码)

带有sql语句的方法
    // login 学生登陆
    public static boolean login(String username ,String password) throws SQLException {
        String sql = "select id from library.user where name = ? and password = ? and type=0";
        Connection conn = JDBC.getConn();
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, username);
        ps.setString(2, password);
        ResultSet res = ps.executeQuery();//保存查询结果的集合
        while (res.next()) {//res.next()是判断下一个结果是否为空,为空代表查询完毕
            return true;
        }
        return false;
    }
写登录页面登录按钮的点击方法
//学生页面的登录按钮
    @FXML
    public void Booklist(MouseEvent mouseEvent) throws SQLException {
        u = username.getText();
        String p = password.getText();
       //SelectController selectController = new SelectController();
       //selectController.setValues(u);//将变量u通过这种方法传到SelectController类里
       //TypeController typeController = new TypeController();
        //typeController.setValues(u);这一部分跟登录方法没有关系
        Boolean ok = Student.login(u,p);
        if(ok){
            HelloApplication.BookScene();
        }else {
            if (!errorDisplayed) {
                HelloApplication.fail();
                errorDisplayed = true;
            }
        }
        }
在FXML文件里设置控件点击事件

JavaFX+MySQL实现图书管理系统(包含完整代码)

此时一个登录方法就完成了。注册方法跟登陆方法差不多但是要注意在注册时需要对数据库现有数据进行判断以防止重复数据出现。

注册功能的sql语句方法
  //学生注册
    public static boolean slogin(String name, String npassword) throws SQLException {
        String sql1 = "select id from library.user where name = ? and password = ? and type=0";//学生和管理员在数据库的一张表通过type的值来区别。这句sql语句是查询库里是否存在有着一模一样的用户名和密码的数据
        String sql = "insert into library.user (name,password,type) values(?,?,0)";
        Connection conn = JDBC.getConn();
        PreparedStatement pg =conn.prepareStatement(sql1);
        PreparedStatement gp = conn.prepareStatement(sql);
        pg.setString(1,name);
        pg.setString(2,npassword);
        gp.setString(1, name);
        gp.setString(2, npassword);
        ResultSet re = pg.executeQuery();
        int up = gp.executeUpdate();//插入结果后更新的条数,如果插入成功即up不为0
        while (up == 0 || re.next()){//若插入失败或者查询的结果有重复的都会返回false
            return false;
        }
        return true;
    }

4.表格控件的绑定

实体类

实体类里主要包括各种实例变量的定义以及get、set方法和构造函数对实例变量进行初始化,注意实例变量的类型必须是SimpleStringProperty类

 private final SimpleStringProperty name;
    private final SimpleStringProperty writer;
    private final SimpleIntegerProperty bank;
    public SBook(String name, String writer, int bank)  {
        this.name = new SimpleStringProperty(name);
        this.writer= new SimpleStringProperty(writer);
        this.bank= new SimpleIntegerProperty(bank);
    }
    public String getName() {
        return name.get();
    }
    public SimpleStringProperty nameProperty() {
        return name;
    }
    public void setName(String name) {
        this.name.set(name);
    }
表格控件的Controller类

先定义表格控以及各列的id,查询数据库的数据后返回一个结果集(结果集类型自己定义)传输到各列上面,还有一个默认方法initialize即每次进入当前页面就会刷新表格

public class TypeController {
    public static String n;
    public TableView tableView;
    public TableColumn name;
    public TableColumn writer;
    public TableColumn bank;
    public static int a;
    public static String k;
    private boolean errorDisplayed = false;
    public void initialize() throws SQLException {
        allBook();
    }
    public void allBook()throws  SQLException{
        String sql = "select name,writer,bank from library.book_list";
        Connection conn = JDBC.getConn();
        PreparedStatement ps = conn.prepareStatement(sql);
        ResultSet resultSet = ps.executeQuery();
        ObservableList sBookList = FXCollections.observableArrayList();
        while (resultSet.next()) {
            String sBookName=resultSet.getString("name");
            String sBookWriter=resultSet.getString("writer");
            int sBookBank = resultSet.getInt("bank");
            SBook sBook = new SBook(sBookName,sBookWriter,sBookBank);
            sBookList.add(sBook);
            // 设置其他属性
        }
        name.setCellValueFactory(new PropertyValueFactory("name"));
        writer.setCellValueFactory(new PropertyValueFactory("writer"));
        bank.setCellValueFactory(new PropertyValueFactory("bank"));
        tableView.setItems(sBookList);
    }
  FXML文件

JavaFX+MySQL实现图书管理系统(包含完整代码)

给整个表格设置id

JavaFX+MySQL实现图书管理系统(包含完整代码)

给各列设置id

JavaFX+MySQL实现图书管理系统(包含完整代码)

5.增、删、改

这几个功能分别用在注册功能、借书还书功能、管理员修改功能、管理员删除功能。

从表格选择图书并删除

借书跟删除功能除了sql语句不一样大体一样,这里删除为例:

// 点击删除按钮
public void delete(MouseEvent mouseEvent) throws SQLException {
    // 获取表格视图的选择模型
    TableView.TableViewSelectionModel selectionModel = GtableView.getSelectionModel();
    
    // 获取被选中的项的列表
    ObservableList selectedItems = selectionModel.getSelectedItems();
    // 检查是否有选中的项
    if (!selectedItems.isEmpty()) {
        // 假设只选择了一行,获取选中的第一项
        GBook selectedItem = selectedItems.get(0);
        // 检查是否成功获取选中项
        if (selectedItem != null) {
            // 获取选中项的作者名字
            String writerName = selectedItem.getWriter();
            // 调用删除方法,传入作者名字
            delete(writerName);
        }
        // 从表格视图中移除选中的行
        ObservableList selectedRows = selectionModel.getSelectedItems();
        GtableView.getItems().removeAll(selectedRows);
    } else {
        // 没有选中项的情况下输出提示
        System.out.println("请选择一本书");
    }
}
修改

修改方法主要使用在管理员从图书馆后台选择书籍去对书籍的名字、作者、种类、库存进行修改。这里在选择某一行时要获取这一行图书的信息并存为成员变量,再点击修改时修改页面就会出现原有的信息。

 //管理员看到图书页面的修改按钮
    @FXML
    public  void alter(MouseEvent mouseEvent){
        TableView.TableViewSelectionModel selectionModel = GtableView.getSelectionModel();
        ObservableList selectedItems = selectionModel.getSelectedItems();
        if(!selectedItems.isEmpty()){
            GBook selectedItem = selectedItems.get(0);
            if (selectedItem != null) {
                int i = selectedItem.getId();
                n = selectedItem.getName();
                w = selectedItem.getWriter();
                t = selectedItem.getType();
                b = selectedItem.getBank();
                AlterController alterController=new AlterController();
                alterController.setValue(n,w,t,b,i);//传入修改的页面Controller类,使TextFiled获取数据
                HelloApplication.AlterScene();
        }
        }else{
            System.out.println("请选择一本书");
        }
    }

6.模糊查询

首先使用两个成员变量储存用户输入的值,这两个变量将作为sql语句条件去查询。这里是让用户可以输入书名或者作者名或者两者综合去查询。若两个都输入这时就要筛选同时符合这两个条件的书籍,这里用了MySQL的if函数

    public void sou(MouseEvent mouseEvent) throws SQLException {
        b = bookname.getText();
        w = bookwriter.getText();
        loadData();
    }
    public void loadData() throws SQLException {
        String sql = "select name,writer,bank from library.book_list where if(? = '',true, name like ?) and if( ? = '',true,writer like ?)";
        Connection conn = JDBC.getConn();
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1,b);
        ps.setString(2,"%"+b+"%");
        ps.setString(3,w);
        ps.setString(4,"%"+w+"%");
        ResultSet resultSet = ps.executeQuery();
        ObservableList sBookList = FXCollections.observableArrayList();
        while (resultSet.next()) {
            String sBookName=resultSet.getString("name");
            String sBookWriter=resultSet.getString("writer");
            int sBookBank = resultSet.getInt("bank");
            SBook sBook = new SBook(sBookName,sBookWriter,sBookBank);
            sBookList.add(sBook);            // 设置其他属性
        }
        name.setCellValueFactory(new PropertyValueFactory("name"));
        writer.setCellValueFactory(new PropertyValueFactory("writer"));
        bank.setCellValueFactory(new PropertyValueFactory("bank"));
        tableView.setItems(sBookList);
    }

7.左外连接查询

这是两表一起查询,在数据库里图书种类我用了数字代替,而在管理员后台看到的是每种数字代表的图书种类。我又额外增添一个type跟type-name对应的表。

    public void initialize() throws SQLException {
        loadData();
    }
    public void loadData() throws SQLException {
        String sql = "select * from book_list as b left join book_type as bt on b.type=bt.book_type";//查询图书数字代表的名称采用左外连接的方法
        Connection conn = JDBC.getConn();
        PreparedStatement ps = conn.prepareStatement(sql);
        ResultSet resultSet = ps.executeQuery();
        ObservableList gBookList = FXCollections.observableArrayList();
        while (resultSet.next()) {
            String gBookName=resultSet.getString("name");
            String gBookWriter=resultSet.getString("writer");
            String gBookBank = resultSet.getString("bank");
            int gBookId= resultSet.getInt("id");
            String gBookUserName=resultSet.getString("username");
            String gBookType= resultSet.getString("type_name");
            GBook gBook = new GBook(gBookName,gBookWriter,gBookBank,gBookUserName,gBookType,gBookId);
            gBookList.add(gBook);
            // 设置其他属性
        }
        Gname.setCellValueFactory(new PropertyValueFactory("name"));
        Gwriter.setCellValueFactory(new PropertyValueFactory("writer"));
        Gbank.setCellValueFactory(new PropertyValueFactory("bank"));
        UserName.setCellValueFactory(new PropertyValueFactory("username"));
        Gtype.setCellValueFactory(new PropertyValueFactory("type"));
        GtableView.setItems(gBookList);
    }

三、总代码

虽然采用MVC对包进行分类但是代码并没有遵守。每个表格控件对应一个Controller类,其他功能基本在HelloController类里,带有sql的方法有的写在控制类里,有的单独写了一个类。

页面类

package com.example.controller;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class HelloApplication extends Application {
    public static Stage primaryStage;
    public void start(Stage primaryStage) throws IOException {
        this.primaryStage = primaryStage;
        starScene();
    }
//登陆不成功显示的画面
    public static void fail(){
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("win.fxml"));
            Scene scene = new Scene(fxmlLoader.load(), 500,150 );
            Stage stage = new Stage();
            stage.setResizable(false);
            stage.setTitle("登陆失败");
            stage.setScene(scene);
            stage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //注册显示用户已存在
    public static void exit(){
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("exit.fxml"));
            Scene scene = new Scene(fxmlLoader.load(), 500,150 );
            Stage stage = new Stage();
            stage.setResizable(false);
            stage.setTitle("注册失败");
            stage.setScene(scene);
            stage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //还书失败
    public static void backFalse(){
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("backfalse.fxml"));
            Scene scene = new Scene(fxmlLoader.load(), 500,150 );
            Stage stage = new Stage();
            stage.setResizable(false);
            stage.setTitle("还书失败");
            stage.setScene(scene);
            stage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //还书成功
    public static void backWin(){
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("backwin.fxml"));
            Scene scene = new Scene(fxmlLoader.load(), 500,150 );
            Stage stage = new Stage();
            stage.setResizable(false);
            stage.setTitle("还书成功");
            stage.setScene(scene);
            stage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //借书成功
    public static void last(){
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("Last.fxml"));
            Scene scene = new Scene(fxmlLoader.load(), 500,150 );
            Stage stage = new Stage();
            stage.setResizable(false);
            stage.setTitle("借书成功");
            stage.setScene(scene);
            stage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //提醒用户还书
    public static void back(){
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("back.fxml"));
            Scene scene = new Scene(fxmlLoader.load(), 500,150 );
            Stage stage = new Stage();
            stage.setResizable(false);
            stage.setTitle("借书失败");
            stage.setScene(scene);
            stage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //学生登录
    public static void starScene() {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
            Scene scene = new Scene(fxmlLoader.load(), 693,460);
            primaryStage.setResizable(false);
            primaryStage.setTitle("欢迎来到网络图书馆");
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //管理员登陆
    public static void loginScene(){
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("login.fxml"));
            Scene scene = new Scene(fxmlLoader.load(), 693, 460);
            primaryStage.setResizable(false);
            primaryStage.setTitle("图书馆管理");
            primaryStage.setScene(scene);
            primaryStage.show();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    //管理员看到的后台数据
    public static void BackScene(){
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("backstage.fxml"));
            Scene scene = new Scene(fxmlLoader.load(), 693, 580);
            primaryStage.setResizable(false);
            primaryStage.setTitle("后台管理");
            primaryStage.setScene(scene);
            primaryStage.show();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    //增加书籍
    public static void addBook(){
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("addBook.fxml"));
            Scene scene = new Scene(fxmlLoader.load(), 693, 500);
            primaryStage.setResizable(false);
            primaryStage.setTitle("后台管理");
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    //添加成功的方法
    public static void Addwin(){
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("addwin.fxml"));
            Scene scene = new Scene(fxmlLoader.load(), 693, 500);
            primaryStage.setResizable(false);
            primaryStage.setTitle("成功了");
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    //查看图书被借阅次数的页面
    public static void inBook(){
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("inBook.fxml"));
            Scene scene = new Scene(fxmlLoader.load(), 693, 400);
            primaryStage.setResizable(false);
            primaryStage.setTitle("后台管理");
            primaryStage.setScene(scene);
            primaryStage.show();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    //注册的方法
    public static void registerScene(){
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("zhuce.fxml"));
            Scene scene = new Scene(fxmlLoader.load(), 693, 460);
            primaryStage.setResizable(false);
            primaryStage.setTitle("注册");
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    //学生登陆进去后显示的图书种类
    public static void BookScene(){
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("Booktype.fxml"));
            Scene scene = new Scene(fxmlLoader.load(), 693, 460);
            primaryStage.setResizable(false);
            primaryStage.setTitle("图书种类");
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    //管理员修改图书的页面
    public static void AlterScene(){
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("alter.fxml"));
            Scene scene = new Scene(fxmlLoader.load(), 693, 400);
            primaryStage.setResizable(false);
            primaryStage.setTitle("修改");
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    //搜索页面
    public static void select(){
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("select.fxml"));
            Scene scene = new Scene(fxmlLoader.load(), 693, 515);
            primaryStage.setResizable(false);
            primaryStage.setTitle("搜索");
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public static void main(String[] args) {
        launch(args);
    }
}

控制器类

HelloController类
package com.example.controller;
import com.example.dao.GBook;
import com.example.dao.JDBC;
import com.example.dao.Student;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Objects;
public class HelloController {
    public int b;
    public static String u;
    @FXML
    private TextField username;
    @FXML
    private PasswordField password;
    @FXML
    private TextField usernames;
    @FXML
    private PasswordField passwords;
    @FXML
    private TextField Name;
    @FXML
    private PasswordField npassword;
    @FXML
    private PasswordField apassword;
    private boolean errorDisplayed = false;
    public HelloController() {
    }
    //学生登陆页面的按钮点击跳转管理员登陆界面
    @FXML
    public void ButtonClick(MouseEvent mouseEvent){HelloApplication.loginScene();}
    //管理员的登录按钮
    @FXML
    public void Glogin(MouseEvent mouseEvent) throws SQLException{
        String y = usernames.getText();
        String m = passwords.getText();
        Boolean ok = Student.glogin(y,m);
        if(ok){
            HelloApplication.BackScene();
        }else {
            if (!errorDisplayed) {
                HelloApplication.exit();
                errorDisplayed = true;
            }
        }
    }
    //从管理员登录界面退回到学生登陆界面
    @FXML
    public void back1(MouseEvent mouseEvent){
        HelloApplication.starScene();
    }
    //添加成功后继续添加书籍的按钮
    @FXML
    public void back4(MouseEvent mouseEvent){
        HelloApplication.addBook();
    }
    //添加图书成功之后返回后台的按钮
    @FXML
    public void back5(MouseEvent mouseEvent){HelloApplication.BackScene();}
    //学生页面的注册按钮
    @FXML
    public void register(MouseEvent mouseEvent){HelloApplication.registerScene();}
    //学生页面的登录按钮
    @FXML
    public void Booklist(MouseEvent mouseEvent) throws SQLException {
        u = username.getText();
        String p = password.getText();
        SelectController selectController = new SelectController();
        selectController.setValues(u);
        TypeController typeController = new TypeController();
        typeController.setValues(u);
        Boolean ok = Student.login(u,p);
        if(ok){
            HelloApplication.BookScene();
        }else {
            // Only display the error page if it hasn't been displayed before.
            if (!errorDisplayed) {
                HelloApplication.fail();
                // Set the flag to true to indicate that the error page has been displayed.
                errorDisplayed = true;
            }
        }
        }
    //学生页面的注册按钮
    @FXML
    public void regis(MouseEvent mouseEvent) throws SQLException {
        String u = Name.getText();
        String p = npassword.getText();
        String a = apassword.getText();
        if (Objects.equals(p, a)) {
            Boolean ok = Student.slogin(u, p);
            if (ok)
                HelloApplication.starScene();
        } else {
            // Only display the error page if it hasn't been displayed before.
            if (!errorDisplayed) {
                HelloApplication.fail();
                // Set the flag to true to indicate that the error page has been displayed.
                errorDisplayed = true;
            }
        }
    }
    //书籍界面通过给b赋值确定图书种类
    @FXML
    public void type1(MouseEvent mouseEvent){
        b=0;
    }
    @FXML
    public void type2(MouseEvent mouseEvent){
        b=1;
    }
    @FXML
    public void type3(MouseEvent mouseEvent){
        b=2;
    }
    @FXML
    public void type4(MouseEvent mouseEvent){
        b=3;
    }
//种类界面的搜索按钮
    public void select(MouseEvent mouseEvent) {
        HelloApplication.select();
    }
    public void back7(MouseEvent mouseEvent) throws SQLException {
        boolean ok = backBook();
        if(ok){
            HelloApplication.backWin();
        }
        else{
            HelloApplication.backFalse();
        }
    }
    public boolean backBook() throws SQLException {
        String sql = "update library.user set BookName = null where name = ?";
        String sql2 = "update library.book_list set bank = bank+1 where UserName = ? ";
        String sql3 = "update library.book_list set UserName = null where UserName = ?";
        Connection con = JDBC.getConn();
        PreparedStatement ps = con.prepareStatement(sql);
        PreparedStatement pd = con.prepareStatement(sql2);
        PreparedStatement pg = con.prepareStatement(sql3);
        ps.setString(1,u);
        pd.setString(1,u);
        pg.setString(1,u);
        int rs=ps.executeUpdate();
        int rd = pd.executeUpdate();
        int rg=pg.executeUpdate();
        if(rs!=0 & rd!=0){
            if(rg!=0){
                return true;
            }else{
                return false;
            }
        }
        else {
            return false;
        }
    }
    public void back9(MouseEvent mouseEvent) {
        HelloApplication.starScene();
    }
    public void back10(MouseEvent mouseEvent) {
        HelloApplication.starScene();
    }
}
图书馆控制类
package com.example.controller;
import com.example.dao.JDBC;
import com.example.dao.SBook;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class TypeController {
    public static String n;
    public TableView tableView;
    public TableColumn name;
    public TableColumn writer;
    public TableColumn bank;
    public static int a;
    public static String k;
    private boolean errorDisplayed = false;
    public void initialize() throws SQLException {
        allBook();
    }
    public void allBook()throws  SQLException{
        String sql = "select name,writer,bank from library.book_list";
        Connection conn = JDBC.getConn();
        PreparedStatement ps = conn.prepareStatement(sql);
        ResultSet resultSet = ps.executeQuery();
        ObservableList sBookList = FXCollections.observableArrayList();
        while (resultSet.next()) {
            String sBookName=resultSet.getString("name");
            String sBookWriter=resultSet.getString("writer");
            int sBookBank = resultSet.getInt("bank");
            SBook sBook = new SBook(sBookName,sBookWriter,sBookBank);
            sBookList.add(sBook);
            // 设置其他属性
        }
        name.setCellValueFactory(new PropertyValueFactory("name"));
        writer.setCellValueFactory(new PropertyValueFactory("writer"));
        bank.setCellValueFactory(new PropertyValueFactory("bank"));
        tableView.setItems(sBookList);
    }
    public void loadData() throws SQLException {
        String sql = "select name,writer,bank from library.book_list where type = ?";
        Connection conn = JDBC.getConn();
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setInt(1,a);
        ResultSet resultSet = ps.executeQuery();
        ObservableList sBookList = FXCollections.observableArrayList();
        while (resultSet.next()) {
            String sBookName=resultSet.getString("name");
            String sBookWriter=resultSet.getString("writer");
            int sBookBank = resultSet.getInt("bank");
            SBook sBook = new SBook(sBookName,sBookWriter,sBookBank);
            sBookList.add(sBook);
            // 设置其他属性
        }
        name.setCellValueFactory(new PropertyValueFactory("name"));
        writer.setCellValueFactory(new PropertyValueFactory("writer"));
        bank.setCellValueFactory(new PropertyValueFactory("bank"));
        tableView.setItems(sBookList);
    }
    @FXML
    public void type1(MouseEvent mouseEvent) throws SQLException {
        a=0;
        loadData();
    }
    @FXML
    public void type2(MouseEvent mouseEvent) throws SQLException {
        a=1;
        loadData();
    }
    @FXML
    public void type3(MouseEvent mouseEvent) throws SQLException {
        a=2;
        loadData();
    }
    @FXML
    public void type4(MouseEvent mouseEvent) throws SQLException {
        a=3;
        loadData();
    }
//显示全部书籍
    public void comein(MouseEvent mouseEvent) throws Exception {
        initialize();
    }
//搜索
    public void select(MouseEvent mouseEvent) {
        HelloApplication.select();
    }
    //还书
    public void back7(MouseEvent mouseEvent) throws SQLException {
        boolean ok = backBook();
        if(ok){
            HelloApplication.backWin();
            allBook();
        }
        else {
            // Only display the error page if it hasn't been displayed before.
            if (!errorDisplayed) {
                HelloApplication.backFalse();
                // Set the flag to true to indicate that the error page has been displayed.
                errorDisplayed = true;
            }
        }
        }
    public boolean backBook() throws SQLException {
        String sql = "update library.user set BookName = null where name = ?";
        String sql2 = "update library.book_list set bank = bank+1 where UserName = ? ";
        String sql3 = "update library.book_list set UserName = null where UserName = ?";
        Connection con = JDBC.getConn();
        PreparedStatement ps = con.prepareStatement(sql);
        PreparedStatement pd = con.prepareStatement(sql2);
        PreparedStatement pg = con.prepareStatement(sql3);
        ps.setString(1,n);
        pd.setString(1,n);
        pg.setString(1,n);
        int rs=ps.executeUpdate();
        int rd = pd.executeUpdate();
        int rg=pg.executeUpdate();
        if(rs!=0 & rd!=0){
            if(rg!=0){
                return true;
            }else{
                return false;
            }
        }
        else {
            return false;
        }
    }
    //选好书之后
    public void OK(MouseEvent mouseEvent) throws SQLException {
        TableView.TableViewSelectionModel selectionModel = tableView.getSelectionModel();
        ObservableList selectedItems = selectionModel.getSelectedItems();
        if(!selectedItems.isEmpty()){
            SBook selectedItem = selectedItems.get(0);
            if(selectedItem != null){
                k = selectedItem.getName();
            }
        }
        else{
            System.out.println("请选择一本书");
        }
        String sql3 = "select BookName from library.user where name=?";
        Connection conn = JDBC.getConn();
        PreparedStatement pb = conn.prepareStatement(sql3);
        pb.setString(1, n);
        ResultSet rs = pb.executeQuery();
        String bk =null;
        while(rs.next()){
            bk=rs.getString("BookName");
        }
        if (bk ==null) {
            boolean ok = addName();
            boolean kc = bank();
            if(ok & kc){
                HelloApplication.last();
                allBook();
            }else{
                HelloApplication.back();
            }
        }
    }
    public boolean addName() throws SQLException {
        String sql = "update library.book_list set UserName = ? where name = ?";
        String sql2 = "update library.user set BookName = ? where name = ?";
        String sql3="insert into library.book_name (BookName,name) values(?,?)";
        Connection conn = JDBC.getConn();
        PreparedStatement ps = conn.prepareStatement(sql);
        PreparedStatement pg = conn.prepareStatement(sql2);
        PreparedStatement gp = conn.prepareStatement(sql3);
        ps.setString(1,n);
        ps.setString(2,k);
        pg.setString(1,k);
        pg.setString(2,n);
        gp.setString(1,k);
        gp.setString(2,n);
        int rs = ps.executeUpdate();
        int rg = pg.executeUpdate();
        int rp = gp.executeUpdate();
        if(rs != 0 & rg !=0 & rp != 0){
            return true;
        }
        return false;
    }
    public boolean bank() throws SQLException{
        String sql ="update library.book_list set bank = bank-1 where name = ?";
        Connection conn = JDBC.getConn();
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1,k);
        int t = ps.executeUpdate();
        if (t != 0){
            return true;
        }
        return false;
    }
//返回登陆页面
    public void back9(MouseEvent mouseEvent) {
            HelloApplication.starScene();
    }
    public void setValues(String u) {
        n=u;
    }
}
搜索页面控制类
package com.example.controller;
import com.example.dao.JDBC;
import com.example.dao.SBook;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class SelectController {
    @FXML
    private TableView tableView;
    @FXML
    private TableColumn name;
    @FXML
    private TableColumn writer;
    @FXML
    private TableColumn bank;
    @FXML
    private TextField bookname;
    @FXML
    private TextField bookwriter;
    public String b;
    public String w;
    public static String n;
    public static String k;
    public void back7(MouseEvent mouseEvent) {
        HelloApplication.BookScene();
    }
    public void sou(MouseEvent mouseEvent) throws SQLException {
        b = bookname.getText();
        w = bookwriter.getText();
        loadData();
    }
    public void loadData() throws SQLException {
        String sql = "select name,writer,bank from library.book_list where if(? = '',true, name like ?) and if( ? = '',true,writer like ?)";
        Connection conn = JDBC.getConn();
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1,b);
        ps.setString(2,"%"+b+"%");
        ps.setString(3,w);
        ps.setString(4,"%"+w+"%");
        ResultSet resultSet = ps.executeQuery();
        ObservableList sBookList = FXCollections.observableArrayList();
        while (resultSet.next()) {
            String sBookName=resultSet.getString("name");
            String sBookWriter=resultSet.getString("writer");
            int sBookBank = resultSet.getInt("bank");
            SBook sBook = new SBook(sBookName,sBookWriter,sBookBank);
            sBookList.add(sBook);            // 设置其他属性
        }
        name.setCellValueFactory(new PropertyValueFactory("name"));
        writer.setCellValueFactory(new PropertyValueFactory("writer"));
        bank.setCellValueFactory(new PropertyValueFactory("bank"));
        tableView.setItems(sBookList);
    }
    public void last(MouseEvent mouseEvent)throws SQLException {
        TableView.TableViewSelectionModel selectionModel = tableView.getSelectionModel();
        ObservableList selectedItems = selectionModel.getSelectedItems();
        if(!selectedItems.isEmpty()){
            SBook selectedItem = selectedItems.get(0);
            if(selectedItem != null){
                k = selectedItem.getName();
            }
        }else {
            System.out.println("请选择一本书");
        }
        String sql = "select BookName from library.user where name = ?";
        String sql3 = "select bank from library.book_list where name=?";
        String sql2 ="insert into library.book_name (BookName,name) values(?,?)";
        Connection conn = JDBC.getConn();
        PreparedStatement ps = conn.prepareStatement(sql);
        PreparedStatement pb = conn.prepareStatement(sql3);
        PreparedStatement pg = conn.prepareStatement(sql2);
        ps.setString(1, n);
        pb.setString(1, k);
        pg.setString(1,k);
        pg.setString(2,n);
        ResultSet re = ps.executeQuery();
        ResultSet rs = pb.executeQuery();
        int rg=pg.executeUpdate();
        String bn = null;
        int bk = 0;
        while (rs.next()&re.next()&rg!=0) {
            bk = rs.getInt("bank");
            bn=re.getString("BookName");
        }
        if (bk != 0 &bn ==null) {
            boolean ok = addName();
            boolean kc = bank();
            if(ok & kc){
                HelloApplication.last();
            }
        } else {
            HelloApplication.back();
        }
    }
    public boolean addName() throws SQLException {
        String sql = "update library.book_list set UserName = ? where name = ?";
        String sql2 = "update library.user set BookName = ? where name = ?";
        Connection conn = JDBC.getConn();
        PreparedStatement ps = conn.prepareStatement(sql);
        PreparedStatement pg = conn.prepareStatement(sql2);
        ps.setString(1,n);
        ps.setString(2,k);
        pg.setString(1,k);
        pg.setString(2,n);
        int rs = ps.executeUpdate();
        int rg = pg.executeUpdate();
        if(rs != 0 & rg !=0){
            return true;
        }
        return false;
    }
    public boolean bank() throws SQLException{
        String sql ="update library.book_list set bank = bank-1 where name = ?";
        Connection conn = JDBC.getConn();
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1,k);
        int t = ps.executeUpdate();
        if (t != 0){
            return true;
        }
        return false;
    }
    public void setValues(String u) {
        n=u;
    }
}
管理员后台控制类
package com.example.controller;
import com.example.dao.GBook;
import com.example.dao.JDBC;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class GbookController {
    public TableView GtableView;
    public TableColumn Gname;
    public TableColumn Gwriter;
    public TableColumn Gbank;
    public TableColumn Gtype;
    public TableColumn UserName;
    private static String n;
    public static String w;
    public static String t;
    public static String b;
    public void initialize() throws SQLException {
        loadData();
    }
    public void loadData() throws SQLException {
        String sql = "select * from book_list as b left join book_type as bt on b.type=bt.book_type";
        Connection conn = JDBC.getConn();
        PreparedStatement ps = conn.prepareStatement(sql);
        ResultSet resultSet = ps.executeQuery();
        ObservableList gBookList = FXCollections.observableArrayList();
        while (resultSet.next()) {
            String gBookName=resultSet.getString("name");
            String gBookWriter=resultSet.getString("writer");
            String gBookBank = resultSet.getString("bank");
            int gBookId= resultSet.getInt("id");
            String gBookUserName=resultSet.getString("username");
            String gBookType= resultSet.getString("type_name");
            GBook gBook = new GBook(gBookName,gBookWriter,gBookBank,gBookUserName,gBookType,gBookId);
            gBookList.add(gBook);
            // 设置其他属性
        }
        Gname.setCellValueFactory(new PropertyValueFactory("name"));
        Gwriter.setCellValueFactory(new PropertyValueFactory("writer"));
        Gbank.setCellValueFactory(new PropertyValueFactory("bank"));
        UserName.setCellValueFactory(new PropertyValueFactory("username"));
        Gtype.setCellValueFactory(new PropertyValueFactory("type"));
        GtableView.setItems(gBookList);
    }
    //删除的方法
    public Boolean delete(String writer)throws SQLException {
            String sql="delete from library.book_list where writer = ? ";
            Connection con = JDBC.getConn();
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1,writer);
            int r = ps.executeUpdate();
           if(r != 0){
               return true;
           }
           return false;
    }
    @FXML
    public void back2(MouseEvent mouseEvent){
        HelloApplication.loginScene();
    }
    //点击删除按钮
    public void delete(MouseEvent mouseEvent) throws SQLException {
        TableView.TableViewSelectionModel selectionModel = GtableView.getSelectionModel();
        ObservableList selectedItems = selectionModel.getSelectedItems();
        if(!selectedItems.isEmpty()){
            GBook selectedItem = selectedItems.get(0); // 假设只选择了一行
            if (selectedItem != null) {
                String s = selectedItem.getWriter();
                delete(s);
            }
            ObservableList selectedRows = selectionModel.getSelectedItems();
            GtableView.getItems().removeAll(selectedRows);
        }else{
            System.out.println("请选择一本书");
        }
        }
    //添加图书
    @FXML
    public void AddBook(MouseEvent mouseEvent) throws SQLException {
        HelloApplication.addBook();
    }
    //管理员看到图书页面的修改按钮
    @FXML
    public  void alter(MouseEvent mouseEvent){
        TableView.TableViewSelectionModel selectionModel = GtableView.getSelectionModel();
        ObservableList selectedItems = selectionModel.getSelectedItems();
        if(!selectedItems.isEmpty()){
            GBook selectedItem = selectedItems.get(0);
            if (selectedItem != null) {
                int i = selectedItem.getId();
                n = selectedItem.getName();
                w = selectedItem.getWriter();
                t = selectedItem.getType();
                b = selectedItem.getBank();
                AlterController alterController=new AlterController();
                alterController.setValue(n,w,t,b,i);
                HelloApplication.AlterScene();
        }
        }else{
            System.out.println("请选择一本书");
        }
    }
//点击查看书籍详情
    public void look(MouseEvent mouseEvent) {
        TableView.TableViewSelectionModel selectionModel = GtableView.getSelectionModel();
        ObservableList selectedItems = selectionModel.getSelectedItems();
        if (!selectedItems.isEmpty()) {
            GBook selectedItem = selectedItems.get(0);
            if (selectedItem != null) {
                String s = selectedItem.getName();
                InbookController inbookController = new InbookController();
                inbookController.setValue(s);
                HelloApplication.inBook();
            }
        } else {
            System.out.println("No item is selected.");
        }
    }
}
增加图书页面控制类
package com.example.controller;
import com.example.dao.GBook;
import javafx.fxml.FXML;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import java.sql.SQLException;
public class AddbookController {
    @FXML
    private TextField Aname;
    @FXML
    private TextField Awriter;
    @FXML
    private TextField Atype;
    @FXML
    public void addwin(MouseEvent mouseEvent) throws SQLException {
        String n = Aname.getText();
        String w = Awriter.getText();
        String t = Atype.getText();
        Boolean ok = GBook.addBook(n,w,t);
        if(ok){
            HelloApplication.Addwin();
        }
    }
    @FXML
    public void back3(MouseEvent mouseEvent){
        HelloApplication.BackScene();
    }
}
修改图书控制类
package com.example.controller;
import com.example.dao.JDBC;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class AlterController {
    public TextField Xname;
    public TextField Xwriter;
    public TextField Xtype;
    public TextField Xbank;
    public static String u;
    public static String r;
    public static String y;
    public static int i;
    public static String s;
    public static String m;
    public void initialize() throws SQLException {
        setValue(u,r,y,s,i);
        Xname.setText(u);
        Xwriter.setText(r);
        Xtype.setText(y);
        Xbank.setText(s);
    }
    public void setValue(String a,String b,String c,String e,int d){
        u=a;
        r=b;
        y=c;
        s=e;
        i=d;
    }
    public boolean alter(String XName,String XWriter,String XType,String Xbank) throws SQLException {
        String sql1="select book_type from library.book_type where type_name = ?";
        String sql = "update library.book_list set name=?,writer=? ,type=?,bank=? where id= ? ";
        Connection con= JDBC.getConn();
        PreparedStatement pg=con.prepareStatement(sql1);
        PreparedStatement ps = con.prepareStatement(sql);
        pg.setString(1,XType);
        ResultSet re = pg.executeQuery();
        while (re.next()){
            m= re.getString("book_type");
        }
        ps.setString(1,XName);
        ps.setString(2,XWriter);
        ps.setString(3,m);
        ps.setString(4,Xbank);
        ps.setInt(5,i);
        int rs = ps.executeUpdate();
        if(rs ==0){
            return false;
        }
        return true;
    }
    public void finish(MouseEvent mouseEvent) throws SQLException {
        String m = Xname.getText();
        String r = Xwriter.getText();
      String p = Xtype.getText();
      String q = Xbank.getText();
        boolean ok =  alter(m,r,p,q);
        if(ok) {
            HelloApplication.BackScene();
        }
        else{
            HelloApplication.last();
        }
    }
    public void back8(MouseEvent mouseEvent) {
        HelloApplication.BackScene();
    }
}
管理员查看图书借阅记录控制类
package com.example.controller;
import com.example.dao.BookName;
import com.example.dao.JDBC;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class InbookController {
    public static String r;
    @FXML
    private TableView BtableView;
    @FXML
    private TableColumn Bookname;
    @FXML
    private  TableColumn Bname;
    public void setValue(String s){
        r=s;
    }
    public void initialize() throws SQLException {
        loadData();
    }
    public void loadData() throws SQLException {
        String sql = "select BookName,name from library.book_name where BookName = ?";
        Connection conn = JDBC.getConn();
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1,r);
        ResultSet resultSet = ps.executeQuery();
        ObservableList BookList = FXCollections.observableArrayList();
        while (resultSet.next()) {
            String gBookName=resultSet.getString("BookName");
            String gBookUserName=resultSet.getString("name");
            BookName Book = new BookName(gBookName,gBookUserName);
            BookList.add(Book);
            // 设置其他属性
        }
        Bookname.setCellValueFactory(new PropertyValueFactory("name"));
        Bname.setCellValueFactory(new PropertyValueFactory("username"));
        BtableView.setItems(BookList);
    }
    public void back11(MouseEvent mouseEvent) {
        HelloApplication.BackScene();
    }
}

实体类

图书馆
package com.example.dao;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
public class SBook {
    private final SimpleStringProperty name;
    private final SimpleStringProperty writer;
    private final SimpleIntegerProperty bank;
    public SBook(String name, String writer, int bank)  {
        this.name = new SimpleStringProperty(name);
        this.writer= new SimpleStringProperty(writer);
        this.bank= new SimpleIntegerProperty(bank);
    }
    public String getName() {
        return name.get();
    }
    public SimpleStringProperty nameProperty() {
        return name;
    }
    public void setName(String name) {
        this.name.set(name);
    }
    public String getWriter(){
        return writer.get();
    }
    public SimpleStringProperty writerProperty() {
        return writer;
    }
    public void setWriter(String writer) {
        this.writer.set(writer);
    }
    public SimpleIntegerProperty bankProperty() {
        return bank;
    }
    public void setBank(int bank) {
        this.bank.set(bank);
    }
    public int getBank(){
        return bank.get();
    }
}
管理员后台类
package com.example.dao;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class GBook {
    private final SimpleStringProperty name;
    private final SimpleStringProperty writer;
    private final SimpleStringProperty username;
    private final SimpleStringProperty bank;
    private final SimpleStringProperty type;
    private final SimpleIntegerProperty id;
    public GBook(String name, String writer, String bank,String username ,String type,int id)  {
        this.name = new SimpleStringProperty(name);
        this.writer= new SimpleStringProperty(writer);
        this.bank= new SimpleStringProperty(bank);
        this.id= new SimpleIntegerProperty(id);
        this.username= new SimpleStringProperty(username);
        this.type= new SimpleStringProperty(type);
    }
    public String getName() {
        return name.get();
    }
    public SimpleStringProperty nameProperty() {
        return name;
    }
    public void setName(String name) {
        this.name.set(name);
    }
    public String getWriter(){
        return writer.get();
    }
    public SimpleStringProperty writerProperty() {
        return writer;
    }
    public void setWriter(String writer) {
        this.writer.set(writer);
    }
    public SimpleStringProperty bankProperty() {
        return bank;
    }
    public void setBank(String bank) {
        this.bank.set(bank);
    }
    public String getBank(){
        return bank.get();
    }
    public String getUsername(){
        return username.get();
    }
    public SimpleStringProperty usernameProperty() {
        return username;
    }
    public void setUsername(String username) {
        this.username.set(username);
    }
    public SimpleStringProperty typeProperty() {
        return type;
    }
    public SimpleIntegerProperty idProperty() {
        return id;
    }
    public void setId(int id) {
        this.id.set(id);
    }
    public int getId(){
        return id.get();
    }
    public void setType(String type) {
        this.type.set(type);
    }
    public String getType(){
        return type.get();
    }
    public static boolean addBook(String Aname,String Awriter,String Atype) throws SQLException {
        String sql1="select name,writer from library.book_list where name =? and writer = ?";
        String sql = "insert into library.book_list (name,writer,bank,type) values(?,?,1,?)";
        Connection conn = JDBC.getConn();
        PreparedStatement pg = conn.prepareStatement(sql1);
        PreparedStatement gp = conn.prepareStatement(sql);
        pg.setString(1,Aname);
        pg.setString(2,Awriter);
        gp.setString(1,Aname);
        gp.setString(2,Awriter);
        gp.setString(3,Atype);
        ResultSet re=pg.executeQuery();
        int up=gp.executeUpdate();
        if(up ==0 && re.next()){
            System.out.println("增加失败");
            return false;
        }
        return true;
    }
}
type对应type-name类
package com.example.dao;
import javafx.beans.property.SimpleStringProperty;
public class BookName{
    private final SimpleStringProperty name;
    private final SimpleStringProperty username;
    public BookName(String name, String username) {
        this.name = new SimpleStringProperty(name);
        this.username = new SimpleStringProperty(username);
    }
    public String getName() {
        return name.get();
    }
    public SimpleStringProperty nameProperty() {
        return name;
    }
    public void setName(String name) {
        this.name.set(name);
    }
    public String getUsername(){
        return username.get();
    }
    public SimpleStringProperty usernameProperty() {
        return username;
    }
    public void setUsername(String username) {
        this.username.set(username);
    }
}
JDBC封装以及一个带有sql语句的方法的类
SQL语句方法类
package com.example.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.example.dao.JDBC;
import static com.example.dao.JDBC.getConn;
public class Student {
    // login 学生登陆
    public static boolean login(String username ,String password) throws SQLException {
        String sql = "select id from library.user where name = ? and password = ? and type=0";
        Connection conn = getConn();
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, username);
        ps.setString(2, password);
        ResultSet res = ps.executeQuery();
        while (res.next()) {
            return true;
        }
        return false;
    }
    public static boolean glogin(String usernames, String passwords) throws SQLException {
        String sql = "select id from library.user where name = ? and password = ? and type=1";
        Connection conn = getConn();
        PreparedStatement gp = conn.prepareStatement(sql);
        gp.setString(1, usernames);
        gp.setString(2, passwords);
        ResultSet res =  gp.executeQuery();
            while (res.next()) {
                return true;
            }
            return false;
    }
    public static boolean slogin(String name, String npassword) throws SQLException {
        String sql1 = "select id from library.user where name = ? and password = ? and type=0";
        String sql = "insert into library.user (name,password,type) values(?,?,0)";
        Connection conn = getConn();
        PreparedStatement pg =conn.prepareStatement(sql1);
        PreparedStatement gp = conn.prepareStatement(sql);
        pg.setString(1,name);
        pg.setString(2,npassword);
        gp.setString(1, name);
        gp.setString(2, npassword);
        ResultSet re = pg.executeQuery();
        int up = gp.executeUpdate();
        while (up ==0 || re.next()){
            return false;
        }
        return true;
    }
}
JDBC封装类
package com.example.dao;
import java.sql.*;
import java.util.Objects;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
    public class JDBC {
        /**
         * URL地址
         */
        private static final String URL = "jdbc:mysql://127.0.0.1:3306/library?useSSL=false";
        /**
         * 登录数据库服务器的账号
         */
        private static final String USER = "root";
        /**
         * 登录数据库服务器的密码
         */
        private static final String PASSWORD = "*******";//换为自己的数据库密码
        /**
         * 返回数据库连接对象
         *
         * @return
         */
        public static Connection getConn() {
            try {
                return DriverManager.getConnection(URL, USER, PASSWORD);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return null;
        }
        /**
         * 关闭资源
         *
         * @param rs
         * @param stat
         * @param conn
         */
        public static void close(ResultSet rs, Statement stat, Connection conn) {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (stat != null) {
                    stat.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        /**
         * 封装通用的更新操作(即通过该方法实现对弈任意数据表的insert,update,delete操作)
         *
         * @param sql    需要执行的SQL语句
         * @param params 执行SQL语句时需要传递进去参数
         * @return 执行结果
         */
        public static boolean exeUpdate(String sql, Object... params) {
            //获取连接对象
            Connection conn = getConn();
            PreparedStatement ps = null;
            try {
                //获取预编译对象
                ps = conn.prepareStatement(sql);
                //执行参数赋值操作
                if (Objects.nonNull(params)) {
                    //循环将所有的参数赋值
                    for (int i = 0; i  0;
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                //关闭资源
                close(null, ps, conn);
            }
            return false;
        }
    }
FXML文件
登录




   
      
         
            
         
      
      
      
         
            
         
      
      
         
            
         
      
      
         
            
         
      
      
      
      
   

登陆失败




   
      
         
            
         
      
      
         
            
         
   

注册




   
      
         
            
         
      
      
         
            
         
      
         
            
         
      
         
            
         
      
      
      
      
      
   

注册失败




    
        
            
                
            
        
        
            
                
            
    

管理员登陆





   
      
         
            
         
      
         
            
         
      
      
         
            
         
      
      
      
      
         
            
         
      
      
      
   

图书馆



   
      
         
            
         
      
      
      
      
      
      
      
      
      
        
          
          
            
        
      
      
   

搜索





   
      
         
            
         
      
         
            
            
            
         
      
      
         
            
         
      
      
      
      
      
      
         
            
         
      
         
            
         
      
   

借书成功




    
        
            
                
            
        
        
            
                
            
    

借书失败




    
        
            
                
            
        
        
            
                
            
      
         
            
         
      
    

还书成功
        
        
        
        

    
        
            
        
    
    
        
            
        


还书失败




    
        
            
                
            
        
        
            
                
            
    
管理员后台




   
      
         
            
         
      
      
        
          
            
          
            
            
        
      
      
      
         
            
         
      
      
      
      
      
   

查询借阅记录



   
      
         
            
         
      
        
          
            
        
      
      
   

修改图书




   
      
         
            
         
      
      
      
         
            
         
      
         
            
         
      
      
      
         
            
         
      
      
      
      
         
            
         
      
      
         
            
         
      
   

增加图书




   
      
         
            
         
      
      
         
            
         
      
      
         
            
         
      
      
         
            
         
      
      
      
      
      
      
         
            
         
      
      
   

增加成功




   
      
         
            
         
      
         
            
         
      
      
      
   


OK写完了,里面还有很多小瑕疵,修改图书时不能把图书修改的跟数据库现存的一样,以及修改图书还是要填写数字。欢迎大家来评论区探讨!!

VPS购买请点击我

文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

目录[+]