Java课程设计之图书管理系统

基于Java swing+jdbc+mysql实现的图书管理系统

一、前言

在暑假期间,我做了一个基于Java swing+mysql+jdbc的学生管理系统并且还发布到了csdn供大家参考和学习一下。最近这个学期初呢,上了Java课程设计的可成,这次我刚好做了一个Java +swing+mysql+jdbc的图书管理系统。刚好趁着最近有空,想把这个系统和大家分享出来,供大家学习和参考。

二、设计需求和项目介绍

2.1设计需求

1.利用jdbc将java程序与mysql建立连接

2.利用java swing将学生信息、图书信息和借书信息显现出来

2.2项目介绍

这是一个基于java swing+jdbc+mysql的图书管理系统,该系统被分为了两个角色,分别是读者(学生)角色和管理员角色。在使用前,使用者需要注册和登陆对应的角色,才能使用对应的功能。读者只能使用借书、还书和查阅借阅信息的功能,而管理员在此基础上还添加了对读者(学生)信息和图书信息的更新和查询操作。而这些界面的和数据的显示是使用java swing实现的,而对于学生信息、图书信息以及借书信息的更新和查询则通过jdbc将java程序和数据库进行连接,并执行相应对应的SQL语句所实现的。

三、图书管理系统的具体设计

3.1数据库库表介绍详细

在这个图书管理系统中,一共有三个表,分别是学生信息表(stu),图书信息表(books)和借阅信息表(borrowrecord)。其中借阅信息表的stu_id字段和stu学生信息表的id字段进行了外键约束,同时借阅信息表的book_id字段和books图书信息表的books_id字段也进行了外键约束。这样方便我们查看借阅表时知晓具体是谁借了哪一本书。

3.1.1各个表的具体结构

学生表结构(stu):

图书信息表结构(books):

借阅表结构(borrowrecord):

值得注意的是,我在创建借阅表时,将stu_id和book_id联合设为了该表的主键,这是为了保证学生在还完书之后,才能继续借阅刚还完的书,也就是说不能同时借阅两本相同的书。另外,我还在为此表添加了级联删除和级联更新的操作,当学生表或者图书表的信息发生变更或者被删除,此表所关联的数据也会相应地变更或被删除。

3.2学生管理模块

3.2.1学生管理界面

学生管理界面,继承JFrame类,是各个学生信息操作模块的入口和展示类

package view.Management;

import view.Management.StuManageView.StuDelete;
import view.Management.StuManageView.StuInsert;
import view.Management.StuManageView.StuQuery;
import view.Management.StuManageView.StuUpdate;

import javax.swing.*;
import java.awt.*;

public class StuManage extends JFrame {
    JButton stuinsert=new JButton("增加读者");
    JButton studelete=new JButton("删除读者");
    JButton stuupdate=new JButton("修改读者信息");
    JButton stuquery=new JButton("查询读者信息");
    JButton cancel=new JButton("取消");
    public StuManage(){
        this.setTitle("学生信息管理");
        this.setSize(800,600);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(new GridBagLayout());
        GridBagConstraints constraintsleft=new GridBagConstraints();
        constraintsleft.gridx = 0; // 列为0
        constraintsleft.gridy = GridBagConstraints.RELATIVE; // 按钮的行相对于前一个按钮的行
        constraintsleft.anchor = GridBagConstraints.CENTER; // 设置水平方向上的位置居中
        constraintsleft.fill = GridBagConstraints.HORIZONTAL; // 填充水平方向
        /*constraintsleft.weightx = 0.5; // 水平方向上的权重为0.5*/
        constraintsleft.insets = new Insets(10, 5, 5, 100); // 设置按钮的内边距
        constraintsleft.ipadx = 10;
        this.add(stuinsert,constraintsleft);
        this.add(studelete,constraintsleft);
        this.add(stuupdate,constraintsleft);
        this.add(stuquery,constraintsleft);
        this.add(cancel,constraintsleft);
        stuinsert.addActionListener(e->{
            if(e.getSource()==stuinsert){
                StuInsert stuInsert=new StuInsert();
                stuInsert.setVisible(true);
            }
        });
        studelete.addActionListener(e->{
            if(e.getSource()==studelete){
                StuDelete stuDelete=new StuDelete();
                stuDelete.setVisible(true);
            }
        });
        stuupdate.addActionListener(e->{
            if(e.getSource()==stuupdate){
                StuUpdate stuUpdate=new StuUpdate();
                stuUpdate.setVisible(true);
            }
        });
        stuquery.addActionListener(e->{
            if(e.getSource()==stuquery){
                StuQuery stuQuery=new StuQuery();
                stuQuery.setVisible(true);
            }
        });
        cancel.addActionListener(e->{
            if(e.getSource()==cancel){
                dispose();
            }
        });
    }
}
3.2.2添加学生

管理员通过输入框可以添加学生信息

package view.Management.StuManageView;

import SQLOperation.StuSQLOperation.StuOperation;

import javax.swing.*;
import java.awt.*;

public class StuInsert extends JFrame{
    JPanel p1=new JPanel();
    JPanel p2=new JPanel();
    JPanel p3=new JPanel();
    JPanel p4=new JPanel();
    JLabel insertname=new JLabel("请输入你要增加读者的名字");
    JLabel insertgender=new JLabel("请输入你要增加读者的性别");
    JLabel insertid=new JLabel("请输入你要增加读者的学号");
    JTextField fieldname=new JTextField(10);
    JTextField fieldgender=new JTextField(10);
    JTextField fieldid=new JTextField(10);
    JButton confirm=new JButton("确认");
    JButton cancel=new JButton("取消");
    public StuInsert(){
        this.setTitle("增加读者");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(800,600);
        this.setLayout(new GridLayout(4,1));
        p1.add(insertname);
        p1.add(fieldname);
        p2.add(insertgender);
        p2.add(fieldgender);
        p3.add(insertid);
        p3.add(fieldid);
        p4.add(confirm);
        p4.add(cancel);
        this.add(p1);
        this.add(p2);
        this.add(p3);
        this.add(p4);
        confirm.addActionListener(e->{
            if(e.getSource()==confirm){
                try{
                    StuOperation.stuinsert(fieldname.getText(),fieldgender.getText(),fieldid.getText());
                }
                catch(Exception ex){
                    JOptionPane.showMessageDialog(null,"添加失败!","警告",JOptionPane.INFORMATION_MESSAGE);
                }
                dispose();
            }
        });
        cancel.addActionListener(e->{
            if(e.getSource()==cancel){
                dispose();
            }
        });
    }
}
3.2.3查询学生

