Java课程设计:基于Java+Swing+MySQL的图书管理系统(内附源码)

文章目录

一、项目介绍

图书管理系统是一个常见的软件项目,广泛应用于图书馆、学校、企业等需要管理图书资源的场景。该系统通常涵盖图书信息录入、查询、借阅、归还等核心功能,是实现图书资源高效管理的重要工具。

随着信息技术的快速发展,传统纸质图书管理方式已经难以满足现代化管理的需求。图书管理系统的数字化转型成为当前图书馆和相关行业的重要发展方向。通过开发和应用图书管理系统,可以实现图书资源的数字化管理,提高工作效率,增强用户体验。

二、项目展示

登录界面

首页

读者查询

借阅图书

图书查询

三、源码展示

登录界面实现

java 复制代码
public class LoginForm extends JFrame {


    private JComboBox comboBox;
    private JLabel title,usernamelab,passwordlab,select;
    private JTextField usernameField;
    private JPasswordField passwordField;
    private JButton submit,updatePwd,regist;
    private UserService service = new UserService();

    public LoginForm() {

        Container container = getContentPane();
        container.setLayout(null);


        submit=new JButton("登录");
        submit.setBounds(20,210,60,20);
        //登录监听
        submit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                loginByUserAndPwd(e);
            }
        });
        regist=new JButton("注册");
        regist.setBounds(90,210,60,20);
        //跳转到注册界面
        regist.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new RegistForm();
            }
        });

        updatePwd=new JButton("修改密码");
        updatePwd.setBounds(160,210,100,20);
        //更新密码
        updatePwd.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new UpdatePwdForm();
            }
        });


        title=new JLabel("图书管理系统");
        title.setFont(new Font("宋体", Font.PLAIN, 24));
        title.setBounds(70,30,200,25);
        usernamelab=new JLabel("用户名:");
        usernamelab.setFont(new Font("宋体", Font.PLAIN, 16));
        usernamelab.setBounds(50,80,60,20);
        passwordlab=new JLabel("密码:");
        passwordlab.setFont(new Font("宋体", Font.PLAIN, 16));
        passwordlab.setBounds(50,120,60,20);

        usernameField=new JTextField();
        usernameField.setBounds(120,80,130,20);
        passwordField=new JPasswordField();
        passwordField.setEchoChar('*');
        passwordField.setBounds(120,120,130,20);


        container.add(title);
        container.add(usernamelab);container.add(usernameField);
        container.add(passwordlab);container.add(passwordField);
        container.add(submit);container.add(regist);container.add(updatePwd);


        //container.setBackground(Color.RED);
        setTitle("登录");
        setSize(300,300);
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    private void loginByUserAndPwd(ActionEvent e) {
        String username = this.usernameField.getText();
        String password = new String(this.passwordField.getPassword());
        String is_admin="";

        if(StringUtil.isEmpty(username)||StringUtil.isEmpty(password)){
            JOptionPane.showMessageDialog(null,"用户名或密码不能为空");
        }else {
                is_admin="1";//管理员
                User user = service.login(username, password, is_admin);
                if (user!=null){
                    JOptionPane.showMessageDialog(null,"登录成功");
                    dispose();
                    new RootMainForm();
                }else {
                    JOptionPane.showMessageDialog(null,"账号或面错误");
                }
            }
        }

    }

添加图书界面实现

java 复制代码
public class AddBookForm extends JFrame {

    private JLabel bookId,bookName,bookType,translator,publishTime,stock,price,publisher,author;
    private JTextField bookIdField,bookNameField,translatorField,stockField,priceField,publisherField,authorField;
    private JButton btn_Add,btn_Cancel;
    private  JComboBox<String> comboBox;
    final JXDatePicker datepick = new JXDatePicker();
     

