Java+MySQL 圖書管理系統

一,功能

管理員登錄
圖書借閱信息管理
圖書信息管理
管理員更改密碼
退出系統

二,工具

Eclipse Version: 2018-09 (4.9.0)
MySQL Workbench 8.0 CE
mysql-connector-java-8.0.13.jar

三、效果圖:

登錄界面:

主界面:

借閱書籍管理:

個人書庫管理:

更改密碼:

四、數據庫設計

1)圖書表

2)用戶表

兩個數據表間沒有關聯:

五、JAVA層次分析

(1)邏輯圖

(2)包結構,采用MVC三層架構組織各個模塊

六、主要Java代碼分析

Dao類(以BookDao為例)

package pers.cyz.dao;
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
 
import pers.cyz.model.Book;
import pers.cyz.util.DBUtil;
 
/**
 * 數據庫圖書表信息數據訪問對象類,包含增加圖書信息、刪除圖書信息
 * 、更新圖書信息、查詢圖書信息、查詢借閱信息和歸還圖書
 * 
 * @author 1651200111 陳彥志
 */
public class BookDao {
 
 
	/**
	 * 增加圖書信息
	 */
 public void addBook(Book book) throws Exception{
 	// 首先拿到數據庫的連接
 Connection con = DBUtil.getConnection();
 String sql="insert into tb_books"
 		// ISBN、書名、圖書價格、圖書作者、出版社
 		+ "(ISBN, book_name, book_price, book_author, published_house,"
 		// 分類號、借書人姓名、借書人電話、借書日期,已借天數
 		+ "book_category, borrower_name, borrower_phone) "
 		+ "values("
 		/*
 		 * 參數用?表示,相當於占位符,然後在對參數進行賦值。當真正執行時,
 		 * 這些參數會加載在SQL語句中,把SQL語句拼接完整才去執行。這樣就會減少對數據庫的操作
 		 */
 		+ "?,?,?,?,?,?,?,?)";
 /*
  * prepareStatement這個方法會將SQL語句加載到驅動程序conn集成程序中,
  * 但是並不直接執行,而是當它調用execute()方法的時候才真正執行;
  */
 PreparedStatement psmt = con.prepareStatement(sql);
 // 先對應SQL語句,給SQL語句傳遞參數
 psmt.setString(1, book.getISBN());
 psmt.setString(2, book.getBookName());
 psmt.setFloat(3, book.getPrice());
 psmt.setString(4, book.getAuthor());
 psmt.setString(5, book.getPublishHouse());
 psmt.setString(6, book.getBookCategory());
 
 if (book.getBorrowerName() == null || book.getBorrowerName() == "") {
 	psmt.setString(7, null);
 }
 else {
 	 psmt.setString(7, book.getBorrowerName());
 }
 
 if (book.getBorrowerPhone() == null || book.getBorrowerPhone() == "") {
 	 psmt.setString(8, null); 
 }
 else {
 	psmt.setString(8, book.getBorrowerPhone()); 
 }
 
 //執行SQL語句
 psmt.execute();
 
 }
	
 
 /**
 * 刪除圖書信息
 */
 public void delBook(int ID) throws SQLException{
 	 // 首先拿到數據庫的連接
  Connection con=DBUtil.getConnection();
  String sql="" + 
   "DELETE FROM tb_books "+  
   // 參數用?表示,相當於占位符
   "WHERE ID = ?";
  // 預編譯sql語句
  PreparedStatement psmt = con.prepareStatement(sql);
  // 先對應SQL語句,給SQL語句傳遞參數
  psmt.setInt(1, ID);
  // 執行SQL語句
  psmt.execute(); 
 }
	 
 
	/**
	 * 更新圖書信息
	 */
 public void changeBook(Book book) throws SQLException{
 	// 首先拿到數據庫的連接
 Connection con=DBUtil.getConnection();
 String sql="update tb_books "
 		+ "set ISBN = ?, book_name = ?, book_price = ?, book_author = ?"
  + ",published_house = ?, book_category = ?, borrower_name = ?, borrower_phone = ? "
  // 參數用?表示,相當於占位符 
 		+ "where ID = ?";
 // 預編譯sql語句
 PreparedStatement psmt = con.prepareStatement(sql);
 // 先對應SQL語句,給SQL語句傳遞參數
 psmt.setString(1, book.getISBN());
 psmt.setString(2, book.getBookName());
 psmt.setFloat(3, book.getPrice());
 psmt.setString(4, book.getAuthor());
 psmt.setString(5, book.getPublishHouse());
 psmt.setString(6, book.getBookCategory());
 if (book.getBorrowerName().equals("")) {
 	psmt.setString(7, null);
 }
 else {
 	 psmt.setString(7, book.getBorrowerName());
 }
 
 if (book.getBorrowerPhone().equals("")) {
 	psmt.setString(8, null);
 }
 else {
 	 psmt.setString(8, book.getBorrowerPhone());
 }
 psmt.setInt(9, book.getID());
 // 執行SQL語句
 psmt.execute(); 
 }
 
	 
 