管理员通过输入框输入学号来查找学生信息

package view.Management.StuManageView;

import SQLOperation.StuSQLOperation.StuOperation;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;

public class StuQuery extends JFrame {
    DefaultTableModel model=new DefaultTableModel();
    JTable stutable=new JTable(model);
    String[] colunmnname={"姓名","性别","学号"};
    JScrollPane jScrollPane=new JScrollPane(stutable);
    JButton cancel=new JButton("取消");
    JPanel p1=new JPanel();
    JPanel p2=new JPanel();
    public StuQuery(){
        this.setSize(800,600);
        this.setLayout(new GridLayout(2,1));
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("学生信息查询");
        p1.add(jScrollPane);
        p2.add(cancel);
        this.add(p1);
        this.add(p2);
        model.setColumnIdentifiers(colunmnname);
        try{
            StuOperation.stuquery(model);
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null,"查询失败!","警告",JOptionPane.WARNING_MESSAGE);
        }
        cancel.addActionListener(e->{
            if(e.getSource()==cancel){
                dispose();
            }
        });
    }
}
3.2.4删除学生

管理员通过输入框可以删除学生系想你

package view.Management.StuManageView;

import SQLOperation.StuSQLOperation.StuOperation;

import javax.swing.*;
import java.awt.*;

public class StuDelete extends JFrame {
    JPanel p1=new JPanel();
    JLabel emptylabel=new JLabel();
    JLabel deleteid=new JLabel("请输入你要删除读者的学号");
    JTextField fieldid=new JTextField(10);
    JButton confirm=new JButton("确认");
    JButton cancel=new JButton("取消");
    public StuDelete(){
        this.setTitle("读者信息删除");
        this.setSize(750,500);
        this.setLayout(new GridLayout(2,1));
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        emptylabel.setPreferredSize(new Dimension(10,300));
        p1.add(emptylabel);
        p1.add(deleteid);
        p1.add(fieldid);
        p1.add(confirm);
        p1.add(cancel);
        this.add(p1);
        confirm.addActionListener(e->{
            if(e.getSource()==confirm){
                try{
                    StuOperation.studelete(fieldid.getText());
                }
                catch(Exception ex){
                    JOptionPane.showMessageDialog(null,"删除失败!","警告",JOptionPane.WARNING_MESSAGE);
                }
                dispose();
            }
        });
        cancel.addActionListener(e->{
            if(e.getSource()==cancel){
                dispose();
            }
        });
    }
}
3.2.5更新学生

管理员通过输入框可以修改学生信息

package view.Management.StuManageView;

import SQLOperation.StuSQLOperation.StuOperation;

import javax.swing.*;
import java.awt.*;

public class StuUpdate extends JFrame {
    JLabel oldid=new JLabel("请输入学号");
    JTextField fieldoldid=new JTextField(10);
    JLabel updatename=new JLabel("请输入修改后的名字");
    JTextField fieldname=new JTextField(10);
    JLabel updategender=new JLabel("请输入修改后的性别");
    JTextField fieldgender=new JTextField(10);
    JLabel updateid=new JLabel("请输入修改后的学号");
    JTextField fieldid=new JTextField(10);
    JPanel p1=new JPanel();
    JPanel p2=new JPanel();
    JPanel p3=new JPanel();
    JPanel p4=new JPanel();
    JPanel p5=new JPanel();
    JButton confirm=new JButton("确认");
    JButton cancel=new JButton("取消");
    public StuUpdate(){
        this.setTitle("读者修改信息");
        this.setSize(800,600);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(new GridLayout(5,1));
        p1.add(oldid);
        p1.add(fieldoldid);
        p2.add(updatename);
        p2.add(fieldname);
        p3.add(updategender);
        p3.add(fieldgender);
        p4.add(updateid);
        p4.add(fieldid);
        p5.add(confirm);
        p5.add(cancel);
        this.add(p1);
        this.add(p2);
        this.add(p3);
        this.add(p4);
        this.add(p5);
        confirm.addActionListener(e->{
            if(e.getSource()==confirm){
                try{
                    StuOperation.stuupdate(fieldoldid.getText(),fieldname.getText(),fieldgender.getText(),fieldid.getText());
                }
                catch(Exception ex){
                    JOptionPane.showMessageDialog(null,"修改失败!","警告",JFrame.EXIT_ON_CLOSE);
                }
                dispose();
            }
        });
        cancel.addActionListener(e->{
            if(e.getSource()==cancel){
                dispose();
            }
        });
    }
}

3.3图书管理模块

3.3.1图书管理界面

图书管理界面,继承JFrame类,是各个图书信息操作模块的入口和展示类

package view.Management;

import view.Management.BookManageView.BookDelete;
import view.Management.BookManageView.BookInsert;
import view.Management.BookManageView.BookQuery;
import view.Management.BookManageView.BookUpdate;

import javax.swing.*;
import java.awt.*;

