主要就是数据库对应表中各对应的字段get、set方法
Eclipse技巧:
Shift + alt + s -> Generate Getters and Setters -> Select all -> Generate 自动生成set、get方法
Controller类(以BookAction为例)
package pers.cyz.controller; import java.util.List; import javax.swing.JTable; import javax.swing.JTextField; import pers.cyz.dao.BookDao; import pers.cyz.model.Book; /** * 图书信息行为控制类,包含增加图书、删除图书 * 、 修改图书、和初始化个人书库管理窗体表格 * * @author 1651200111 陈彦志 */ public class BookAction { /** * 初始化窗体表格 * @return * results */ @SuppressWarnings("rawtypes") public Object[][] initializTable(String[] columnNames) throws Exception{ BookDao bookDao = new BookDao(); List list = bookDao.query(); 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.getAuthor(); results[i][3] = book.getPrice(); results[i][4] = book.getISBN(); results[i][5] = book.getPublishHouse(); results[i][6] = book.getBookCategory(); String borrowerName = book.getBorrowerName(); if (borrowerName == null) { borrowerName = ""; results[i][7] = borrowerName; } else { results[i][7] = borrowerName; } String borrowerPhone = book.getBorrowerPhone(); if (borrowerPhone == null) { borrowerPhone = ""; results[i][8] = borrowerPhone; } else { results[i][8] = borrowerPhone; } } return results; } /** * 添加图书信息 */ public void addBookInformation (JTextField textFieldISBN, JTextField textFieldName ,JTextField textFieldPrice, JTextField textFieldAuthor, JTextField textFieldPublishedHouse , JTextField textFieldBookCategory, JTextField textFieldBorrowName , JTextField textFieldBorrowPhone) throws Exception { BookDao bookDao=new BookDao(); Book book=new Book(); book.setISBN(textFieldISBN.getText()); book.setBookName(textFieldName.getText()); float price = Float.parseFloat(textFieldPrice.getText()); book.setPrice(price); book.setAuthor(textFieldAuthor.getText()); book.setPublishHouse(textFieldPublishedHouse.getText()); book.setBookCategory(textFieldBookCategory.getText()); if (textFieldBorrowName.getText() == null ||textFieldBorrowName.getText() == "" ) { book.setBorrowerName(null); } else { book.setBorrowerName(textFieldBorrowName.getText()); } if (textFieldBorrowPhone.getText() == null || textFieldBorrowPhone.getText() == "") { book.setBorrowerPhone(null); } else { book.setBorrowerPhone(textFieldBorrowPhone.getText()); } //添加图书 bookDao.addBook(book); } /** * 删除图书信息 */ public void delBookInformation (JTable table) throws Exception { int selRow = table.getSelectedRow(); int ID = Integer.parseInt(table.getValueAt(selRow, 0).toString()); BookDao bookDao=new BookDao(); Book book=new Book(); book.setID(ID); // 删除图书信息 bookDao.delBook(ID); } /** * 修改图书信息 */ public void changeBookInformation (JTextField textFieldISBN, JTextField textFieldName ,JTextField textFieldPrice, JTextField textFieldAuthor, JTextField textFieldPublishedHouse , JTextField textFieldBookCategory, JTextField textFieldBorrowerName , JTextField textFieldBorrowerPhone, JTable table) throws Exception{ BookDao bookDao=new BookDao(); Book book=new Book(); int selRow = table.getSelectedRow(); int ID = Integer.parseInt(table.getValueAt(selRow, 0).toString()); book.setID(ID); book.setISBN(textFieldISBN.getText()); book.setBookName(textFieldName.getText()); book.setAuthor(textFieldAuthor.getText()); float price = Float.parseFloat(textFieldPrice.getText()); book.setPrice(price); book.setPublishHouse(textFieldPublishedHouse.getText()); book.setBookCategory(textFieldBookCategory.getText()); book.setBorrowerName(textFieldBorrowerName.getText()); book.setBorrowerPhone(textFieldBorrowerPhone.getText()); //修改图书 bookDao.changeBook(book); } }
util类(以DBUtil为例)
package pers.cyz.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; /** * 连接数据库类,包含一个对外提供获取数据库连接的方法 * * @author 1651200111 陈彦志 */ public class DBUtil { // 数据库连接路径 private static final String URL = "jdbc:mysql://127.0.0.1:3306/db_books?" + "useUnicode = true & serverTimezone = GMT" // MySQL在高版本需要指明是否进行SSL连接 + "& characterEncoding = utf8 & useSSL = false"; private static final String NAME = "root"; private static final String PASSWORD = "root"; private static Connection conn = null; // 静态代码块(将加载驱动、连接数据库放入静态块中) static{ try { // 加载驱动程序 Class.forName("com.mysql.cj.jdbc.Driver"); // 获取数据库的连接 conn = DriverManager.getConnection(URL, NAME, PASSWORD); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } // 对外提供一个方法来获取数据库连接 public static Connection getConnection(){ return conn; } }
util类(以BackgroundImage为例)
package pers.cyz.util; import java.awt.Container; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; /** * 设置背景图片类 * * @author 1651200111 陈彦志 */ public class BackgroundImage { public BackgroundImage(JFrame frame,Container container,String ImageName) { // 限定加载图片路径 ImageIcon icon= new ImageIcon("res/" + ImageName); final JLabel labelBackground = new JLabel(); ImageIcon iconBookManageSystemBackground = icon; labelBackground.setIcon(iconBookManageSystemBackground); // 设置label的大小 labelBackground.setBounds(0,0,iconBookManageSystemBackground.getIconWidth() ,iconBookManageSystemBackground.getIconHeight()); // 将背景图片标签放入桌面面板的最底层 frame.getLayeredPane().add(labelBackground,new Integer(Integer.MIN_VALUE)); // 将容器转换为面板设置为透明 JPanel panel = (JPanel)container; panel.setOpaque(false); } }
重点内容 :
将图片标签放在窗体底层面板,然后将窗体转化为容器,将容器面板设为透明,背景图片就设置好了,之后就可以直接在该容器中添加组件
将所有两个或两个以上类需要用到的代码段全部封装到了公共类。整体按照MVC三层架构组织
参考文章:http://1000zx.cn/article/204374.htm
参考文章:http://1000zx.cn/article/88326.htm
到此这篇关于Java+MySQL实现图书管理系统(完整代码)的文章就介绍到这了,更多相关java mysql图书管理系统内容请搜索