您好,登錄后才能下訂單哦!
小編給大家分享一下使用java實現圖書信息管理代方法,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
圖書信息管理與讀者信息管理相似,共包括兩部分,圖書添加和圖書查詢與修改,圖書信息添加要求正確輸入圖書的八項信息,并且ISBN編號唯一,實現過程中要將ISBN作為主鍵。下面看實現過程:
數據庫中書籍:
添加書籍過程:
數據庫內容增加:
圖書的查詢與修改:查詢過程可以根據不同的條件查詢,如ISBN,作者,出版社,圖書名稱等,默認將所有圖書全部顯示在列表中,輸入查詢條件后再表格中顯示查詢結果。
本模塊用到標簽面板,可以很方便的查詢所需要的信息。修改過程輸入ISBN編號后按回車鍵將在下方顯示所有的圖書信息,然后修改圖書信息后所有內容就會更新。
圖書查詢:
圖書信息修改:
添加圖書源代碼如下:AddBook.java
package pdsu.bbm.view; import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.Font; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Iterator; import java.util.List; import javax.swing.ButtonGroup; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import pdsu.bbm.dao.BookDao; import pdsu.bbm.dao.BookTypeDao; import pdsu.bbm.model.Book; import pdsu.bbm.model.BookType; public class AddBook extends JFrame implements ActionListener { private JPanel contentPane; private JLabel label_1; private JLabel lbll; private JLabel label_2; private JLabel label_3; private JLabel label_4; private JLabel label_5; JPanel centerpPanel; JButton button1, button2, button3; private JComboBox comboBox_1; Image icon = new ImageIcon("image/icon.png").getImage(); private JTextField ISBNfiled; private JTextField publishField; private JTextField publishDateField; private JTextField publishTime; private JTextField unitPriceField; private JTextField bookNameField; private JTextField authorFiled; public static void main(String[] args) { new AddBook(); } public AddBook() { setTitle("添加圖書信息"); setSize(555, 333); setIconImage(icon); setLocationRelativeTo(null); setTitle("添加圖書信息"); setSize(555, 334); setLocationRelativeTo(null); setVisible(true); centerpPanel = new JPanel(); centerpPanel.setLayout(null); JLabel label1 = new JLabel("ISBN:"); label1.setFont(new Font("宋體", Font.PLAIN, 20)); label1.setBounds(71, 26, 95, 42); centerpPanel.add(label1); ISBNfiled = new JTextField(); ISBNfiled.setBounds(120, 30, 110, 30); centerpPanel.add(ISBNfiled); ISBNfiled.setColumns(10); JLabel label = new JLabel("類 別:"); label.setFont(new Font("宋體", Font.PLAIN, 20)); label.setBounds(306, 30, 116, 35); centerpPanel.add(label); label_1 = new JLabel("書 名:"); label_1.setFont(new Font("宋體", Font.PLAIN, 20)); label_1.setBounds(50, 75, 154, 50); centerpPanel.add(label_1); lbll = new JLabel("作 者:"); lbll.setFont(new Font("宋體", Font.PLAIN, 20)); lbll.setBounds(306, 75, 137, 50); centerpPanel.add(lbll); label_2 = new JLabel("出版社:"); label_2.setFont(new Font("宋體", Font.PLAIN, 20)); label_2.setBounds(50, 130, 154, 50); centerpPanel.add(label_2); label_3 = new JLabel("出版日期:"); label_3.setFont(new Font("宋體", Font.PLAIN, 20)); label_3.setBounds(285, 135, 137, 50); centerpPanel.add(label_3); publishField = new JTextField(); publishField.setColumns(10); publishField.setBounds(120, 143, 110, 30); centerpPanel.add(publishField); publishDateField = new JTextField(); publishDateField.setColumns(10); publishDateField.setBounds(380, 143, 120, 30); centerpPanel.add(publishDateField); label_4 = new JLabel("印刷次數:"); label_4.setFont(new Font("宋體", Font.PLAIN, 20)); label_4.setBounds(28, 190, 154, 50); centerpPanel.add(label_4); publishTime = new JTextField(); publishTime.setColumns(10); publishTime.setBounds(120, 203, 110, 30); centerpPanel.add(publishTime); label_5 = new JLabel("單 價:"); label_5.setFont(new Font("宋體", Font.PLAIN, 20)); label_5.setBounds(305, 194, 84, 42); centerpPanel.add(label_5); unitPriceField = new JTextField(); unitPriceField.setColumns(10); unitPriceField.setBounds(380, 203, 120, 30); centerpPanel.add(unitPriceField); getContentPane().add(centerpPanel, BorderLayout.CENTER); bookNameField = new JTextField(); bookNameField.setColumns(10); bookNameField.setBounds(120, 86, 110, 30); centerpPanel.add(bookNameField); authorFiled = new JTextField(); authorFiled.setColumns(10); authorFiled.setBounds(380, 86, 120, 30); centerpPanel.add(authorFiled); List<BookType> rs = BookTypeDao.selectBookType(); Iterator<BookType> iterator = rs.iterator(); String[] AllTypeName = new String[rs.size()]; int i = 0; while (iterator.hasNext()) { String typename = iterator.next().getTypename(); AllTypeName[i] = typename; i++; } comboBox_1 = new JComboBox(AllTypeName); comboBox_1.setBounds(380, 30, 120, 30); comboBox_1.setFont(new Font("宋體", Font.PLAIN, 16)); centerpPanel.add(comboBox_1); ButtonGroup group = new ButtonGroup(); JPanel panel2 = new JPanel(); panel2.setLayout(new FlowLayout()); button1 = new JButton("添加"); button1.setFont(new Font("宋體", Font.PLAIN, 20)); button2 = new JButton("關閉"); button2.setFont(new Font("宋體", Font.PLAIN, 20)); button3 = new JButton("重置"); button3.setFont(new Font("宋體", Font.PLAIN, 20)); panel2.add(button1); panel2.add(button3); panel2.add(button2); button1.addActionListener(this); button2.addActionListener(this); button3.addActionListener(this); getContentPane().add(panel2, BorderLayout.SOUTH); setVisible(true); } @SuppressWarnings("unused") public void actionPerformed(ActionEvent e) { if (e.getSource() == button1) { String Isbn = ISBNfiled.getText().toString(); String bookname = bookNameField.getText(); String author = authorFiled.getText(); String selectType = comboBox_1.getSelectedItem().toString(); String publish = publishField.getText(); String publishdate = publishDateField.getText(); String time = publishTime.getText().trim(); String price = unitPriceField.getText().trim(); if (Isbn.equals("") || bookname.equals("") || author.equals("") || selectType.equals("") || publish.equals("") || publishdate.equals("") || time.equals("") || price.equals("")) { JOptionPane.showMessageDialog(this, "請輸入完整信息"); return; } else { BookType bt = new BookType(); Book book = new Book(); book.setISBN(Isbn); book.setBookname(bookname); book.setAuthor(author); bt.setTypename(selectType); int m = 0; List<BookType> list = BookTypeDao .selectIdByTypename(selectType); Iterator<BookType> ite = list.iterator(); while (ite.hasNext()) { m = ite.next().getId(); } book.setTypeid(m + ""); book.setPublish(publish); try { book.setPublishtime(Integer.parseInt(time)); book.setUnitprice(Integer.parseInt(price)); } catch (NumberFormatException e1) { e1.printStackTrace(); } SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date regDate = null; try { regDate =sdf.parse(publishdate); } catch (ParseException e1) { JOptionPane.showMessageDialog(this, "請輸入正確的日期"); publishDateField.setText(""); } book.setPublishdate(regDate); if(regDate==null) return; if (BookDao.selectBookByISBN(Isbn) != null) { JOptionPane.showMessageDialog(this, "該ISBN編號已存在"); return; } else if (BookDao.insertBook(book) != 0) { JOptionPane.showMessageDialog(this, "添加成功"); } else JOptionPane.showMessageDialog(this, "添加失敗"); } } if (e.getSource() == button3) { ISBNfiled.setText(""); bookNameField.setText(""); authorFiled.setText(""); publishField.setText(""); publishDateField.setText(""); publishTime.setText(""); unitPriceField.setText(""); } if (e.getSource() == button2) { dispose(); new MainWindow(); } } }
圖書查詢與修改源代碼:BookSelectandModify.java
package pdsu.bbm.view; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Font; import java.awt.GridLayout; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.Iterator; import java.util.List; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTabbedPane; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.SwingConstants; import javax.swing.table.TableModel; import pdsu.bbm.dao.BookDao; import pdsu.bbm.dao.BookTypeDao; import pdsu.bbm.model.Book; import pdsu.bbm.model.BookType; public class BookSelectandModify extends JFrame implements ActionListener { // 設置序列號 private static final long serialVersionUID = 1L; private JTabbedPane jtabbedPane; // 標簽面板 // selectJP查詢面板 select_conditionJP下拉列表面板 select_resultJP結果按鈕面板updateJP修改面板 // updateJP修改面板,bookJP中間面板 buttonJP2按鈕面板 private JPanel selectJP, select_conditionJP, select_resultJP, buttonJP1, bookJP, updateJP, buttonJP2;// private JTextField selectJTF, ISBNJTF, booknameJTF, authorJTF, printtimeJTF, publishJTF, publishdateJTF, unitpriceJTF; private JLabel ISBNJL, booknameJL, authorJL, categoryJL, printtimeJL, publishJL, publishdateJL, unitpriceJL; // 重點! private JTable jtable;// 定義表格 private JComboBox choiceJCB, booktypeJCB; private JScrollPane jscrollPane; private JButton selectJB, exitJB, updateJB, resetJB;// 查詢按鈕,退出按鈕,修改按鈕,關閉按鈕 private TableModel getSelect; private String[] title = { "ISBN", "圖書序列", "圖書名稱", "作者", " 出版社 ", "出版日期", "印刷次數", "單價" }; Image icon = new ImageIcon("image/icon.png").getImage(); public BookSelectandModify() { super(); setIconImage(icon); setTitle("圖書查詢與修改"); setBounds(100, 100, 555, 400); setLocationRelativeTo(null); // JTabbedPane選項卡 jtabbedPane = new JTabbedPane(); add(jtabbedPane); selectJP = new JPanel(); selectJP.setLayout(new BorderLayout()); jtabbedPane.add("圖書信息查詢", selectJP); // 查詢條件面板 select_conditionJP = new JPanel(); choiceJCB = new JComboBox(); String array[] = { "ISBN", "圖書名稱", "圖書序列", "作者", "出版社","所有" };// 設置為一維數組 for (int i = 0; i < array.length; i++) { choiceJCB.addItem(array[i]); } select_conditionJP.add(choiceJCB); selectJTF = new JTextField(20); select_conditionJP.add(selectJTF); selectJP.add(select_conditionJP, BorderLayout.NORTH); // 查詢結果面板 select_resultJP = new JPanel(); BookDao dao = new BookDao(); List<Book> list = dao.selectBook(); jtable = new JTable(getSelect(list), title); // !設置表格大小不變 jtable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); // !設置列寬 jtable.getColumnModel().getColumn(4).setPreferredWidth(175); jscrollPane = new JScrollPane(jtable);// 把表格加入滾動面板 // 顯示滾動面板邊框 jscrollPane.setPreferredSize(new Dimension(450, 170)); select_resultJP.add(jscrollPane); selectJP.add(select_resultJP, BorderLayout.CENTER); // 查詢按鈕面板 buttonJP1 = new JPanel(); selectJB = new JButton("查詢"); selectJB.setFont(new Font("宋體", Font.PLAIN, 20)); selectJB.addActionListener(this); buttonJP1.add(selectJB); exitJB = new JButton("退出"); exitJB.setFont(new Font("宋體", Font.PLAIN, 20)); exitJB.addActionListener(this); buttonJP1.add(exitJB); selectJP.add(buttonJP1, BorderLayout.SOUTH); // 信息修改頁面 updateJP = new JPanel(); updateJP.setLayout(new BorderLayout()); jtabbedPane.addTab("圖書信息修改", updateJP); bookJP = new JPanel(); final GridLayout gridLayout = new GridLayout(8, 2); // 設置邊框間的距離 gridLayout.setVgap(8); gridLayout.setHgap(8); bookJP.setLayout(gridLayout); ISBNJL = new JLabel("ISBN:"); ISBNJL.setFont(new Font("宋體",Font.PLAIN,20)); ISBNJL.setHorizontalAlignment(SwingConstants.CENTER); bookJP.add(ISBNJL); ISBNJTF = new JTextField(20); ISBNJTF.addActionListener(this); bookJP.add(ISBNJTF); categoryJL = new JLabel("類 別:"); categoryJL.setFont(new Font("宋體",Font.PLAIN,20)); categoryJL.setHorizontalAlignment(SwingConstants.CENTER); bookJP.add(categoryJL); // 下拉列表 List<BookType> list1 = BookTypeDao.selectBookType(); Iterator<BookType> it = list1.iterator(); String ty[] = new String[list1.size()]; int i = 0; booktypeJCB = new JComboBox(); while (it.hasNext()) { ty[i] = it.next().getTypename(); booktypeJCB.addItem(ty[i]); i++; } bookJP.add(booktypeJCB); booknameJL = new JLabel("書 名:"); booknameJL.setFont(new Font("宋體",Font.PLAIN,20)); booknameJL.setHorizontalAlignment(SwingConstants.CENTER); bookJP.add(booknameJL); booknameJTF = new JTextField(); booknameJTF.setColumns(20); bookJP.add(booknameJTF); authorJL = new JLabel("作 者:"); authorJL.setFont(new Font("宋體",Font.PLAIN,20)); authorJL.setHorizontalAlignment(SwingConstants.CENTER); bookJP.add(authorJL); authorJTF = new JTextField(); authorJTF.setColumns(20); bookJP.add(authorJTF); publishJL = new JLabel("出版社:"); publishJL.setFont(new Font("宋體",Font.PLAIN,20)); publishJL.setHorizontalAlignment(SwingConstants.CENTER); bookJP.add(publishJL); publishJTF = new JTextField(); bookJP.add(publishJTF); publishdateJL = new JLabel("出版日期:"); publishdateJL.setFont(new Font("宋體",Font.PLAIN,20)); publishdateJL.setHorizontalAlignment(SwingConstants.CENTER); bookJP.add(publishdateJL); publishdateJTF = new JTextField(); publishdateJTF.setHorizontalAlignment(SwingConstants.LEFT); bookJP.add(publishdateJTF); printtimeJL = new JLabel("印刷次數:"); printtimeJL.setFont(new Font("宋體",Font.PLAIN,20)); printtimeJL.setHorizontalAlignment(SwingConstants.CENTER); bookJP.add(printtimeJL); printtimeJTF = new JTextField(); bookJP.add(printtimeJTF); unitpriceJL = new JLabel("單 價:"); unitpriceJL.setFont(new Font("宋體",Font.PLAIN,20)); unitpriceJL.setHorizontalAlignment(SwingConstants.CENTER); bookJP.add(unitpriceJL); unitpriceJTF = new JTextField(); bookJP.add(unitpriceJTF); // 按鈕面板 // 按鈕面板設計 buttonJP2 = new JPanel(); updateJB = new JButton("修改"); updateJB.setFont(new Font("宋體", Font.PLAIN, 20)); updateJB.addActionListener(this); resetJB = new JButton("關閉"); resetJB.setFont(new Font("宋體", Font.PLAIN, 20)); resetJB.addActionListener(this); buttonJP2.add(updateJB); buttonJP2.add(resetJB); updateJP.add(bookJP, BorderLayout.CENTER); updateJP.add(buttonJP2, BorderLayout.SOUTH); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } // 將查詢表格加入面板 private Object[][] getSelect(List<Book> list) { // TODO Auto-generated method stub Object[][] objects = new Object[list.size()][title.length]; for (int i = 0; i < list.size(); i++) { Book book = list.get(i); objects[i][0] = book.getISBN(); objects[i][1] = book.getTypeid();// 圖書序列 objects[i][2] = book.getBookname();// 圖書名稱 objects[i][3] = book.getAuthor();// 圖書作者 objects[i][4] = book.getPublish();// 出版社 objects[i][5] = book.getPublishdate();// 出版日期 objects[i][6] = book.getPublishtime();// 印刷次數 objects[i][7] = book.getUnitprice();// 單價 } return objects; } public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getSource()==ISBNJTF){ String isbn=ISBNJTF.getText(); String typename=null; String id=BookDao.selectBookByISBN(isbn).getTypeid(); typename=BookTypeDao.selectById(id); System.out.println(typename); booktypeJCB.setSelectedItem(typename); booknameJTF.setText(BookDao.selectBookByISBN(isbn).getBookname()); authorJTF.setText(BookDao.selectBookByISBN(isbn).getAuthor()); publishJTF.setText(BookDao.selectBookByISBN(isbn).getPublish()); publishdateJTF.setText(BookDao.selectBookByISBN(isbn).getPublishdate()+""); printtimeJTF.setText(BookDao.selectBookByISBN(isbn).getPublishtime()+""); unitpriceJTF.setText(BookDao.selectBookByISBN(isbn).getUnitprice()+""); } if (e.getSource() == selectJB) {// 按照ISBN編碼查找 int r = choiceJCB.getSelectedIndex(); if (r == 0) { String name = selectJTF.getText().trim(); // 強制轉換為線性表類型 List<Book> list = new ArrayList<Book>(); Book book = BookDao.selectBookByISBN(name); if (book == null) { JOptionPane.showMessageDialog(this, "該編碼不存在!"); } else { list.add(book); Object[][] data = getSelect(list); jtable = new JTable(data, title); jtable.getColumnModel().getColumn(4).setPreferredWidth(175); jtable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); jscrollPane.setViewportView(jtable); } } if (r == 1) {// 根據圖書名稱查詢信息 String name = selectJTF.getText().toString(); List<Book> list = BookDao.selectBookByName(name); // 判斷線性表是否為空 if (list == null || list.size() == 0) { JOptionPane.showMessageDialog(this, "該圖書不存在!"); } else { Object[][] data = getSelect(list); jtable = new JTable(data, title); tianjia(); } } if (r == 2) {// 根據圖書序列查詢信息 String name = selectJTF.getText().toString(); List<Book> list = BookDao.selectBookByType(name); // 判斷線性表是否為空 if (list == null || list.size() == 0) { JOptionPane.showMessageDialog(this, "該圖書不存在!"); } else { Object[][] data = getSelect(list); jtable = new JTable(data, title); tianjia(); } } if (r == 3) { // 根據作者進行查找 String name = selectJTF.getText().toString(); // 根據作者進行查找 List<Book> list = BookDao.selectBookByAuthor(name); // 判斷線性表是否為空 if (list == null || list.size() == 0) { JOptionPane.showMessageDialog(this, "該圖書不存在!"); } else { Object[][] data = getSelect(list); jtable = new JTable(data, title); tianjia(); } } if (r == 4) {// 根據出版社進行查找 String name = selectJTF.getText().toString(); List<Book> list = BookDao.selectBookByPublish(name); if (list == null || list.size() == 0) { JOptionPane.showMessageDialog(this, "該圖書不存在!"); } else { Object[][] data = getSelect(list); jtable = new JTable(data, title); tianjia(); } } if(r==5){ List<Book> list =BookDao.selectBook(); } } if (e.getSource() == updateJB) { String ISBN = ISBNJTF.getText().trim(); String typename = ((String) booktypeJCB.getSelectedItem()).trim(); String id = BookTypeDao.selectByTypename(typename); String bookname = booknameJTF.getText(); String author = authorJTF.getText(); String publish = publishJTF.getText(); String publishdate = publishdateJTF.getText(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date regDate = null; try { regDate = sdf.parse(publishdate); } catch (Exception e2) { e2.printStackTrace(); JOptionPane.showMessageDialog(this, "請輸入正確的日期格式!"); } if (regDate == null) return; int publishtime = Integer.parseInt(printtimeJTF.getText()); int unitprice = Integer.parseInt(unitpriceJTF.getText()); // 封裝對象 Book book = new Book(); book.setISBN(ISBN); book.setTypeid(id); book.setBookname(bookname); book.setAuthor(author); book.setPublish(publish); book.setPublishdate(regDate); book.setPublishtime(publishtime); book.setUnitprice(unitprice); book.setTypename(typename); int result = BookDao.update(book); List<Book> list2 = new ArrayList<Book>(); list2.add(book); // System.out.println(list2); if (result == 1) { JOptionPane.showMessageDialog(this, "修改成功!"); BookDao bk=new BookDao(); List<Book> list =bk.selectBook(); jtable =new JTable(getSelect(list),title); tianjia(); } else { JOptionPane.showMessageDialog(this, "修改失敗!"); } } if (e.getSource() == resetJB) { dispose(); new MainWindow(); } if (e.getSource() == exitJB) { dispose(); new MainWindow(); } } private void tianjia() { jtable.getColumnModel().getColumn(4).setPreferredWidth(175); jtable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); jscrollPane.setViewportView(jtable); } public static void main(String[] args) { new BookSelectandModify(); } }
以上是“使用java實現圖書信息管理代方法”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。