public class BookManage extends JFrame {
    JButton bookinsert=new JButton("增加图书");
    JButton bookdelete=new JButton("删除图书");
    JButton bookupdate=new JButton("修改图书信息");
    JButton bookquery=new JButton("查询图书信息");
    JButton cancel=new JButton("取消");
    public BookManage(){
        this.setTitle("学生信息管理");
        this.setSize(800,600);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(new GridBagLayout());
        GridBagConstraints constraintsleft=new GridBagConstraints();
        constraintsleft.gridx = 0; // 列为0
        constraintsleft.gridy = GridBagConstraints.RELATIVE; // 按钮的行相对于前一个按钮的行
        constraintsleft.anchor = GridBagConstraints.CENTER; // 设置水平方向上的位置居中
        constraintsleft.fill = GridBagConstraints.HORIZONTAL; // 填充水平方向
        /*constraintsleft.weightx = 0.5; // 水平方向上的权重为0.5*/
        constraintsleft.insets = new Insets(10, 5, 5, 100); // 设置按钮的内边距
        constraintsleft.ipadx = 10;
        this.add(bookinsert,constraintsleft);
        this.add(bookdelete,constraintsleft);
        this.add(bookupdate,constraintsleft);
        this.add(bookquery,constraintsleft);
        this.add(cancel,constraintsleft);
        bookinsert.addActionListener(e->{
            if(e.getSource()==bookinsert){
                BookInsert bookInsert=new BookInsert();
                bookInsert.setVisible(true);
            }
        });
        bookdelete.addActionListener(e->{
            if(e.getSource()==bookdelete){
                BookDelete bookDelete=new BookDelete();
                bookDelete.setVisible(true);
            }
        });
        bookupdate.addActionListener(e->{
            if(e.getSource()==bookupdate){
                BookUpdate bookUpdate=new BookUpdate();
                bookUpdate.setVisible(true);
            }
        });
        bookquery.addActionListener(e->{
            if(e.getSource()==bookquery){
                BookQuery bookQuery=new BookQuery();
                bookQuery.setVisible(true);
            }
        });
        cancel.addActionListener(e->{
            if(e.getSource()==cancel){
                dispose();
            }
        });
    }
}
3.3.2添加图书

管理员通过输入框可以添加图书信息

package view.Management.BookManageView;

import SQLOperation.BookSQLOperation.BookOperation;

import javax.swing.*;
import java.awt.*;
public class BookInsert extends JFrame {
    JPanel p1=new JPanel();
    JPanel p2=new JPanel();
    JPanel p3=new JPanel();
    JPanel p4=new JPanel();
    JPanel p5=new JPanel();
    JPanel p6=new JPanel();
    JLabel insertbookname=new JLabel("请输入你要增加图书的名字");
    JLabel insertauthor=new JLabel("请输入你要增加图书的作者");
    JLabel insertpublisher=new JLabel("请输入你要增加图书的出版社");
    JLabel insertbooknum=new JLabel("请输入你要增加图书的编号");
    JTextField fieldbookname=new JTextField(10);
    JTextField fieldauthor=new JTextField(10);
    JTextField fieldpublisher=new JTextField(10);
    JTextField fieldbooknum=new JTextField(10);
    JLabel insertstock=new JLabel("请输入你要增加图书的数量");
    JTextField fieldstock=new JTextField(10);
    JButton confirm=new JButton("确认");
    JButton cancel=new JButton("取消");
    public BookInsert(){
        this.setTitle("增加图书信息");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(800,600);
        this.setLayout(new GridLayout(6,1));
        p1.add(insertbookname);
        p1.add(fieldbookname);
        p2.add(insertauthor);
        p2.add(fieldauthor);
        p3.add(insertpublisher);
        p3.add(fieldpublisher);
        p4.add(insertbooknum);
        p4.add(fieldbooknum);
        p5.add(insertstock);
        p5.add(fieldstock);
        p6.add(confirm);
        p6.add(cancel);
        this.add(p1);
        this.add(p2);
        this.add(p3);
        this.add(p4);
        this.add(p5);
        this.add(p5);
        this.add(p6);
        confirm.addActionListener(e->{
            if(e.getSource()==confirm){
                try{
                    BookOperation.bookinsert(fieldbookname.getText(),fieldauthor.getText(),fieldpublisher.getText(),fieldbooknum.getText(),Integer.parseInt(fieldstock.getText()));
                }
                catch(Exception ex){
                    JOptionPane.showMessageDialog(null,"添加失败!","警告",JOptionPane.INFORMATION_MESSAGE);
                }
                dispose();
            }
        });
        cancel.addActionListener(e->{
            if(e.getSource()==cancel){
                dispose();
            }
        });
    }
}
3.3.3查询图书

管理员通过输入框可以查询图书信息

package view.Management.BookManageView;

import SQLOperation.BookSQLOperation.BookOperation;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;

public class BookQuery extends JFrame {
    DefaultTableModel model=new DefaultTableModel();
    JTable booktable=new JTable(model);
    JScrollPane jScrollPane=new JScrollPane(booktable);
    String[] columnname={"书名","作者","出版社","图书编号","库存量"};
    JPanel p1=new JPanel();
    JPanel p2=new JPanel();
    JButton cancel=new JButton("取消");
    public BookQuery(){
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(800,600);
        this.setTitle("图书查询信息");
        this.setLayout(new GridLayout(2,1));
        p1.add(jScrollPane);
        p2.add(cancel);
        this.add(p1);
        this.add(p2);
        model.setColumnIdentifiers(columnname);
        try{
            BookOperation.bookquery(model);
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null,"查询失败!","警告",JOptionPane.WARNING_MESSAGE);
        }
        cancel.addActionListener(e->{
            if(e.getSource()==cancel){
                dispose();
            }
        });
    }
}
3.3.4删除图书

管理员通过输入框可以删除某个图书的信息

package view.Management.BookManageView;

import SQLOperation.BookSQLOperation.BookOperation;
import SQLOperation.StuSQLOperation.StuOperation;

import javax.swing.*;
import java.awt.*;

