找回密碼
       立即注冊(cè)

      QQ登錄

      只需一步,快速開始

      搜索
      查看: 2385|回復(fù): 0
      收起左側(cè)

      Java圖書管理系統(tǒng)源程序 請(qǐng)多多指教

      [復(fù)制鏈接]
      ID:871814 發(fā)表于 2021-1-1 14:56 | 顯示全部樓層 |閱讀模式
      圖書管理系統(tǒng)是智能辦公系統(tǒng)的重要組成部分。目前,圖書管理系統(tǒng)正以方便、快捷的優(yōu)點(diǎn)慢慢滲入人們的生活,將傳統(tǒng)的圖書管理方式徹底的解脫出來,提高管理效率,減輕管理人員的工作量,減小出概率,使讀者可以花更多的時(shí)間在選擇圖書上。從而使人們有更多時(shí)間來獲取信息、了解信息、掌握信息。采用數(shù)據(jù)庫技術(shù)生成的圖書管理系統(tǒng)將會(huì)極大的方便借閌者并簡(jiǎn)化圖書館管理人員的勞動(dòng),使理人員從繁忙、復(fù)雜的工作進(jìn)入到一個(gè)簡(jiǎn)單.高效的工作中;谶@個(gè)問題,開發(fā)了圖書管理系統(tǒng)。系統(tǒng)實(shí)現(xiàn)了借還書的方便性、高效性、有效性和及時(shí)性。
      public class MainFrame {
              JFrame frame = new JFrame("個(gè)人書屋");
              Container container = frame.getContentPane();
              public MainFrame() {
                      // 設(shè)置背景圖片
                      new BackgroundImage(frame,container,"mainFrame.jpg");
                      // 添加工具欄以及各組件和監(jiān)聽事件
                      new MenuBar(frame);
                      // 設(shè)置窗口大小、位置、可視、默認(rèn)關(guān)閉方式等
                      new FrameOption(frame);
              }
      }

      工具欄類
      public class MenuBar {
              JMenuBar menuBar;
              JMenuItem menuItemBookInformation;
              JMenuItem menuItemBorrowManage;
              JMenuItem menuItemChangUser;
              JMenuItem menuItemExit;
              public MenuBar(JFrame frame) {
                      menuBar = new JMenuBar();
                      // 圖書信息管理菜單項(xiàng)
                      menuItemBookInformation = new JMenuItem();         
                      setMenuItemBookInformationn(frame);
                      // 圖書借閱管理菜單項(xiàng)
                      menuItemBorrowManage = new JMenuItem();
                      setMenuItemBorrowManage(frame);
                      // 用戶信息更改菜單項(xiàng)
                      menuItemChangUser = new JMenuItem();
                      setMenuItemChangeUser(frame);
                      // 退出系統(tǒng)菜單項(xiàng)
                      menuItemExit = new JMenuItem();
                      setMenuItemExit(frame);
                      menuBar.add(menuItemBorrowManage);
                      menuBar.add(menuItemBookInformation);
                      menuBar.add(menuItemChangUser);
                      menuBar.add(menuItemExit);
                      frame.setJMenuBar(menuBar);
              }
              
              /**
               * 設(shè)置退出系統(tǒng)菜單項(xiàng)
               */
              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();
                              }
                      });
              }
      }

      設(shè)置窗口相關(guān)設(shè)置類
      public class FrameOption {
              public FrameOption(JFrame frame) {
                      frame.setVisible(true);
                      // 窗口不可調(diào)整大小
                      frame.setResizable(false);
                      frame.setSize(800, 508);
                      frame.setLocation(200,100);
                      frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);        
              }

      }
      public class BookBorrow {
              JFrame frame = new JFrame("個(gè)人書屋");
              Container container = frame.getContentPane();
              // 表格
              JTable table;
              // 顯示表格的滾動(dòng)面板
              JScrollPane scrollPane;
              // 歸還圖書按鈕
              JButton buttonReturn;
              BorrowAction borrowAction;
              public BookBorrow() {
                      frame.setLayout(null);
                      // 設(shè)置背景圖片
                      new BackgroundImage(frame,container,"BookBorrow.jpg");
                      // 添加工具欄以及各組件和監(jiān)聽事件
                      new MenuBar(frame);
                      // 設(shè)置表格
                      setTable();
                      // 歸還圖書按鈕
                      buttonReturn = new JButton();
                      setButtonReturn();
                      container.add(buttonReturn);
                      container.add(scrollPane);
                      // 設(shè)置窗口大小、位置、可視、默認(rèn)關(guān)閉方式等                        
                      new FrameOption(frame);
              }


              
              

              /**
               * 設(shè)置歸還圖書按鈕
               */
              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,"請(qǐng)先選中要?dú)w還的表格項(xiàng)","錯(cuò)誤"
                                                              , JOptionPane.PLAIN_MESSAGE);
                                      }                        
                              }
                      });
              }

              /**
               * 設(shè)置表格
               */
              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();
                      }
              }
      }

      借還書相應(yīng)行為類
      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);
              }
      }



      借還書數(shù)據(jù)庫操作類
              /**
               * 查詢借閱信息
               *
               * @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()){//如果對(duì)象中有數(shù)據(jù),就會(huì)循環(huán)打印出來                  
                              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();//首先拿到數(shù)據(jù)庫的連接
              String sql="UPDATE tb_books "
                              // ISBN、圖書名稱、作者、價(jià)格
                              + "SET "
                              // 借書人姓名、借書人電話
                              + "borrower_name = ?, borrower_phone = ? "
                      // 參數(shù)用?表示,相當(dāng)于占位符
                              + "WHERE ID = ?";
              // 預(yù)編譯sql語句
              PreparedStatement psmt = con.prepareStatement(sql);
              // 先對(duì)應(yīng)SQL語句,給SQL語句傳遞參數(shù)
              psmt.setString(1, book.getBorrowerName());
              psmt.setString(2, book.getBorrowerPhone());
              psmt.setInt(3, book.getID());
              // 執(zhí)行SQL語句
              psmt.execute();   
          }

      @SuppressWarnings("serial")
      public class BookInformation extends JFrame {
              JFrame frame = new JFrame("個(gè)人書屋");
              Container container = frame.getContentPane();
              // 增加、刪除、修改按鈕
              JButton buttonAdd, buttonDel, buttonChange,buttonReset;
              // ISBN、圖書名稱、圖書價(jià)格、圖書作者文本框
              JTextField textFieldISBN, textFieldBookName, textFieldPrice, textFieldAuthor;
              // 出版社、圖書分類號(hào)、借書人姓名文本框
              JTextField textFieldPublishedHouse, textFieldBookCategory;
              // 借書人姓名、借書人電話
              JTextField textFieldBorrowName, textFieldBorrowPhone;
              // 表格
              JTable table;
              // 顯示表格的滾動(dòng)面板
              JScrollPane scrollPane;
              BookAction bookAction;

              public BookInformation()  {
                      frame.setLayout(null);
                      // 設(shè)置背景圖片
                      new BackgroundImage(frame,container,"BookInformation.jpg");
                      // 添加工具欄以及各組件和監(jiān)聽事件
                      new MenuBar(frame);
                      bookAction = new BookAction();
                      // ISBN文本框
                      textFieldISBN = new JTextField();
                      setTextFieldISBN();               
                      // 圖書名稱文本框
                      textFieldBookName = new JTextField();
                      setTextFieldBookName();
                      // 圖書價(jià)格文本框
                      textFieldPrice = new JTextField();
                      setTextFieldBookPrice();
                      // 圖書作者文本框
                      textFieldAuthor = new JTextField();
                      setTextFieldAuthor();
                      // 出版社文本框
                      textFieldPublishedHouse = new JTextField();
                      setTextFieldPublishedHouse();        
                      // 圖書分類號(hào)文本框
                      textFieldBookCategory = new JTextField();
                      setTextFieldBookCategory();        
                      // 借書人姓名文本框
                      textFieldBorrowName = new JTextField();
                      setTextFieldBorrowName();
                      // 借書人電話文本框
                      textFieldBorrowPhone = new JTextField();
                      setTextFieldBorrowPhone();
                      // 設(shè)置窗體表格
                      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);
                      // 設(shè)置窗口大小、位置、可視、默認(rèn)關(guān)閉方式等
                      new FrameOption(frame);
              }

              /**
               * 設(shè)置借書人電話文本框
               */
              private void setTextFieldBorrowPhone() {
                      textFieldBorrowPhone.setBounds(490,312,232,23);
              }
              /**
               * 設(shè)置借書人姓名文本框
               */
              private void setTextFieldBorrowName() {
                      textFieldBorrowName.setBounds(150,312,200,23);
              }
              /**
               * 設(shè)置修改按鈕
               */
              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,"表中沒有該數(shù)據(jù)","錯(cuò)誤"
                                                              , JOptionPane.PLAIN_MESSAGE);
                                      }
                              }
                      });
              }
              /**
               * 設(shè)置圖書分類號(hào)文本框
               */
              private void setTextFieldBookCategory() {
                      textFieldBookCategory.setBounds(582,340,140,23);
              }

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

              /**
               * 設(shè)置圖書作者文本框
               */
              private void setTextFieldAuthor() {
                      textFieldAuthor.setBounds(586,280,140,23);
              }

              /**
               * 設(shè)置圖書價(jià)格文本框
               */
              private void setTextFieldBookPrice() {
                      textFieldPrice.setBounds(120,340,140,23);
              }

              /**
               * 設(shè)置圖書名稱文本框
               */
              private void setTextFieldBookName() {
                      textFieldBookName.setBounds(348,280,140,23);
              }

              /**
               * 設(shè)置ISBN文本框
               */
              private void setTextFieldISBN() {
                      // 限制文本框長(zhǎng)度為17
                      textFieldISBN.setDocument(new LimitTextLength(17));
                      textFieldISBN.setBounds(120,280,140,23);
              }

              /**
               * 設(shè)置刪除按鈕
               */
              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();
                                      }                        
                              }
                      });
              }

              /**
               * 設(shè)置文本框重置按鈕
               */
              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("");
                              }
                      });
              }

              /**
               * 設(shè)置添加按鈕
               */
              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號(hào)不能為空","錯(cuò)誤"
                                                              , JOptionPane.PLAIN_MESSAGE);
                                      }
                                      else if(textFieldISBN.getText().length() != 17) {        
                                              JOptionPane.showMessageDialog(null,"ISBN號(hào)位數(shù)必須是13位","錯(cuò)誤"
                                                              , JOptionPane.PLAIN_MESSAGE);        
                                      }
                                      else if(textFieldBookName.getText().length() == 0) {
                                              JOptionPane.showMessageDialog(null, "圖書名稱不能為空", "錯(cuò)誤"
                                                              , JOptionPane.PLAIN_MESSAGE);
                                      }
                                             
                                      else if(textFieldAuthor.getText().length() == 0) {
                                              JOptionPane.showMessageDialog(null, "圖書作者不能為空", "錯(cuò)誤"
                                                              , JOptionPane.PLAIN_MESSAGE);
                                      }
                                      else if(textFieldPrice.getText().length() == 0) {
                                              JOptionPane.showMessageDialog(null, "圖書價(jià)格不能為空", "錯(cuò)誤"
                                                              , JOptionPane.PLAIN_MESSAGE);
                                      }
                                      else if(textFieldPublishedHouse.getText().length() == 0) {
                                              JOptionPane.showMessageDialog(null, "出版社不能為空", "錯(cuò)誤"
                                                              , JOptionPane.PLAIN_MESSAGE);
                                      }
                                      else if(textFieldBookCategory.getText().length() == 0) {
                                              JOptionPane.showMessageDialog(null, "圖書分類號(hào)不能為空", "錯(cuò)誤"
                                                              , JOptionPane.PLAIN_MESSAGE);
                                      }
                                      else if(textFieldPrice.getText().length() > 4) {
                                              JOptionPane.showMessageDialog(null, "圖書價(jià)格不能超過4位數(shù)", "錯(cuò)誤"
                                                              , 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();
                                              }
                                      }
                              }
                      });
              }

              /**
               * 設(shè)置窗體表格
               */
              private void setTable() {
                      String[] columnNames = {"ID", "圖書名稱","圖書作者","圖書價(jià)格(元)","ISBN"
                                      ,"出版社","分類號(hào)", "借書人姓名", "借書人電話"
                                      };        
                      try {
                              BookAction bookAction = new BookAction();
                              Object[][] results = bookAction.initializTable(columnNames);
                     
                              table = new JTable(results,columnNames);
                              // 設(shè)置表格字段居中
                              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();
                      }
              }
      }
      回復(fù)

      使用道具 舉報(bào)

      本版積分規(guī)則

      手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

      Powered by 單片機(jī)教程網(wǎng)

      快速回復(fù) 返回頂部 返回列表