    public AddBookForm(){
        Container container = getContentPane();
        container.setLayout(null);

        btn_Add=new JButton("保存");
        btn_Add.setBounds(190,310,80,20);
        btn_Cancel=new JButton("取消");
        btn_Cancel.setBounds(320,310,80,20);
        //取消按钮监听
        btn_Cancel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        //添加按钮监听
        btn_Add.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    addBook(e);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });

        bookId=new JLabel("图书编号");
        bookId.setFont(new Font("宋体", Font.PLAIN, 16));
        bookId.setBounds(50,50,100,20);
        bookType=new JLabel("图书类型");
        bookType.setFont(new Font("宋体", Font.PLAIN, 16));
        bookType.setBounds(50,90,100,20);
        translator=new JLabel("译者");
        translator.setFont(new Font("宋体", Font.PLAIN, 16));
        translator.setBounds(50,130,100,20);
        publishTime=new JLabel("出版时间");
        publishTime.setFont(new Font("宋体", Font.PLAIN, 16));
        publishTime.setBounds(50,170,100,20);
        stock=new JLabel("库存数量");
        stock.setFont(new Font("宋体", Font.PLAIN, 16));
        stock.setBounds(50,210,100,20);
        bookName=new JLabel("图书名称");
        bookName.setFont(new Font("宋体", Font.PLAIN, 16));
        bookName.setBounds(280,50,100,20);
        author=new JLabel("作者");
        author.setFont(new Font("宋体", Font.PLAIN, 16));
        author.setBounds(280,90,100,20);
        publisher=new JLabel("出版社");
        publisher.setFont(new Font("宋体", Font.PLAIN, 16));
        publisher.setBounds(280,130,100,20);
        price=new JLabel("定价");
        price.setFont(new Font("宋体", Font.PLAIN, 16));
        price.setBounds(280,170,100,20);

        bookIdField=new JTextField();
        bookIdField.setColumns(10);
        bookIdField.setBounds(120,50,130,20);
        String[] ty=new String[]{"文学","理学"};
        comboBox = new JComboBox<>(ty);
        comboBox.setBounds(120,90,130,20);
        translatorField=new JTextField();
        translatorField.setColumns(10);
        translatorField.setBounds(120,130,130,20);
        Date date = new Date();
        // 设置 date日期
        datepick.setDate(date);
        datepick.setBounds(120,170,130,20);

        stockField=new JTextField();
        stockField.setColumns(10);
        stockField.setBounds(120,210,130,20);
        bookNameField=new JTextField();
        bookNameField.setColumns(10);
        bookNameField.setBounds(360,50,130,20);
        authorField=new JTextField();
        authorField.setColumns(10);
        authorField.setBounds(360,90,130,20);
        publisherField=new JTextField();
        publisherField.setColumns(10);
        publisherField.setBounds(360,130,130,20);
        priceField=new JTextField();
        priceField.setColumns(10);
        priceField.setBounds(360,170,130,20);

        container.add(bookId);container.add(bookIdField);
        container.add(bookName);container.add(bookNameField);
        container.add(bookType);container.add(comboBox);
        container.add(author);container.add(authorField);
        container.add(translator);container.add(translatorField);
        container.add(publisher);container.add(publisherField);
        container.add(publishTime);container.add(datepick);
        container.add(price);container.add(priceField);
        container.add(stock);container.add(stockField);
        container.add(btn_Add);container.add(btn_Cancel);


        setTitle("添加图书");
        setSize(600,400);
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
        //setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }


    private void addBook(ActionEvent e) throws Exception {
        String bookId = this.bookIdField.getText();
        String bookname = this.bookNameField.getText();
        String booktype = (String) this.comboBox.getSelectedItem();
        String author = this.authorField.getText();
        String translator = this.translatorField.getText();
        String publisher = this.publisherField.getText();
        Date date = this.datepick.getDate();
        java.sql.Date publishtime = new java.sql.Date(date.getTime());

        float price = Float.parseFloat(this.priceField.getText());
        int stock = Integer.parseInt(this.stockField.getText());
        Book book = new Book(bookId, bookname, booktype, author, translator, publisher, publishtime, price, stock);
        BookService bookService = new BookService();
        int i=bookService.addBook(book);
        if (i>0){
            JOptionPane.showMessageDialog(null,"添加成功");
            dispose();
        }else {
            JOptionPane.showMessageDialog(null,"添加失败");
        }
    }

借阅图书实现

