Book information management is similar to reader information management. It includes two parts: book addition and book query and modification. Book information addition requires the correct input of eight information of the book, and the ISBN number is unique. During the implementation process, ISBN should be used as the primary key. Let’s see the implementation process:
Books in the database:
The process of adding books:
Database content added:
Query and modification of books: The query process can be queried according to different conditions, such as ISBN, author, publisher, book name, etc., and all books are displayed in the list by default. After entering the query conditions, the query results are displayed in the table.
This module uses the tag panel, which can easily query the required information. When the modification process enters the ISBN number and press Enter to display all book information below, then all content will be updated after modifying the book information.
Book Query:
Modify book information:
Add the source code of the book as follows: 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("Add book information"); setSize(555, 333); setIconImage(icon); setLocationRelativeTo(null); setTitle("Add book information"); 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("Category:"); label.setFont(new Font("Zongyi", Font.PLAIN, 20)); label.setBounds(306, 30, 116, 35); centerpPanel.add(label); label_1 = new JLabel("Book title:"); label_1.setFont(new Font("宋体", Font.PLAIN, 20)); label_1.setBounds(50, 75, 154, 50); centerpPanel.add(label_1); lbll = new JLabel("Author:"); lbll.setFont(new Font("宋体", Font.PLAIN, 20)); lbll.setBounds(306, 75, 137, 50); centerpPanel.add(lbll); label_2 = new JLabel("Publisher:"); 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("Number of printing:"); 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("Add"); button1.setFont(new Font("宋体", Font.PLAIN, 20)); button2 = new JButton("宋体", Font.PLAIN, 20)); button3 = new JButton("Reset"); 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, "Please enter full information"); 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> item = list.iterator(); while (ite.hasNext()) { m = item.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, "Please enter the correct date"); publishDateField.setText(""); } book.setPublishdate(regDate); if(regDate==null) return; if (BookDao.selectBookByISBN(Isbn) != null) { JOptionPane.showMessageDialog(this, "This ISBN number already exists"); return; } else if (BookDao.insertBook(book) != 0) { JOptionPane.showMessageDialog(this, "Added successfully"); } else JOptionPane.showMessageDialog(this, "Add failed"); } } if (e.getSource() == button3) { ISBNfiled.setText(""); bookNameField.setText(""); authorField.setText(""); publishField.setText(""); publishDateField.setText(""); publishTime.setText(""); unitPriceField.setText(""); unitPriceField.setText(""); } if (e.getSource() == button2) { dispose(); new MainWindow(); } } } Book query and modify source code: 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 { // Set the serial number private static final long serialVersionUID = 1L; private JTabbedPane jtabbedPane; // Tag panel// selectJP query panel select_conditionJP drop-down list panel select_resultJP result button panel updateJP modification panel// updateJP modification panel, bookJP middle panel buttonJP2 button panel 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; // Focus! private JTable jtable;// Define table private JComboBox choiceJCB, booktypeJCB; private JScrollPane jscrollPane; private JButton selectJB, exitJB, updateJB, resetJB;// Query button, exit button, modify button, close button private TableModel getSelect; private String[] title = { "ISBN", "Book Sequence", "Book Name", "Author", "Publisher", "Date of Publication", "Number of Prints", "Unit Price" }; Image icon = new ImageIcon("image/icon.png").getImage(); public BookSelectandModify() { super(); setIconImage(icon); setTitle("Book Query and Modification"); setBounds(100, 100, 555, 400); setLocationRelativeTo(null); // JTabbedPane TabjtabbedPane = new JTabbedPane(); add(jtabbedPane); selectJP = new JPanel(); selectJP.setLayout(new BorderLayout()); jtabbedPane.add("Book Information Query", selectJP); // Query Condition Panel select_conditionJP = new JPanel(); choiceJCB = new JComboBox(); String array[] = { "ISBN", "Book Name", "Book Sequence", "Author", "Publisher", "All" };// Set to a one-dimensional array 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); // Query result panel select_resultJP = new JPanel(); BookDao dao = new BookDao(); List<Book> list = dao.selectBook(); jtable = new JTable(getSelect(list), title); // !Set the table size unchanged jtable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); // !Set the column width jtable.getColumnModel().getColumn(4).setPreferredWidth(175); jscrollPane = new JScrollPane(jtable);// Add the table to the scroll panel// Show the scroll panel border jscrollPane.setPreferredSize(new Dimension(450, 170)); select_resultJP.add(jscrollPane); selectJP.add(select_resultJP, BorderLayout.CENTER); // Query button panel buttonJP1 = new JPanel(); selectJB = new JButton("Query"); selectJB.setFont(new Font("Zongyi", Font.PLAIN, 20)); selectJB.addActionListener(this); buttonJP1.add(selectJB); exitJB = new JButton("Exit"); exitJB.setFont(new Font("Zongyi", Font.PLAIN, 20)); exitJB.addActionListener(this); buttonJP1.add(exitJB); selectJP.add(buttonJP1, BorderLayout.SOUTH); // Information modification page updateJP = new JPanel(); updateJP.setLayout(new BorderLayout()); jtabbedPane.addTab("Book Information Modification", updateJP); bookJP = new JPanel(); final GridLayout gridLayout = new GridLayout(8, 2); // Set the distance between borders 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("Category:"); categoryJL.setFont(new Font("宋体",Font.PLAIN,20)); categoryJL.setHorizontalAlignment(SwingConstants.CENTER); bookJP.add(categoryJL); // drop-down list 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("Book title:"); 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("Author:"); 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("Publisher:"); publishJL.setFont(new Font("宋体",Font.PLAIN,20)); publishJL.setHorizontalAlignment(SwingConstants.CENTER); bookJP.add(publishJL); publishJTF = new JTextField(); bookJP.add(publishJTF); publishdateJL = new JLabel("Published Date:"); 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("Number of printing:"); printtimeJL.setFont(new Font("宋体",Font.PLAIN,20)); printtimeJL.setHorizontalAlignment(SwingConstants.CENTER); bookJP.add(printtimeJL); printtimeJTF = new JTextField(); bookJP.add(printtimeJTF); unitpriceJL = new JLabel("Unit Price:"); unitpriceJL.setFont(new Font("宋体",Font.PLAIN,20)); unitpriceJL.setHorizontalAlignment(SwingConstants.CENTER); bookJP.add(unitpriceJL); unitpriceJTF = new JTextField(); bookJP.add(unitpriceJTF); // Button panel// Button panel design buttonJP2 = new JPanel(); updateJB = new JButton("Modify"); updateJB.setFont(new Font("Songyi", Font.PLAIN, 20)); updateJB.addActionListener(this); resetJB = new JButton("Close"); resetJB.setFont(new Font("Zongyi", 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); } // Add the query table to the panel 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[1] = book.getTypeid();// Book sequence objects[i][2] = book.getBookname();// Book name objects[i][3] = book.getAuthor();// Book author objects[i][4] = book.getPublish();// Publisher objects[i][5] = book.getPublishdate();// Publication date objects[i][6] = book.getPublishtime();// Number of printing objects[i][7] = book.getUnitprice();// Unit price} 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()); publishdateJTF.setText(BookDao.selectBookByISBN(isbn).getPublishdate()+""); printtimeJTF.setText(BookDao.selectBookByISBN(isbn).getPublishtime()+""); unitpriceJTF.setText(BookDao.selectBookByISBN(isbn).getUnitprice()+""); } if (e.getSource() == selectJB) {// Find int r = choiceJCB.getSelectedIndex(); if (r == 0) { String name = selectJTF.getText().trim(); // Cast to linear table type List<Book> list = new ArrayList<Book>(); Book book = BookDao.selectBookByISBN(name); if (book == null) { JOptionPane.showMessageDialog(this, "this encoding does not exist!"); } 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) {// Query information based on the book name String name = selectJTF.getText().toString(); List<Book> list = BookDao.selectBookByName(name); // Determine whether the linear table is empty if (list == null || list.size() == 0) { JOptionPane.showMessageDialog(this, "The book does not exist!"); } else { Object[][] data = getSelect(list); jtable = new JTable(data, title); tianjia(); } } if (r == 2) {// Query information based on book sequence String name = selectJTF.getText().toString(); List<Book> list = BookDao.selectBookByType(name); // Determine whether the linear table is empty if (list == null || list.size() == 0) { JOptionPane.showMessageDialog(this, "The book does not exist!"); } else { Object[][] data = getSelect(list); jtable = new JTable(data, title); tianjia(); } } if (r == 3) { // Find String according to the author name = selectJTF.getText().toString(); // Search for List<Book> list = BookDao.selectBookByAuthor(name); // Check whether the linear table is empty if (list == null || list.size() == 0) { JOptionPane.showMessageDialog(this, "this book does not exist!"); } else { Object[][] data = getSelect(list); jtable = new JTable(data, title); tianjia(); } } if (r == 4) {// Search for String according to the publisher name = selectJTF.getText().toString(); List<Book> list = BookDao.selectBookByPublish(name); if (list == null || list.size() == 0) { JOptionPane.showMessageDialog(this, "this book does not exist!"); } 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, "Please enter the correct date format!"); } if (regDate == null) return; int publishtime = Integer.parseInt(printtimeJTF.getText()); int unitprice = Integer.parseInt(unitpriceJTF.getText()); // Encapsulate object 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, "Modified successfully!"); BookDao bk=new BookDao(); List<Book> list =bk.selectBook(); jtable =new JTable(getSelect(list),title); tianjia(); } else { JOptionPane.showMessageDialog(this, "Modified failed!"); } } 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(); } }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.