public class BookDelete extends JFrame {
    JPanel p1=new JPanel();
    JLabel emptylabel=new JLabel();
    JLabel deletenum=new JLabel("请输入你要删除图书的编号");
    JTextField fieldnum=new JTextField(10);
    JButton confirm=new JButton("确认");
    JButton cancel=new JButton("取消");
    public BookDelete(){
        this.setTitle("图书信息删除");
        this.setSize(750,500);
        this.setLayout(new GridLayout(2,1));
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        emptylabel.setPreferredSize(new Dimension(10,300));
        p1.add(emptylabel);
        p1.add(deletenum);
        p1.add(fieldnum);
        p1.add(confirm);
        p1.add(cancel);
        this.add(p1);
        confirm.addActionListener(e->{
            if(e.getSource()==confirm){
                try{
                    BookOperation.bookdelete(fieldnum.getText());
                }
                catch(Exception ex){
                    JOptionPane.showMessageDialog(null,"删除失败!","警告",JOptionPane.WARNING_MESSAGE);
                }
                dispose();
            }
        });
        cancel.addActionListener(e->{
            if(e.getSource()==cancel){
                dispose();
            }
        });
    }

}
3.3.5更新图书

管理员通过输入框可以对图书信息进行修改或更新

package view.Management.BookManageView;

import SQLOperation.BookSQLOperation.BookOperation;

import javax.swing.*;
import java.awt.*;

public class BookUpdate extends JFrame {
    JPanel p1=new JPanel();
    JPanel p2=new JPanel();
    JPanel p3=new JPanel();
    JPanel p4=new JPanel();
    JPanel p5=new JPanel();
    JPanel p6=new JPanel();
    JPanel p7=new JPanel();
    JLabel oldbooknum=new JLabel("请输入你想修改的图书编号");
    JTextField fieldoldbooknum=new JTextField(10);
    JLabel updatebookname=new JLabel("请输入修改后的书名");
    JTextField fieldbookname=new JTextField(10);
    JLabel updatebookauthor=new JLabel("请输入修改后的作者名");
    JTextField fieldbookauthor=new JTextField(10);
    JLabel updatebookpublisher=new JLabel("请输入修改后的出版社");
    JTextField fieldbookpublisher=new JTextField(10);
    JLabel updatebooknum=new JLabel("请输入修改后的图书编号");
    JTextField fieldbooknum=new JTextField(10);
    JLabel updatebookstock=new JLabel("请输入修改后的图书数量");
    JTextField fieldbookstock=new JTextField(10);
    JButton confirm=new JButton("确认");
    JButton cancel=new JButton("取消");
    public BookUpdate(){
        this.setTitle("图书修改信息");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(800,600);
        this.setLayout(new GridLayout(7,1));
        p1.add(oldbooknum);
        p1.add(fieldoldbooknum);
        p2.add(updatebookname);
        p2.add(fieldbookname);
        p3.add(updatebookauthor);
        p3.add(fieldbookauthor);
        p4.add(updatebookpublisher);
        p4.add(fieldbookpublisher);
        p5.add(updatebooknum);
        p5.add(fieldbooknum);
        p6.add(updatebookstock);
        p6.add(fieldbookstock);
        p7.add(confirm);
        p7.add(cancel);
        this.add(p1);
        this.add(p2);
        this.add(p3);
        this.add(p4);
        this.add(p5);
        this.add(p6);
        this.add(p7);
        confirm.addActionListener(e->{
            if(e.getSource()==confirm){
                try{
                    BookOperation.bookupdate(fieldoldbooknum.getText(),fieldbookname.getText(),fieldbookauthor.getText(),fieldbookpublisher.getText(),fieldbooknum.getText(),Integer.parseInt(fieldbookstock.getText()));
                }
                catch(Exception ex){
                    JOptionPane.showMessageDialog(null,"修改失败!","警告",JOptionPane.WARNING_MESSAGE);
                }
                dispose();
            }
        });
        cancel.addActionListener(e->{
            if(e.getSource()==cancel){
                dispose();
            }
        });
    }
}

3.4借书模块

3.4.1借书管理界面

借书管理界面,继承JFrame类,是借书、还书和查找借书记录操作模块的入口和展示类

package view.Management;

import view.Management.BorrowManageView.BorrowDelete;
import view.Management.BorrowManageView.BorrowInsert;
import view.Management.BorrowManageView.BorrowQuery;

import javax.swing.*;
import java.awt.*;

public class BorrowManage extends JFrame {
    JButton borrowinsert=new JButton("借书");
    JButton borrowdelete=new JButton("还书");
    JButton borrowquery=new JButton("查询借书信息");
    JButton cancel=new JButton("取消");
    public BorrowManage(){
        this.setSize(800,600);
        this.setTitle("借阅管理");
        this.setLayout(new GridBagLayout());
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GridBagConstraints constraintsleft=new GridBagConstraints();
        constraintsleft.gridx = 0; // 列为0
        constraintsleft.gridy = GridBagConstraints.RELATIVE; // 按钮的行相对于前一个按钮的行
        constraintsleft.anchor = GridBagConstraints.CENTER; // 设置水平方向上的位置居中
        constraintsleft.fill = GridBagConstraints.HORIZONTAL; // 填充水平方向
        /*constraintsleft.weightx = 0.5; // 水平方向上的权重为0.5*/
        constraintsleft.insets = new Insets(10, 5, 5, 100); // 设置按钮的内边距
        constraintsleft.ipadx = 10;
        this.add(borrowinsert,constraintsleft);
        this.add(borrowdelete,constraintsleft);
        this.add(borrowquery,constraintsleft);
        this.add(cancel,constraintsleft);
        borrowinsert.addActionListener(e->{
            if(e.getSource()==borrowinsert){
                BorrowInsert borrowInsert=new BorrowInsert();
                borrowInsert.setVisible(true);
            }
        });
        borrowdelete.addActionListener(e->{
            if(e.getSource()==borrowdelete){
                BorrowDelete borrowDelete=new BorrowDelete();
                borrowDelete.setVisible(true);
            }
        });
        borrowquery.addActionListener(e->{
            if(e.getSource()==borrowquery){
                BorrowQuery borrowQuery=new BorrowQuery();
                borrowQuery.setVisible(true);
            }
        });
        cancel.addActionListener(e->{
            if(e.getSource()==cancel){
                dispose();
            }
        });
    }
}
3.4.2借书

