找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1646|回复: 0
收起左侧

Java图书管理系统源程序 请多多指教

[复制链接]
ID:871814 发表于 2021-1-1 14:56 | 显示全部楼层 |阅读模式
图书管理系统是智能办公系统的重要组成部分。目前,图书管理系统正以方便、快捷的优点慢慢渗入人们的生活,将传统的图书管理方式彻底的解脱出来,提高管理效率,减轻管理人员的工作量,减小出概率,使读者可以花更多的时间在选择图书上。从而使人们有更多时间来获取信息、了解信息、掌握信息。采用数据库技术生成的图书管理系统将会极大的方便借闶者并简化图书馆管理人员的劳动,使理人员从繁忙、复杂的工作进入到一个简单.高效的工作中。基于这个问题,开发了图书管理系统。系统实现了借还书的方便性、高效性、有效性和及时性。
public class MainFrame {
        JFrame frame = new JFrame("个人书屋");
        Container container = frame.getContentPane();
        public MainFrame() {
                // 设置背景图片
                new BackgroundImage(frame,container,"mainFrame.jpg");
                // 添加工具栏以及各组件和监听事件
                new MenuBar(frame);
                // 设置窗口大小、位置、可视、默认关闭方式等
                new FrameOption(frame);
        }
}

工具栏类
public class MenuBar {
        JMenuBar menuBar;
        JMenuItem menuItemBookInformation;
        JMenuItem menuItemBorrowManage;
        JMenuItem menuItemChangUser;
        JMenuItem menuItemExit;
        public MenuBar(JFrame frame) {
                menuBar = new JMenuBar();
                // 图书信息管理菜单项
                menuItemBookInformation = new JMenuItem();         
                setMenuItemBookInformationn(frame);
                // 图书借阅管理菜单项
                menuItemBorrowManage = new JMenuItem();
                setMenuItemBorrowManage(frame);
                // 用户信息更改菜单项
                menuItemChangUser = new JMenuItem();
                setMenuItemChangeUser(frame);
                // 退出系统菜单项
                menuItemExit = new JMenuItem();
                setMenuItemExit(frame);
                menuBar.add(menuItemBorrowManage);
                menuBar.add(menuItemBookInformation);
                menuBar.add(menuItemChangUser);
                menuBar.add(menuItemExit);
                frame.setJMenuBar(menuBar);
        }
        
        /**
         * 设置退出系统菜单项
         */
        private void setMenuItemExit(JFrame frame) {
                menuItemExit.setIcon(new ImageIcon("res/menuItemExit.jpg"));
                menuItemExit.addActionListener(new ActionListener() {               
                        @Override
                        public void actionPerformed(ActionEvent e) {
                                // 使父窗体不可见
                                frame.setVisible(false);
                                new Login();
                        }
                });
        }

        private void setMenuItemChangeUser(JFrame frame) {
                menuItemChangUser.setIcon(new ImageIcon("res/menuItemChangePassword.jpg"));
                menuItemChangUser.addActionListener(new ActionListener() {        
                        @Override
                        public void actionPerformed(ActionEvent e) {
                                // 使父窗体不可见
                                frame.setVisible(false);
                                new ChangeUserInformation();
                        }
                });
        }

        private void setMenuItemBorrowManage(JFrame frame) {
                menuItemBorrowManage.setIcon(new ImageIcon("res/menuBookCategoryManage.jpg"));
                menuItemBorrowManage.addActionListener(new ActionListener() {               
                        @Override
                        public void actionPerformed(ActionEvent e) {
                                // 使父窗体不可见
                                frame.setVisible(false);
                                new BookBorrow();
                        }
                });
        }

        private void setMenuItemBookInformationn(JFrame frame) {
                menuItemBookInformation.setIcon(new ImageIcon("res/menuBookInformationManage.jpg"));
                menuItemBookInformation.addActionListener(new ActionListener() {               
                        @Override
                        public void actionPerformed(ActionEvent e) {
                                // 使父窗体不可见
                                frame.setVisible(false);        
                                new BookInformation();
                        }
                });
        }
}

