運行效果:
注冊登陸界面

注冊存在的賬戶時
登陸之后主界面如下

點擊圖書管理-圖書更新界面如下

圖書列表

項目說明:
由於平時工作比較忙,也沒時間寫,可是我在公眾號后台看見好多小伙伴討論,我就抽時間寫個當作參考。本系統界面我個人就從簡設計了,本來打算使用windowbuilder插件設計的,可想到使用windowbuilder插件之后導致代碼冗余,會影響到代碼可讀性,可能對小白不友好。雖然界面設計簡單,但是功能上我會盡量想着寫全,當然對於頁面你可以增加自己的設計,比如增加一個背景圖片等,網上都有指導,對於界面美觀度從簡了。
關鍵代碼:
用戶登陸
package bookmanage.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import bookmanage.model.User; import bookmanage.utils.DbUtil; /** * @Description 連接數據庫工具類 * @author com.javayihao.top */ public class UserDao { /** * 根據用戶賬號查詢用戶 * * @param accout * 入參:用戶賬號 * @return 查找的用戶 */ public User getUserByAccout(String accout) { Connection connection = DbUtil.getConnection(); String sql = "select accout,pass from t_user where accout=?"; try { PreparedStatement ps = connection.prepareStatement(sql); ps.setString(1, accout); ResultSet rs = ps.executeQuery(); if (rs.next()) {// 存在用戶,封裝用戶返回 User user = new User(rs.getString("accout"), rs.getString("pass")); DbUtil.close(connection, ps);// 關閉連接 return user; } } catch (SQLException e) { e.printStackTrace(); } return null; } public boolean insertUser(User user) { Connection connection = DbUtil.getConnection();// 獲得數據庫連接對象 String sql = "insert into t_user(accout,pass)values(?,?)"; try { PreparedStatement ps = connection.prepareStatement(sql); ps.setString(1, user.getAccout()); ps.setString(2, user.getPass()); if (!ps.execute()) {// 成功 DbUtil.close(connection, ps);// 關閉連接 return true; } } catch (SQLException e) { e.printStackTrace(); } return false;// 失敗 } }
圖書更新
package bookmanage.view; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import bookmanage.dao.BookDao; import bookmanage.model.Book; /** * @Description 用於圖書增刪改查面板 * @author com.javayihao.top */ public class CrudBookPanel extends JPanel implements ActionListener { // 定義首頁按鈕、圖書列表按鈕、 其他功能按鈕,增加圖書、刪除圖書、修改圖書、查詢圖書 private JButton addBtn, deleteBtn, updateBtn, findBtn; // 定義標簽 底部信息標簽、 圖書編號、 圖書名稱、 圖書數量、 圖書價格 private JLabel idLabel, nameLabel, numLabel, priceLabel; // 定義圖書編號、 名稱、 數量、 價格文本框 private JTextField idJTextField, nameJTextField, numJTextField, priceJTextField; // 定義文本對象 private String bookIdText; private String bookNameText; private String bookNumText; private String bookPriceText; // 圖書數量和價格 private Integer numBook; private Float priceBook; // 定義對象BookDao private BookDao bookDao; public CrudBookPanel() { bookDao = new BookDao();//實例化圖書操作對象 // 實例化增刪改查按鈕 addBtn = new JButton("增加圖書"); addBtn.addActionListener(this);// 設置圖書增加按鈕監聽 addBtn.setActionCommand("addbook"); deleteBtn = new JButton("刪除圖書"); deleteBtn.addActionListener(this);// 設置圖書刪除按鈕監聽 deleteBtn.setActionCommand("deletebook"); updateBtn = new JButton("修改圖書"); updateBtn.addActionListener(this);// 設置圖書修改按鈕監聽 updateBtn.setActionCommand("updatebook"); findBtn = new JButton("查詢圖書"); findBtn.addActionListener(this);// 設置圖書查詢按鈕監聽 findBtn.setActionCommand("findbook"); // 實例化圖書編號 名稱 數量 價格標簽 idLabel = new JLabel("圖書編號"); nameLabel = new JLabel("圖書名稱"); priceLabel = new JLabel("圖書價格"); numLabel = new JLabel("圖書數量"); // 實例化文本框 idJTextField = new JTextField(12); nameJTextField = new JTextField(12); numJTextField = new JTextField(12); priceJTextField = new JTextField(12); this.setLayout(new GridLayout(6, 2, 2, 2)); // 給增刪改查面板添加圖書編號 名稱 數量 價格標簽以及文本框 this.add(idLabel); this.add(idJTextField); this.add(nameLabel); this.add(nameJTextField); this.add(priceLabel); this.add(priceJTextField); this.add(numLabel); this.add(numJTextField); // 給增刪改查面板添加圖書編號 名稱 數量 價格按鈕 this.add(addBtn); this.add(deleteBtn); this.add(updateBtn); this.add(findBtn); } @Override public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("addbook")) {// 添加圖書 addbook(); } else if (e.getActionCommand().equals("deletebook")) {// 刪除圖書 deleteBook(); } else if (e.getActionCommand().equals("updatebook")) {// 修改圖書 updateBook(); } else if (e.getActionCommand().equals("findbook")) {// 查詢圖書 findBook(); } } /** * 查詢圖書 */ private void findBook() { bookIdText = idJTextField.getText().trim().toString(); Book book = bookDao.getBookById(bookIdText); if (bookIdText.equals("")) { JOptionPane.showMessageDialog(this, "圖書編號不能為空"); } else if (book != null) {// 當前輸入的圖書編號存在 try { nameJTextField.setText(book.getName());// 填充圖書名稱文本框 numJTextField.setText(book.getNum() + "");// 將數字類型轉成字符串並填充文本框 priceJTextField.setText(book.getPrice() + "");// 將數字類型轉成字符串並填充文本框 } catch (Exception e) { JOptionPane.showMessageDialog(this, "圖書查詢異常 請聯系管理員"); } } else { JOptionPane.showMessageDialog(this, "圖書不存在"); } } /** * 修改圖書 */ private void updateBook() { bookIdText = idJTextField.getText().trim().toString(); bookNameText = nameJTextField.getText().trim().toString(); bookNumText = numJTextField.getText().trim().toString(); bookPriceText = priceJTextField.getText().trim().toString(); if (bookIdText.equals("")) { JOptionPane.showMessageDialog(this, "圖書編號不能為空"); } else if (bookNameText.equals("")) { JOptionPane.showMessageDialog(this, "圖書名稱不能為空"); } else if (bookNumText.equals("")) { JOptionPane.showMessageDialog(this, "圖書數量不能為空"); } else if (bookPriceText.equals("")) { JOptionPane.showMessageDialog(this, "圖書價格不能為空"); } else { // 將圖書數量和圖書價格轉成對應的數字類型 if (bookDao.getBookById(bookIdText) == null) {// 圖書不存在 JOptionPane.showMessageDialog(this, "輸入正確的圖書編號"); } else { try { numBook = Integer.parseInt(bookNumText); priceBook = Float.parseFloat(bookPriceText); bookDao.updateBook(new Book(bookIdText, bookNameText, numBook, priceBook)); JOptionPane.showMessageDialog(this, "圖書修改成功"); } catch (Exception e) { JOptionPane.showMessageDialog(this, "輸入正確的圖書數量和價格"); e.printStackTrace(); } } } } /** * 刪除圖書 */ private void deleteBook() { bookIdText = idJTextField.getText().trim().toString(); if (bookIdText.equals("")) { JOptionPane.showMessageDialog(this, "圖書編號不能為空"); } else if (bookDao.getBookById(bookIdText) != null) {// 當前輸入的圖書編號是否存在 try { bookDao.deleteBootByid(bookIdText); JOptionPane.showMessageDialog(this, "圖書刪除成功"); } catch (Exception e) { JOptionPane.showMessageDialog(this, "圖書刪除異常 請聯系管理員"); } } else { JOptionPane.showMessageDialog(this, "圖書不存在"); } } /** * 增加圖書 */ private void addbook() { bookIdText = idJTextField.getText().trim().toString(); bookNameText = nameJTextField.getText().trim().toString(); bookNumText = numJTextField.getText().trim().toString(); bookPriceText = priceJTextField.getText().trim().toString(); if (bookIdText.equals("")) { JOptionPane.showMessageDialog(this, "圖書編號不能為空"); } else if (bookNameText.equals("")) { JOptionPane.showMessageDialog(this, "圖書名稱不能為空"); } else if (bookNumText.equals("")) { JOptionPane.showMessageDialog(this, "圖書數量不能為空"); } else if (bookPriceText.equals("")) { JOptionPane.showMessageDialog(this, "圖書價格不能為空"); } else { // 將圖書數量和圖書價格轉成對應的數字類型 if (bookDao.getBookById(bookIdText) != null) {// 編號重復 JOptionPane.showMessageDialog(this, "圖書編號重復"); } else { 