读者通过输入框可以借书

package view.Management.BorrowManageView;

import SQLOperation.BorrowOperation.BorrowOperation;
import javafx.util.converter.LocalDateStringConverter;

import javax.swing.*;
import java.awt.*;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class BorrowInsert extends JFrame {
    JLabel borrowid=new JLabel("请输入你的学号");
    JTextField fieldid=new JTextField(10);
    JLabel borrowbooknum=new JLabel("请输入你要借书的编号");
    JTextField fieldbooknum=new JTextField(10);
    JLabel returndate=new JLabel("请输入还书的日期");
    JTextField fieldreturndate=new JTextField(10);
    JPanel p1=new JPanel();
    JPanel p2=new JPanel();
    JPanel p3=new JPanel();
    JPanel p4=new JPanel();
    JButton confirm=new JButton("确定");
    JButton cancel=new JButton("取消");
    public BorrowInsert(){
        this.setTitle("借书");
        this.setLayout(new GridLayout(4,1));
        this.setSize(800,600);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        p1.add(borrowid);
        p1.add(fieldid);
        p2.add(borrowbooknum);
        p2.add(fieldbooknum);
        p3.add(returndate);
        p3.add(fieldreturndate);
        p4.add(confirm);
        p4.add(cancel);
        this.add(p1);
        this.add(p2);
        this.add(p3);
        this.add(p4);
        LocalDate currenttime=LocalDate.now();
        DateTimeFormatter formatter=DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String borrowdate=currenttime.format(formatter);
        confirm.addActionListener(e->{
            if(e.getSource()==confirm){
                try{
                    BorrowOperation.borrowinsert(fieldid.getText(),fieldbooknum.getText(),borrowdate,fieldreturndate.getText());
                }
                catch(Exception ex){
                    JOptionPane.showMessageDialog(null,"借书失败!请确保你的学号和图书编号正确且存在!或者你已经借了这本书!","警告",JOptionPane.WARNING_MESSAGE);
                }
                dispose();
            }
        });
        cancel.addActionListener(e->{
            if(e.getSource()==cancel){
                dispose();
            }
        });
    }
}
3.4.3还书

读者通过输入框可以还书

package view.Management.BorrowManageView;

import SQLOperation.BorrowOperation.BorrowOperation;

import javax.swing.*;
import java.awt.*;

public class BorrowDelete extends JFrame {
    JLabel borrowid=new JLabel("请输入你的学号");
    JTextField fieldid=new JTextField(10);
    JLabel returnbooknum=new JLabel("请输入你要还书的编号");
    JTextField fieldreturnnum=new JTextField(10);
    JPanel p1=new JPanel();
    JPanel p2=new JPanel();
    JPanel p3=new JPanel();
    JButton confirm=new JButton("确定");
    JButton cancel=new JButton("取消");
    public BorrowDelete(){
        this.setTitle("还书");
        this.setSize(800,600);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(new GridLayout(3,1));
        p1.add(borrowid);
        p1.add(fieldid);
        p2.add(returnbooknum);
        p2.add(fieldreturnnum);
        p3.add(confirm);
        p3.add(cancel);
        this.add(p1);
        this.add(p2);
        this.add(p3);
        confirm.addActionListener(e->{
            if(e.getSource()==confirm){
                try{
                    BorrowOperation.borrowdelete(fieldid.getText(),fieldreturnnum.getText());
                }
                catch(Exception ex){
                    JOptionPane.showMessageDialog(null,"还书失败!请确保你的学号和图书编号正确且存在!","警告",JOptionPane.WARNING_MESSAGE);
                }
                dispose();
            }
        });
        cancel.addActionListener(e->{
            if(e.getSource()==cancel){
                dispose();
            }
        });
    }
}
3.4.4查询借书记录

读者通过输入框可以查询借书记录

package view.Management.BorrowManageView;

import SQLOperation.BorrowOperation.BorrowOperation;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;

public class BorrowQuery extends JFrame {
    DefaultTableModel model=new DefaultTableModel();
    JTable borrowtable=new JTable(model);
    JScrollPane jScrollPane=new JScrollPane(borrowtable);
    String[] columnname={"学号","图书编号","借书日期","还书日期"};
    JPanel p1=new JPanel();
    JPanel p2=new JPanel();
    JButton cancel=new JButton("取消");
    public BorrowQuery(){
        this.setSize(800,600);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("借阅信息查询");
        this.setLayout(new GridLayout(2,1));
        this.add(jScrollPane);
        model.setColumnIdentifiers(columnname);
        p1.add(jScrollPane);
        p2.add(cancel);
        this.add(p1);
        this.add(p2);
        try{
            BorrowOperation.borrowquery(model);
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null,"查询失败!","警告",JOptionPane.WARNING_MESSAGE);
        }
        cancel.addActionListener(e->{
            if(e.getSource()==cancel){
                dispose();
            }
        });
    }
}

3.5程序主界面

程序的主界面,是各个子管理模块的入口和展示类,同时还附有用户和管理员两个角色的登录、注册功能

package view;

import view.Management.BookManage;
import view.Management.BorrowManage;
import view.Management.StuManage;

import javax.swing.*;
import java.awt.*;
import java.util.HashMap;