java 复制代码
public class BorrowBookForm extends JFrame {
    private JLabel bookId,readerId,bookName,publisher,price,author,publishTime,stock
            ,readerName,readerType,max_num,days_num
            ,borrowNum,isBorrow,borrowDate
            ,readerInfo,borrowInfo,bookInfo;
    private JTextField bookIdField,readerIdField,bookNameField,authorField,publisherField,publishTimeField,priceField,stockField
            ,readerNameField,readerTypeField,max_numField,days_numField
            ,borrowNumField,isBorrowField;
    private JButton btn_Check,btn_Borrow,btn_Close;
    private BorrowService borrowService=new BorrowService();
    private ReaderService readerService=new ReaderService();
    final JXDatePicker datepick1,datepick2;

    public BorrowBookForm() {
        Container container = getContentPane();
        container.setLayout(null);

        btn_Check=new JButton("查询");
        btn_Check.setBounds(450,20,60,20);
        btn_Borrow=new JButton("借出");
        btn_Borrow.setBounds(200,410,60,20);
        btn_Close=new JButton("关闭");
        btn_Close.setBounds(300,410,60,20);
        btn_Close.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        btn_Check.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                checkReaderIdAndBookId(e);
            }
        });
        btn_Borrow.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                borrowBook(e);
            }
        });

        bookId=new JLabel("图书编号");
        bookId.setFont(new Font("宋体", Font.PLAIN, 16));
        bookId.setBounds(50,20,100,20);
        readerId=new JLabel("读者编号");
        readerId.setFont(new Font("宋体", Font.PLAIN, 16));
        readerId.setBounds(250,20,100,20);

        bookInfo=new JLabel("--------------------------图书信息--------------------------");
        bookInfo.setFont(new Font("宋体", Font.PLAIN, 16));
        bookInfo.setBounds(50,55,500,20);
        bookName=new JLabel("图书名称:");
        bookName.setFont(new Font("宋体", Font.PLAIN, 16));
        bookName.setBounds(50,80,100,20);
        author=new JLabel("作者:");
        author.setFont(new Font("宋体", Font.PLAIN, 16));
        author.setBounds(350,80,100,20);
        publisher=new JLabel("出版社:");
        publisher.setFont(new Font("宋体", Font.PLAIN, 16));
        publisher.setBounds(50,110,100,20);
        publishTime=new JLabel("出版时间:");
        publishTime.setFont(new Font("宋体", Font.PLAIN, 16));;
        publishTime.setBounds(350,110,100,20);
        price=new JLabel("订价:");
        price.setFont(new Font("宋体", Font.PLAIN, 16));
        price.setBounds(50,140,100,20);
        stock=new JLabel("库存量:");
        stock.setFont(new Font("宋体", Font.PLAIN, 16));
        stock.setBounds(350,140,100,20);

        readerInfo=new JLabel("--------------------------读者信息--------------------------");
        readerInfo.setFont(new Font("宋体", Font.PLAIN, 16));
        readerInfo.setBounds(50,170,500,20);
        readerName=new JLabel("读者姓名:");
        readerName.setFont(new Font("宋体", Font.PLAIN, 16));
        readerName.setBounds(50,195,100,20);
        readerType=new JLabel("读者类型:");
        readerType.setFont(new Font("宋体", Font.PLAIN, 16));
        readerType.setBounds(350,195,100,20);
        max_num=new JLabel("最大可借数:");
        max_num.setFont(new Font("宋体", Font.PLAIN, 16));
        max_num.setBounds(50,220,100,20);
        days_num=new JLabel("最大可借天数:");
        days_num.setFont(new Font("宋体", Font.PLAIN, 16));
        days_num.setBounds(350,220,130,20);
        borrowInfo=new JLabel("--------------------------借阅信息--------------------------");
        borrowInfo.setFont(new Font("宋体", Font.PLAIN, 16));
        borrowInfo.setBounds(50,250,500,20);

        /**
         * 文本框
         */
        bookIdField=new JTextField();
        bookIdField.setBounds(120,20,100,20);
        readerIdField=new JTextField();
        readerIdField.setBounds(320,20,100,20);
        bookNameField=new JTextField();
        bookNameField.setEditable(false);
        bookNameField.setBounds(140,80,100,20);
        authorField=new JTextField();
        authorField.setEditable(false);
        authorField.setBounds(430,80,100,20);
        publisherField=new JTextField();
        publisherField.setEditable(false);
        publisherField.setBounds(140,110,100,20);
        //出版时间
        Date date = new Date();
        // 设置 date日期
        datepick1= new JXDatePicker();
        datepick1.setDate(date);
        datepick1.setEditable(false);
        datepick1.setBounds(430,110,100,20);

        priceField=new JTextField();
        priceField.setEditable(false);
        priceField.setBounds(140,140,110,20);
        stockField=new JTextField();
        stockField.setEditable(false);
        stockField.setBounds(430,140,100,20);
        readerNameField=new JTextField();
        readerNameField.setEditable(false);
        readerNameField.setBounds(140,195,100,20);
        readerTypeField=new JTextField();
        readerTypeField.setEditable(false);
        readerTypeField.setBounds(430,195,100,20);
        max_numField=new JTextField();
        max_numField.setEditable(false);
        max_numField.setBounds(140,220,100,20);
        days_numField=new JTextField();
        days_numField.setEditable(false);
        days_numField.setBounds(470,220,60,20);
        borrowNumField=new JTextField();
        borrowNumField.setEditable(false);
        borrowNumField.setBounds(210,275,100,20);
        isBorrowField=new JTextField();
        isBorrowField.setEditable(false);
        isBorrowField.setBounds(250,300,100,20);
        //借阅日期
        Date date2 = new Date();
        // 设置 date日期
        datepick2= new JXDatePicker();
        datepick2.setDate(date2);
        datepick2.setBounds(140,325,100,20);

        /**
         * 借阅者文本框
         */
        borrowNum=new JLabel("该读书已借图书数量:");
        borrowNum.setFont(new Font("宋体", Font.PLAIN, 16));
        borrowNum.setBounds(50,275,200,20);
        isBorrow=new JLabel("该读者是否可借所选图书:");
        isBorrow.setFont(new Font("宋体", Font.PLAIN, 16));
        isBorrow.setBounds(50,300,200,20);
        borrowDate=new JLabel("借阅日期:");
        borrowDate.setFont(new Font("宋体", Font.PLAIN, 16));
        borrowDate.setBounds(50,325,100,20);


        /**
         * 添加到容器
         */
        container.add(bookId);container.add(bookIdField);
        container.add(readerId);container.add(readerIdField);
        container.add(btn_Check);
        container.add(bookInfo);
        container.add(bookName);container.add(bookNameField);
        container.add(author);container.add(authorField);
        container.add(publisher);container.add(publisherField);
        container.add(publishTime);container.add(datepick1);
        container.add(price);container.add(priceField);
        container.add(stock);container.add(stockField);
        container.add(readerInfo);
        container.add(readerName);container.add(readerNameField);
        container.add(readerType);container.add(readerTypeField);
        container.add(max_num);container.add(max_numField);
        container.add(days_num);container.add(days_numField);
        container.add(borrowInfo);
        container.add(borrowNum);container.add(borrowNumField);
        container.add(isBorrow);container.add(isBorrowField);
        container.add(borrowDate);container.add(datepick2);
        container.add(btn_Borrow);container.add(btn_Close);

        setTitle("借阅图书");
        setSize(600,500);
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
        //setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    private void borrowBook(ActionEvent e) {
        //获取图书字段
        String bookId = bookIdField.getText();
        //获取库存量
        int stock = Integer.parseInt(this.stockField.getText());
        //获取已借数量
        int outloan = Integer.parseInt(borrowNumField.getText());
        //获取最大可借数
        int maxnum= Integer.parseInt(max_numField.getText());
        //获取读者id
        int readerId = Integer.parseInt(readerIdField.getText());
        //获取天数
        int daysNum = Integer.parseInt(days_numField.getText());
        //获取当前日期
        Date date = this.datepick2.getDate();
        java.sql.Date borrowDate = new java.sql.Date(date.getTime());
        //指定归还日期
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, daysNum);
        Date newDate = cal.getTime();
        //设置借书和还书的标识
        String if_back="";
        Borrow borrow=borrowService.findBorrowById(readerId, bookId);
        System.out.println(borrow);
        if (borrow==null){
            if_back="1";
            Borrow borrowAdd = new Borrow(bookId,readerId,borrowDate,new java.sql.Date(newDate.getTime()),if_back);
            int i=borrowService.addBorrowDate(borrowAdd);
            if (i>0){
                //每次借阅+1;
                outloan++;
                //最大可借数-1;
                maxnum--;
                //库存-1
                stock--;
                if (stock<=0){
                    this.btn_Borrow.setEnabled(false);
                }else {
                    BookService bookService = new BookService();
                    Book book = new Book(bookId,stock,outloan);
                    Reader reader = new Reader(readerId,maxnum);
                    ReaderService readerService = new ReaderService();
                    //更新读者可借的最大数量
                    int i2 = readerService.updateReaderMaxnum(reader);
                    //更新库存量和已借数量
                    int i1 = bookService.updateBookStockAndOutloan(book);
                    borrowService.addBookandRead(bookId,readerId);
                    if (i1>0&&i2>0){
                        //更新后设置库存文本框
                        stockField.setText(String.valueOf(book.getStock()));
                        borrowNumField.setText(String.valueOf(book.getOutloan()));
                        max_numField.setText(String.valueOf(reader.getMax_num()));
                        JOptionPane.showMessageDialog(null,"借出成功");
                        dispose();
                    }
                }
            }else {
                JOptionPane.showMessageDialog(null,"借出失败");
            }
        }
    }

    //连表查询图书和读者信息
    private void checkReaderIdAndBookId(ActionEvent e) {
        String bookId = bookIdField.getText();
        int readerId = Integer.parseInt(readerIdField.getText());
        //System.out.println(readerId);
        ReaderBorrowBook rbb=borrowService.findBorrowBybookIdAndreaderId(bookId,readerId);
        //System.out.println(rbb);
        if (rbb!=null){
            bookNameField.setText(rbb.getBookname());//书名
            authorField.setText(rbb.getAuthor());//作者
            publisherField.setText(rbb.getPublisher());//出版社
            datepick1.setDate(rbb.getPublish_time());//出版日期
            priceField.setText(String.valueOf(rbb.getPricce()));//价格
            stockField.setText(String.valueOf(rbb.getStock()));//库存
            readerNameField.setText(rbb.getReadername());//读者名
            readerTypeField.setText(rbb.getReadertype());//读者类型
            max_numField.setText(String.valueOf(rbb.getMax_num()));//最大借书量
            days_numField.setText(String.valueOf(rbb.getDays_num()));//最大借书天数
            borrowNumField.setText(String.valueOf(rbb.getOutloan()));//已借书数量
            Borrow borrow=borrowService.findBorrowById(readerId, bookId);

            System.out.println(borrow);
            if (rbb.getMax_num()<=0){
                isBorrowField.setText("无法再借图书");
                //不能点击借书按钮
                btn_Borrow.setEnabled(false);
            }else {
                //可以点击借书按钮
                btn_Borrow.setEnabled(true);
            }
            if (borrow==null){
                    isBorrowField.setText("是");
                    btn_Borrow.setEnabled(true);
            }else {
                //判读是1还是0,从而判断是否被借还是归还
                if (borrow.getIf_back().equals("1")){
                    isBorrowField.setText("否");
                    datepick2.setDate(borrow.getBorrowDate());
                    btn_Borrow.setEnabled(false);
                }else if (borrow.getIf_back().equals("0")){
                    isBorrowField.setText("是");
                    btn_Borrow.setEnabled(true);
                }
            }
        }
    }
}

四、源码获取

源码已经打包了,点击下面蓝色链接获取!

点我获取源码

相关推荐
憨子周36 分钟前
2M的带宽怎么怎么设置tcp滑动窗口以及连接池
java·网络·网络协议·tcp/ip
FIN技术铺2 小时前
Redis集群模式之Redis Sentinel vs. Redis Cluster
数据库·redis·sentinel
霖雨2 小时前
使用Visual Studio Code 快速新建Net项目
java·ide·windows·vscode·编辑器
SRY122404192 小时前
javaSE面试题
java·开发语言·面试
Fiercezm3 小时前
JUC学习
java
无尽的大道3 小时前
Java 泛型详解:参数化类型的强大之处
java·开发语言
ZIM学编程3 小时前
Java基础Day-Sixteen
java·开发语言·windows
我不是星海3 小时前
1.集合体系补充(1)
java·数据结构
放逐者-保持本心,方可放逐3 小时前
react 组件应用
开发语言·前端·javascript·react.js·前端框架
P.H. Infinity3 小时前
【RabbitMQ】07-业务幂等处理
java·rabbitmq·java-rabbitmq