设置窗口相关设置类
public class FrameOption {
        public FrameOption(JFrame frame) {
                frame.setVisible(true);
                // 窗口不可调整大小
                frame.setResizable(false);
                frame.setSize(800, 508);
                frame.setLocation(200,100);
                frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);        
        }

}
public class BookBorrow {
        JFrame frame = new JFrame("个人书屋");
        Container container = frame.getContentPane();
        // 表格
        JTable table;
        // 显示表格的滚动面板
        JScrollPane scrollPane;
        // 归还图书按钮
        JButton buttonReturn;
        BorrowAction borrowAction;
        public BookBorrow() {
                frame.setLayout(null);
                // 设置背景图片
                new BackgroundImage(frame,container,"BookBorrow.jpg");
                // 添加工具栏以及各组件和监听事件
                new MenuBar(frame);
                // 设置表格
                setTable();
                // 归还图书按钮
                buttonReturn = new JButton();
                setButtonReturn();
                container.add(buttonReturn);
                container.add(scrollPane);
                // 设置窗口大小、位置、可视、默认关闭方式等                        
                new FrameOption(frame);
        }


        
        

        /**
         * 设置归还图书按钮
         */
        private void setButtonReturn() {
                buttonReturn.setBounds(580,390,100,25);
                buttonReturn.setIcon(new ImageIcon("res/button_return.jpg"));
                buttonReturn.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                                try {
                                        borrowAction = new BorrowAction();
                                        borrowAction.BorrowBook(table);
                                        frame.setVisible(false);        
                                        new BookBorrow();
                                } catch(Exception e1) {
                                        JOptionPane.showMessageDialog(null,"请先选中要归还的表格项","错误"
                                                        , JOptionPane.PLAIN_MESSAGE);
                                }                        
                        }
                });
        }

        /**
         * 设置表格
         */
        private void setTable() {
                String[] columnNames = {"ID", "图书名称", "借书人姓名", "借书人电话"};
                try {
                        BorrowAction borrowAction = new BorrowAction();
                        Object[][] results = borrowAction.initializTable(columnNames);
                        table = new JTable(results,columnNames);
                        new SetTableColumnCenter(table);
                        scrollPane = new JScrollPane(table);
                        scrollPane.setViewportView(table);
                        scrollPane.setBounds(20,80,760,190);                        
                } catch(Exception e) {
                        e.printStackTrace();
                }
        }
}

借还书相应行为类
public class BorrowAction {
        BookDao borrowerDao;
        /**
         * 初始化借还书管理窗体表格
         * @return
         *                 results
         */
        @SuppressWarnings("rawtypes")
        public Object[][] initializTable(String[] columnNames) throws Exception{
                borrowerDao = new BookDao();
                List list = borrowerDao.borrowQuery();
                Object[][] results = new Object[list.size()][columnNames.length];
                for(int i = 0; i < list.size(); i++) {               
                        Book book = (Book)list.get(i);                        
                        results[ i][0] = book.getID();
                        results[ i][1] = book.getBookName();        
                        results[ i][2] = book.getBorrowerName();               
                        results[ i][3] = book.getBorrowerPhone();
                }                  
                return results;
        }
        
        /**
         * 归还图书
         */
        public void BorrowBook (JTable table) throws Exception{
        borrowerDao=new BookDao();
        Book book=new Book();     
                int selRow = table.getSelectedRow();
                int ID = Integer.parseInt(table.getValueAt(selRow, 0).toString());        
        book.setID(ID);
        book.setBorrowerName(null);
        book.setBorrowerPhone(null);
        // 归还图书
        borrowerDao.returnBook(book);
        }
}