public class MainView extends JFrame {
    private HashMap<String,String> adminhashMap=new HashMap<>();
    private HashMap<String,String> readerhashMap=new HashMap<>();
    JLabel welcome=new JLabel("欢迎来到图书管理系统");
    JLabel username=new JLabel("用户名");
    JLabel password=new JLabel("密码");
    JPanel p1=new JPanel();
    JPanel p2=new JPanel();
    JPanel p3=new JPanel();
    JTextField usernamefield=new JTextField(10);
    JPasswordField passwordfield=new JPasswordField(10);
    JButton exit=new JButton("登出");
    JButton adminregister=new JButton("管理员注册");
    JButton readerregister=new JButton("读者注册");
    JButton stumanage=new JButton("读者管理");
    JButton bookmanage=new JButton("书库管理");
    JButton borrowmanage=new JButton("借阅管理");
    public MainView() throws HeadlessException {
        this.setTitle("图书管理系统");
        this.setSize(800,600);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(new GridBagLayout());
        this.setVisible(true);
        GridBagConstraints constraintsleft=new GridBagConstraints();
        constraintsleft.gridx = 0; // 列为0
        constraintsleft.gridy = GridBagConstraints.RELATIVE; // 按钮的行相对于前一个按钮的行
        constraintsleft.anchor = GridBagConstraints.CENTER; // 设置水平方向上的位置居中
        constraintsleft.fill = GridBagConstraints.HORIZONTAL; // 填充水平方向
        /*constraintsleft.weightx = 0.5; // 水平方向上的权重为0.5*/
        constraintsleft.insets = new Insets(10, 5, 5, 100); // 设置按钮的内边距
        constraintsleft.ipadx = 10;

        GridBagConstraints constraintsright=new GridBagConstraints();
        constraintsright.gridx=1;
        constraintsright.gridy = GridBagConstraints.RELATIVE; // 按钮的行相对于前一个按钮的行
        constraintsright.anchor = GridBagConstraints.CENTER; // 设置水平方向上的位置居中
        constraintsright.fill = GridBagConstraints.HORIZONTAL; // 填充水平方向
        /*constraintsleft.weightx = 0.5; // 水平方向上的权重为0.5*/
        constraintsright.insets = new Insets(10, 100, 5, 5); // 设置按钮的内边距
        constraintsright.ipadx = 10;

        this.add(stumanage,constraintsleft);
        this.add(bookmanage,constraintsleft);
        this.add(borrowmanage,constraintsleft);

        this.add(welcome,constraintsright);
        p1.add(username);
        p1.add(usernamefield);
        p2.add(password);
        p2.add(passwordfield);
        p3.add(readerregister);
        p3.add(adminregister);
        this.add(p1,constraintsright);
        this.add(p2,constraintsright);
        this.add(p3,constraintsright);
        this.add(exit,constraintsright);
        exit.addActionListener(e -> {
            if(e.getSource()==exit){
                dispose();
            }
        });
        readerregister.addActionListener(e->{
            if(e.getSource()==readerregister){
                if(readerhashMap.containsKey(usernamefield.getText())&&readerhashMap.get(usernamefield.getText()).equals(passwordfield.getText())){
                    JOptionPane.showMessageDialog(null,"该账户已存在!","警告",JOptionPane.WARNING_MESSAGE);
                }
                else{
                    readerhashMap.put(usernamefield.getText(),passwordfield.getText());
                    JOptionPane.showMessageDialog(null,"注册成功!","通知",JOptionPane.INFORMATION_MESSAGE);
                }
            }
        });
        adminregister.addActionListener(e -> {
            if(e.getSource()==adminregister){
                if(adminhashMap.containsKey(usernamefield.getText())&&adminhashMap.get(usernamefield.getText()).equals(passwordfield.getText())){
                    JOptionPane.showMessageDialog(null,"该账户已存在!","警告",JOptionPane.WARNING_MESSAGE);
                }
                else{
                    adminhashMap.put(usernamefield.getText(),passwordfield.getText());
                    JOptionPane.showMessageDialog(null,"注册成功!","通知",JOptionPane.INFORMATION_MESSAGE);
                }
            }
        });
        stumanage.addActionListener(e->{
            if(!adminhashMap.containsKey(usernamefield.getText())){
                JOptionPane.showMessageDialog(null,"管理员用户名不存在,请注册!","警告",JOptionPane.WARNING_MESSAGE);
            }
            else{
                if(adminhashMap.get(usernamefield.getText()).equals(passwordfield.getText())){
                    JOptionPane.showMessageDialog(null,"登陆成功!","通知",JOptionPane.INFORMATION_MESSAGE);
                    StuManage stuManage=new StuManage();
                    stuManage.setVisible(true);
                }
                else
                    JOptionPane.showMessageDialog(null,"密码错误,请重新输入!","警告",JOptionPane.WARNING_MESSAGE);
            }
        });
        bookmanage.addActionListener(e->{
            if(!adminhashMap.containsKey(usernamefield.getText())){
                JOptionPane.showMessageDialog(null,"管理员用户名不存在,请注册!","警告",JOptionPane.WARNING_MESSAGE);
            }
            else{
                if(adminhashMap.get(usernamefield.getText()).equals(passwordfield.getText())){
                    JOptionPane.showMessageDialog(null,"登陆成功!","通知",JOptionPane.INFORMATION_MESSAGE);
                    BookManage bookManage=new BookManage();
                    bookManage.setVisible(true);
                }
                else
                    JOptionPane.showMessageDialog(null,"密码错误,请重新输入!","警告",JOptionPane.WARNING_MESSAGE);
            }
        });
        borrowmanage.addActionListener(e->{
            if(!readerhashMap.containsKey(usernamefield.getText())){
                JOptionPane.showMessageDialog(null,"读者用户名不存在,请注册!","警告",JOptionPane.WARNING_MESSAGE);
            }
            else{
                if(readerhashMap.get(usernamefield.getText()).equals(passwordfield.getText())){
                    JOptionPane.showMessageDialog(null,"登陆成功!","通知",JOptionPane.INFORMATION_MESSAGE);
                    BorrowManage borrowManage=new BorrowManage();
                    borrowManage.setVisible(true);
                }
                else
                    JOptionPane.showMessageDialog(null,"密码错误,请重新输入!","警告",JOptionPane.WARNING_MESSAGE);
            }
        });
    }
    public static void main(String[] args) {
        MainView mainView=new MainView();
    }
}