	/**
	 * 查詢書籍信息
	 */
	public List<Book> query() throws Exception{	 
		Connection con = DBUtil.getConnection();	 
		Statement stmt = con.createStatement();	 
		ResultSet rs = stmt.executeQuery("select "
				// ISBN、書名、作者、圖書價格、出版社
				+ "ID, ISBN, book_name, book_author, book_price, published_house, "
				// 分類號、借書人姓名、借書人電話
				+ "book_category, borrower_name, borrower_phone "
				+ "from tb_books");	 
		List<Book> bookList = new ArrayList<Book>();	 
		Book book = null;	 
		// 如果對象中有數據,就會循環打印出來
		while (rs.next()){	  
			book = new Book();	 
			book.setID(rs.getInt("ID"));
			book.setISBN(rs.getString("ISBN"));
			book.setBookName(rs.getString("book_name"));	 
			book.setAuthor(rs.getString("book_author"));
			book.setPrice(rs.getFloat("book_price"));
			book.setPublishHouse(rs.getString("published_house"));
			book.setBookCategory(rs.getString("book_category"));
			book.setBorrowerName(rs.getString("borrower_name"));
			book.setBorrowerPhone(rs.getString("borrower_phone"));
			bookList.add(book);	 
		}	 
		return bookList;	 
	}
 
 
	/**
	 * 查詢借閱信息
	 * 
	 * @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()){	  
			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();
 String sql="UPDATE tb_books "
 		// ISBN、圖書名稱、作者、價格
 		+ "SET "
 		// 借書人姓名、借書人電話
 		+ "borrower_name = ?, borrower_phone = ? "
  // 參數用?表示,相當於占位符 
 		+ "WHERE ID = ?";
 // 預編譯sql語句
 PreparedStatement psmt = con.prepareStatement(sql);
 // 先對應SQL語句,給SQL語句傳遞參數
 psmt.setString(1, book.getBorrowerName());
 psmt.setString(2, book.getBorrowerPhone());
 psmt.setInt(3, book.getID());
 // 執行SQL語句
 psmt.execute(); 
 } 
}

重點內容 :

JDBC進行簡單的數據庫增刪改查

詳細參考:https://www.cnblogs.com/Qian123/p/5339164.html#_labelTop

Model類(以Book為例)

package pers.cyz.model;
 
/**
 * 圖書模型類,包含數據庫圖書表各對應的字段get、set方法
 * 
 * @author 1651200111 陳彥志
 */
public class Book {
	private int ID;
	// ISBN號
	private String ISBN;
	// 圖書名稱
	private String bookName;
	// 圖書價格
	private float price;
	// 圖書作者
	private String author;
	// 出版社
	private String publishedHouse;
	// 圖書分類號
	private String bookCategory;
	// 借書人姓名
	private String borrowerName;
	// 借書人電話
	private String borrowerPhone;
 
	/**
	 * 獲取ID
	 */
	public int getID() {
		return ID;
	}
	/**
	 * 設置ID
	 */
	public void setID(int iD) {
		ID = iD;
	}
	
	/**
	 * 獲取ISBN
	 */
	public String getISBN() {
		return ISBN;
	}
	/**
	 * 設置ISBN
	 */
	public void setISBN(String iSBN) {
		ISBN = iSBN;
	}
	
	
	/**
	 * 獲取圖書名稱
	 */
	public String getBookName() {
		return bookName;
	}
	/**
	 * 設置圖書名稱
	 */
	public void setBookName(String bookName) {
		this.bookName = bookName;
	}
	
	
	/**
	 * 獲取圖書價格
	 */
	public float getPrice() {
		return price;
	}
	/**
	 * 設置圖書價格
	 */
	public void setPrice(float price) {
		this.price = price;
	}
	
	
	/**
	 * 獲取圖書作者
	 */
	public String getAuthor() {
		return author;
	}
	/**
	 * 設置圖書作者
	 */
	public void setAuthor(String author) {
		this.author = author;
	}
	
	
	/**
	 * 獲取出版社
	 */
	public String getPublishHouse() {
		return publishedHouse;
	}
	/**
	 * 設置出版社
	 */
	public void setPublishHouse(String publishedHouse) {
		this.publishedHouse = publishedHouse;
	}
	
	
	/**
	 * 獲取圖書分類信息
	 */
	public String getBookCategory() {
		return bookCategory;
	}
	/**
	 * 設置圖書分類信息
	 */
	public void setBookCategory(String bookCategory) {
		this.bookCategory = bookCategory;
	}
	
	
	/**
	 * 獲取借書人姓名
	 */
	public String getBorrowerName() {
		return borrowerName;
	}
	/**
	 * 設置借書人姓名
	 */
	public void setBorrowerName(String borrowerName) {
		this.borrowerName = borrowerName;
	}
	
	
	/**
	 * 獲取借書人電話
	 */
	public String getBorrowerPhone() {
		return borrowerPhone;
	}
	/**
	 * 設置借書人電話
	 */
	public void setBorrowerPhone(String borrowerPhone) {
		this.borrowerPhone = borrowerPhone;
	} 
}

重點內容 :

主要就是數據庫對應表中各對應的字段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三層架構組織 

參考文章:https://www.cnblogs.com/Qian123/p/5339164.html#_labelTop

參考文章:https://blog.csdn.net/acm_hmj/article/details/52830920

源碼打包下載地址:www.jb51.net/codes/769916.html

到此這篇關於Java+MySQL 圖書管理系統的文章就介紹到這瞭,更多相關Java 圖書管理內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀:

    None Found