借还书数据库操作类
        /**
         * 查询借阅信息
         *
         * @return
         *                 bookList
         */
        public List<Book> borrowQuery() throws Exception{               
                Connection con = DBUtil.getConnection();               
                Statement stmt = con.createStatement();               
                ResultSet rs = stmt.executeQuery(""
                                // ID、书名、借书人姓名、借书人电话
                                + "SELECT ID, book_name, borrower_name, borrower_phone "
                                + "FROM tb_books "
                                + "WHERE borrower_name IS NOT NULL"
                                );              
                List<Book> bookList = new ArrayList<Book>();               
                Book book = null;            
                while (rs.next()){//如果对象中有数据,就会循环打印出来                  
                        book = new Book();            
                        book.setID(rs.getInt("ID"));
                        book.setBookName(rs.getString("book_name"));               
                        book.setBorrowerName(rs.getString("borrower_name"));
                        book.setBorrowerPhone(rs.getString("borrower_phone"));
                        bookList.add(book);               
                }               
                return bookList;         
        }

        /**
         * 更新图书信息,归还图书
         */
    public void returnBook(Book book) throws SQLException{
        Connection con=DBUtil.getConnection();//首先拿到数据库的连接
        String sql="UPDATE tb_books "
                        // ISBN、图书名称、作者、价格
                        + "SET "
                        // 借书人姓名、借书人电话
                        + "borrower_name = ?, borrower_phone = ? "
                // 参数用?表示,相当于占位符
                        + "WHERE ID = ?";
        // 预编译sql语句
        PreparedStatement psmt = con.prepareStatement(sql);
        // 先对应SQL语句,给SQL语句传递参数
        psmt.setString(1, book.getBorrowerName());
        psmt.setString(2, book.getBorrowerPhone());
        psmt.setInt(3, book.getID());
        // 执行SQL语句
        psmt.execute();   
    }

@SuppressWarnings("serial")
public class BookInformation extends JFrame {
        JFrame frame = new JFrame("个人书屋");
        Container container = frame.getContentPane();
        // 增加、删除、修改按钮
        JButton buttonAdd, buttonDel, buttonChange,buttonReset;
        // ISBN、图书名称、图书价格、图书作者文本框
        JTextField textFieldISBN, textFieldBookName, textFieldPrice, textFieldAuthor;
        // 出版社、图书分类号、借书人姓名文本框
        JTextField textFieldPublishedHouse, textFieldBookCategory;
        // 借书人姓名、借书人电话
        JTextField textFieldBorrowName, textFieldBorrowPhone;
        // 表格
        JTable table;
        // 显示表格的滚动面板
        JScrollPane scrollPane;
        BookAction bookAction;

        public BookInformation()  {
                frame.setLayout(null);
                // 设置背景图片
                new BackgroundImage(frame,container,"BookInformation.jpg");
                // 添加工具栏以及各组件和监听事件
                new MenuBar(frame);
                bookAction = new BookAction();
                // ISBN文本框
                textFieldISBN = new JTextField();
                setTextFieldISBN();               
                // 图书名称文本框
                textFieldBookName = new JTextField();
                setTextFieldBookName();
                // 图书价格文本框
                textFieldPrice = new JTextField();
                setTextFieldBookPrice();
                // 图书作者文本框
                textFieldAuthor = new JTextField();
                setTextFieldAuthor();
                // 出版社文本框
                textFieldPublishedHouse = new JTextField();
                setTextFieldPublishedHouse();        
                // 图书分类号文本框
                textFieldBookCategory = new JTextField();
                setTextFieldBookCategory();        
                // 借书人姓名文本框
                textFieldBorrowName = new JTextField();
                setTextFieldBorrowName();
                // 借书人电话文本框
                textFieldBorrowPhone = new JTextField();
                setTextFieldBorrowPhone();
                // 设置窗体表格
                setTable();
                // 增加按钮
                buttonAdd = new JButton();
                setButtonAdd();        
                // 删除按钮
                buttonDel = new JButton();
                setButtonDel();               
                // 修改按钮
                buttonChange = new JButton();
                setButtonChange();        
                // 重置按钮
                buttonReset = new JButton();
                setButtonReset();
                container.add(scrollPane);
                container.add(buttonAdd);
                container.add(buttonDel);
                container.add(buttonReset);
                container.add(buttonChange);
                container.add(textFieldISBN);
                container.add(textFieldBookName);
                container.add(textFieldAuthor);
                container.add(textFieldPrice);
                container.add(textFieldBookCategory);
                container.add(textFieldPublishedHouse);
                container.add(textFieldBorrowName);
                container.add(textFieldBorrowPhone);
                // 设置窗口大小、位置、可视、默认关闭方式等
                new FrameOption(frame);
        }