值得注意的是,程序的主界面不仅是各个模块的进入入口,它还包括了包括了读者和管理员的登陆和注册功能。在本次的图书管理系统中,分为了两个角色,分别是读者即学生角色和管理员角色。其中,读者只能使用借书、还书和查询图书借阅信息的功能,而管理员相较于前者则多了对读者(学生)的信息和图书信息的增删改查的功能。大家也可以多加一个超级管理员的角色,负责对管理员的信息进行管理和操作,而这个超级管理员最好就只有一个。对于不同角色登陆和注册的功能实现,我是通过hashmap来实现的,账号和密码正好对应着hashmap的键值。注册就往其中put键值对,登录就验证用户输入的账号密码是否存在于hashmap中就好了。

3.6Java程序连接数据库的工具类(JDBC)

package utils;

import java.sql.*;

public class Utils {
    private static final String Drivername="com.mysql.cj.jdbc.Driver";
    private static final String url="jdbc:mysql://localhost:3306/librarymanagement?characterEncoding=utf8";
    private static final String username="root";
    private static final String password="123456";
    static {
        try{
            Class.forName(Drivername);
        }
        catch(ClassNotFoundException e){
            e.printStackTrace();
        }
    }
    public static Connection getConnection(){
        Connection connection=null;
        try{
            connection=DriverManager.getConnection(url,username,password);
        }
        catch(SQLException e){
            e.printStackTrace();
        }
        return connection;
    }
    public static void Close(Statement statement,Connection connection){
        Close(null,statement,connection);
    }
    public static void Close(ResultSet resultSet,Statement statement,Connection connection){
        try{
            if(resultSet!=null && !resultSet.isClosed()){
                resultSet.close();
            }
            if(statement!=null && !statement.isClosed()){
                statement.close();
            }
            if(connection!=null && !connection.isClosed()){
                connection.close();
            }
        }
        catch(Exception e){
            System.out.println("关闭数据库失败");
        }
    }
}

其实一个工具类,在其中封装好了两个静态方法getConnection()和Close()方法,它们分别可以帮助我们建立Java程序和数据库之间的连接,同时关闭与数据库的连接,减少代码的冗余,提高代码的复用性。

3.7各个模块对应功能的SQL语句操作
3.7.1学生管理模块的SQL语句操作

封装了对学生信息进行crud(增查改删)的SQL语句

package SQLOperation.StuSQLOperation;

import utils.Utils;

import javax.swing.table.DefaultTableModel;
import java.sql.*;

public class StuOperation {
    public static void stuinsert(String name,String gender,String id) throws Exception {
        String sql="insert into stu(name,gender,id) values(?,?,?)";
        Connection connection= Utils.getConnection();
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        preparedStatement.setString(1,name);
        preparedStatement.setString(2,gender);
        preparedStatement.setString(3,id);
        int i= preparedStatement.executeUpdate();
        Utils.Close(preparedStatement,connection);
    }
    public static void studelete(String id)throws Exception{
        String sql="delete from stu where id=?";
        Connection connection=Utils.getConnection();
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        preparedStatement.setString(1,id);
        int i= preparedStatement.executeUpdate();
        Utils.Close(preparedStatement,connection);
    }
    public static void stuupdate(String oldid,String name,String gender,String id)throws Exception{
        String sql="update stu set name=?,gender=?,id=? where id=?";
        Connection connection=Utils.getConnection();
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        preparedStatement.setString(1,name);
        preparedStatement.setString(2,gender);
        preparedStatement.setString(3,id);
        preparedStatement.setString(4,oldid);
        int i=preparedStatement.executeUpdate();
        Utils.Close(preparedStatement,connection);
    }
    public static void stuquery(DefaultTableModel model)throws Exception{
        String sql="select name,gender,id from stu";
        Connection connection=Utils.getConnection();
        Statement statement=connection.createStatement();
        ResultSet rs=statement.executeQuery(sql);
        while(rs.next()){
            String name=rs.getString("name");
            String gender=rs.getString("gender");
            String id=rs.getString("id");
            model.addRow(new Object[]{name,gender,id});
        }
        Utils.Close(rs,statement,connection);
    }
}
3.7.2图书管理模块的SQL语句操作

封装了对图书信息进行crud(增查改删)的SQL语句

package SQLOperation.BookSQLOperation;

import utils.Utils;
import javax.swing.table.DefaultTableModel;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class BookOperation {
    public static void bookinsert(String name,String author,String publisher,String num,int stock)throws Exception{
        String sql="insert into books(book_name,book_author,book_publisher,book_num,book_stock) values(?,?,?,?,?)";
        Connection connection= Utils.getConnection();
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        preparedStatement.setString(1,name);
        preparedStatement.setString(2,author);
        preparedStatement.setString(3,publisher);
        preparedStatement.setString(4,num);
        preparedStatement.setInt(5,stock);
        int i=preparedStatement.executeUpdate();
        Utils.Close(preparedStatement,connection);
    }
    public static void bookdelete(String num)throws Exception{
        String sql="delete from books where book_num=?";
        Connection connection=Utils.getConnection();
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        preparedStatement.setString(1,num);
        int i=preparedStatement.executeUpdate();
        Utils.Close(preparedStatement,connection);
    }
    public static void bookupdate(String oldnum,String name,String author,String publisher,String num,int stock)throws Exception{
        String sql="update books set book_name=?,book_author=?,book_publisher=?,book_num=?,book_stock=? where book_num=?";
        Connection connection=Utils.getConnection();
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        preparedStatement.setString(1,name);
        preparedStatement.setString(2,author);
        preparedStatement.setString(3,publisher);
        preparedStatement.setString(4,num);
        preparedStatement.setInt(5,stock);
        preparedStatement.setString(6,oldnum);
        int i=preparedStatement.executeUpdate();
        Utils.Close(preparedStatement,connection);
    }
    public static void bookquery(DefaultTableModel model)throws Exception{
        String sql="select book_name,book_author,book_publisher,book_num,book_stock from books";
        Connection connection=Utils.getConnection();
        Statement statement= connection.createStatement();
        ResultSet rs=statement.executeQuery(sql);
        while(rs.next()){
            String bookname=rs.getString("book_name");
            String bookauthor=rs.getString("book_author");
            String bookpublisher=rs.getString("book_publisher");
            String booknum=rs.getString("book_num");
            String bookstock=rs.getString("book_stock");
            model.addRow(new Object[]{bookname,bookauthor,bookpublisher,booknum,bookstock});
        }
        Utils.Close(rs,statement,connection);
    }
}
3.7.3借书模块的SQL语句操作

封装了对借书模块进行crud(增查改删)的SQL语句

package SQLOperation.BorrowOperation;

import utils.Utils;

import javax.swing.table.DefaultTableModel;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class BorrowOperation{
    public static void borrowinsert(String id,String booknum,String borrowdate,String returndate)throws Exception{
        String insertsql="insert into borrowrecord(stu_id,book_id,borrow_date,return_date) values(?,?,?,?)";
        String borrowbooksql="update books set book_stock=book_stock - 1 where book_num=?";
        Connection connection1= Utils.getConnection();
        PreparedStatement insertsqlpreparedStatement=connection1.prepareStatement(insertsql);
        insertsqlpreparedStatement.setString(1,id);
        insertsqlpreparedStatement.setString(2,booknum);
        insertsqlpreparedStatement.setString(3,borrowdate);
        insertsqlpreparedStatement.setString(4,returndate);
        Connection connection2=Utils.getConnection();
        PreparedStatement borrowbooksqlpreparedStatement=connection2.prepareStatement(borrowbooksql);
        borrowbooksqlpreparedStatement.setString(1,booknum);
        int i=insertsqlpreparedStatement.executeUpdate();
        int j=borrowbooksqlpreparedStatement.executeUpdate();
        Utils.Close(insertsqlpreparedStatement,connection1);
        Utils.Close(borrowbooksqlpreparedStatement,connection2);
    }
    public static void borrowdelete(String id,String booknum)throws Exception{
        String deletesql="delete from borrowrecord where stu_id=? and book_id=?";
        String returnbooksql="update books set book_stock=book_stock + 1 where book_num=?";
        Connection connection1= Utils.getConnection();
        PreparedStatement deletesqlpreparedStatement = connection1.prepareStatement(deletesql);
        deletesqlpreparedStatement.setString(1,id);
        deletesqlpreparedStatement.setString(2,booknum);
        Connection connection2=Utils.getConnection();
        PreparedStatement returnbooksqlpreparedStatement= connection2.prepareStatement(returnbooksql);
        returnbooksqlpreparedStatement.setString(1,booknum);
        int i=deletesqlpreparedStatement.executeUpdate();
        int j=returnbooksqlpreparedStatement.executeUpdate();
        Utils.Close(deletesqlpreparedStatement,connection1);
        Utils.Close(returnbooksqlpreparedStatement,connection2);
    }
    public static void borrowquery(DefaultTableModel model)throws Exception{
        String sql="select stu_id,book_id,borrow_date,return_date from borrowrecord";
        Connection connection=Utils.getConnection();
        Statement statement = connection.createStatement();
        ResultSet rs=statement.executeQuery(sql);
        while(rs.next()){
            String stuid=rs.getString("stu_id");
            String bookid=rs.getString("book_id");
            String borrowdate= rs.getString("borrow_date");
            String returndate=rs.getString("return_date");
            model.addRow(new Object[]{stuid,bookid,borrowdate,returndate});
        }
        Utils.Close(rs,statement,connection);
    }
}

四、总结和反思

4.1总结

本项目是我在本学期Java课程中编写的,全程代码都是我自己一个一个敲出来的,完全属于个人原创了。该项目实现了读者(学生)、管理员的登陆和注册功能。其中读者只能进行借书、还书和查询借书记录的操作,而管理员基于上述的操作上还添加了对学生信息和图书信息的更新和查询操作。方便添加和修改读者(学生)的个人信息,同时还能更新和修改图书的信息,以便于提高该图书管理系统的可用性和合理性。

4.2反思和建议

4.2.1对该项目的反思

由于Java课设课时紧张,导致该系统存在一些不足之处或者一些隐藏的bug,以及存在一些代码编写不规范的问题。首先,该系统应该添加一个超级管理员的角色以便于对管理员和学生信息、和图书信息、借阅信息进行一个总体的管理和约束。当然这个超级管理员一般只需要一个就足够了,这也是常规管理系统所必备的角色之一。第二,在本系统中存在一个非常不合常理的设计。就是当读者(学生)或者管理员根据某些特定的信息比如学生学号、图书序号进行信息的更新或者查询操作,往往因为没有展示出读者(学生)或者图书的详细信息而使得使用者无法合理便捷地进行数据的更新和查询。因此我们在重构这个系统中,最好在对各类数据进行更新或者查询单个信息的操作时能够添加一个展示全部信息的功能,使得使用者免除像我们开发者只能通过查看数据库才能对数据进行更新和查询。

4.2.2个人建议

对于像重构这个项目的朋友,我希望你最好能够提前了解和掌握有关Java swing、jdbc、mysql和Java基础的相关知识,当然你想利用本项目来学习和巩固这些知识也是可以的。如果你觉得写的还不错的话,麻烦你可以点个赞和关注再走吧。

4.3源代码链接地址

源代码链接地址:

LibraryManageMentSystem https://www.alipan.com/s/n1bu1SE958H

提取码: e63z 

如果链接已经失效,可以私信我免费获取源代码的链接。

4.4学生管理系统

如果对学生管理系统感兴趣的朋友可以阅读和了解一下我的上一篇博客:http://t.csdnimg.cn/ZcyLu

  • 11
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值