        /**
         * 设置借书人电话文本框
         */
        private void setTextFieldBorrowPhone() {
                textFieldBorrowPhone.setBounds(490,312,232,23);
        }
        /**
         * 设置借书人姓名文本框
         */
        private void setTextFieldBorrowName() {
                textFieldBorrowName.setBounds(150,312,200,23);
        }
        /**
         * 设置修改按钮
         */
        private void setButtonChange() {
                buttonChange.setBounds(470,390,60,25);
                buttonChange.setIcon(new ImageIcon("res/button_change.jpg"));
                buttonChange.addActionListener(new ActionListener() {               
                        @Override
                        public void actionPerformed(ActionEvent e) {        
                                try {
                                        bookAction.changeBookInformation(textFieldISBN, textFieldBookName
                                                        ,textFieldPrice, textFieldAuthor, textFieldPublishedHouse
                                                        , textFieldBookCategory, textFieldBorrowName, textFieldBorrowPhone, table);               
                                        frame.setVisible(false);
                                        new BookInformation();                                
                                }catch(Exception e1) {
                                        JOptionPane.showMessageDialog(null,"表中没有该数据","错误"
                                                        , JOptionPane.PLAIN_MESSAGE);
                                }
                        }
                });
        }
        /**
         * 设置图书分类号文本框
         */
        private void setTextFieldBookCategory() {
                textFieldBookCategory.setBounds(582,340,140,23);
        }

        /**
         * 设置出版社文本框
         */
        private void setTextFieldPublishedHouse() {
                textFieldPublishedHouse.setBounds(348,340,140,23);
        }

        /**
         * 设置图书作者文本框
         */
        private void setTextFieldAuthor() {
                textFieldAuthor.setBounds(586,280,140,23);
        }

        /**
         * 设置图书价格文本框
         */
        private void setTextFieldBookPrice() {
                textFieldPrice.setBounds(120,340,140,23);
        }

        /**
         * 设置图书名称文本框
         */
        private void setTextFieldBookName() {
                textFieldBookName.setBounds(348,280,140,23);
        }

        /**
         * 设置ISBN文本框
         */
        private void setTextFieldISBN() {
                // 限制文本框长度为17
                textFieldISBN.setDocument(new LimitTextLength(17));
                textFieldISBN.setBounds(120,280,140,23);
        }

        /**
         * 设置删除按钮
         */
        private void setButtonDel() {
                buttonDel.setBounds(580,390,60,25);
                buttonDel.setIcon(new ImageIcon("res/button_del.jpg"));
                buttonDel.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                                // TODO Auto-generated method stub
                                try {
                                        bookAction.delBookInformation(table);
                                        frame.setVisible(false);        
                                        new BookInformation();
                                } catch(Exception e1) {
                                        e1.printStackTrace();
                                }                        
                        }
                });
        }

        /**
         * 设置文本框重置按钮
         */
        private void setButtonReset() {
                buttonReset.setBounds(270,390,150,25);
                buttonReset.setIcon(new ImageIcon("res/button_textReset.jpg"));
                buttonReset.addActionListener(new ActionListener() {                        
                        @Override
                        public void actionPerformed(ActionEvent e) {
                                // TODO Auto-generated method stub
                                textFieldBookName.setText("");
                                textFieldAuthor.setText("");
                                textFieldISBN.setText("");
                                textFieldPrice.setText("");
                                textFieldBookCategory.setText("");
                                textFieldPublishedHouse.setText("");
                        }
                });
        }

        /**
         * 设置添加按钮
         */
        private void setButtonAdd() {
                buttonAdd.setBounds(700,390,60,25);
                buttonAdd.setIcon(new ImageIcon("res/button_add.jpg"));
                buttonAdd.addActionListener(new ActionListener() {               
                        @Override
                        public void actionPerformed(ActionEvent e) {
                                // TODO Auto-generated method stub
                                if(textFieldISBN.getText().length() == 0) {
                                        JOptionPane.showMessageDialog(null,"ISBN号不能为空","错误"
                                                        , JOptionPane.PLAIN_MESSAGE);
                                }
                                else if(textFieldISBN.getText().length() != 17) {        
                                        JOptionPane.showMessageDialog(null,"ISBN号位数必须是13位","错误"
                                                        , JOptionPane.PLAIN_MESSAGE);        
                                }
                                else if(textFieldBookName.getText().length() == 0) {
                                        JOptionPane.showMessageDialog(null, "图书名称不能为空", "错误"
                                                        , JOptionPane.PLAIN_MESSAGE);
                                }
                                       
                                else if(textFieldAuthor.getText().length() == 0) {
                                        JOptionPane.showMessageDialog(null, "图书作者不能为空", "错误"
                                                        , JOptionPane.PLAIN_MESSAGE);
                                }
                                else if(textFieldPrice.getText().length() == 0) {
                                        JOptionPane.showMessageDialog(null, "图书价格不能为空", "错误"
                                                        , JOptionPane.PLAIN_MESSAGE);
                                }
                                else if(textFieldPublishedHouse.getText().length() == 0) {
                                        JOptionPane.showMessageDialog(null, "出版社不能为空", "错误"
                                                        , JOptionPane.PLAIN_MESSAGE);
                                }
                                else if(textFieldBookCategory.getText().length() == 0) {
                                        JOptionPane.showMessageDialog(null, "图书分类号不能为空", "错误"
                                                        , JOptionPane.PLAIN_MESSAGE);
                                }
                                else if(textFieldPrice.getText().length() > 4) {
                                        JOptionPane.showMessageDialog(null, "图书价格不能超过4位数", "错误"
                                                        , JOptionPane.PLAIN_MESSAGE);
                                }
                                else {
                                        try {
                                                bookAction.addBookInformation(textFieldISBN, textFieldBookName, textFieldPrice,textFieldAuthor
                                                                ,textFieldPublishedHouse,textFieldBookCategory,textFieldBorrowName,textFieldBorrowPhone);               
                                                frame.setVisible(false);        
                                                new BookInformation();
                                        }catch(Exception e1) {
                                                e1.printStackTrace();
                                        }
                                }
                        }
                });
        }

        /**
         * 设置窗体表格
         */
        private void setTable() {
                String[] columnNames = {"ID", "图书名称","图书作者","图书价格(元)","ISBN"
                                ,"出版社","分类号", "借书人姓名", "借书人电话"
                                };        
                try {
                        BookAction bookAction = new BookAction();
                        Object[][] results = bookAction.initializTable(columnNames);
               
                        table = new JTable(results,columnNames);
                        // 设置表格字段居中
                        new SetTableColumnCenter(table);
                        scrollPane = new JScrollPane(table);
                        scrollPane.setViewportView(table);
                        scrollPane.setBounds(20,80,760,190);                        
                        table.addMouseListener(new MouseListener() {                        
                                @Override
                                public void mouseReleased(MouseEvent e) {
                                        // TODO Auto-generated method stub                        
                                }                        
                                @Override
                                public void mousePressed(MouseEvent e) {
                                        // TODO Auto-generated method stub        
                                }                                
                                @Override
                                public void mouseExited(MouseEvent e) {
                                        // TODO Auto-generated method stub                                       
                                }                                
                                @Override
                                public void mouseEntered(MouseEvent e) {
                                        // TODO Auto-generated method stub                                       
                                }                        
                                @Override
                                public void mouseClicked(MouseEvent e) {
                                        // TODO Auto-generated method stub
                                        String ISBN,  bookName, price, author;
                                        String publishedHouse, category, borrowName, borrowPhone;
                                       
                                        int selRow = table.getSelectedRow();
                        
                                        bookName = table.getValueAt(selRow, 1).toString();
                                        author = table.getValueAt(selRow, 2).toString();
                                        price = table.getValueAt(selRow, 3).toString();
                                        ISBN = table.getValueAt(selRow, 4).toString();
                                        publishedHouse = table.getValueAt(selRow, 5).toString();
                                        category = table.getValueAt(selRow, 6).toString();        
                                                
                                        if (table.getValueAt(selRow, 7).toString() == null) {
                                                borrowName = "";
                                        }
                                        else {
                                                borrowName = table.getValueAt(selRow, 7).toString();
                                        }
                                       
                                        if (table.getValueAt(selRow, 8).toString() == null) {
                                                borrowPhone = "";
                                        }
                                        else {
                                                borrowPhone = table.getValueAt(selRow, 8).toString();
                                        }                        
                                        textFieldBookName.setText(bookName);
                                        textFieldAuthor.setText(author);
                                        textFieldPrice.setText(price);
                                        textFieldISBN.setText(ISBN);
                                        textFieldPublishedHouse.setText(publishedHouse);
                                        textFieldBookCategory.setText(category);
                                        textFieldBorrowName.setText(borrowName);
                                        textFieldBorrowPhone.setText(borrowPhone);
                                }
                        });
                } catch(Exception e) {
                        e.printStackTrace();
                }
        }
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表