您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)怎么在Java中利用MySQL實(shí)現(xiàn)一個(gè)圖書管理系統(tǒng),可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
管理員登錄
圖書借閱信息管理
圖書信息管理
管理員更改密碼
退出系統(tǒng)
Eclipse Version: 2018-09 (4.9.0)
MySQL Workbench 8.0 CE
mysql-connector-java-8.0.13.jar
1)圖書表
2)用戶表
兩個(gè)數(shù)據(jù)表間沒(méi)有關(guān)聯(lián):
(1)邏輯圖
(2)包結(jié)構(gòu),采用MVC三層架構(gòu)組織各個(gè)模塊
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; /** * 數(shù)據(jù)庫(kù)圖書表信息數(shù)據(jù)訪問(wèn)對(duì)象類,包含增加圖書信息、刪除圖書信息 * 、更新圖書信息、查詢圖書信息、查詢借閱信息和歸還圖書 * * @author 1651200111 陳彥志 */ public class BookDao { /** * 增加圖書信息 */ public void addBook(Book book) throws Exception{ // 首先拿到數(shù)據(jù)庫(kù)的連接 Connection con = DBUtil.getConnection(); String sql="insert into tb_books" // ISBN、書名、圖書價(jià)格、圖書作者、出版社 + "(ISBN, book_name, book_price, book_author, published_house," // 分類號(hào)、借書人姓名、借書人電話、借書日期,已借天數(shù) + "book_category, borrower_name, borrower_phone) " + "values(" /* * 參數(shù)用?表示,相當(dāng)于占位符,然后在對(duì)參數(shù)進(jìn)行賦值。當(dāng)真正執(zhí)行時(shí), * 這些參數(shù)會(huì)加載在SQL語(yǔ)句中,把SQL語(yǔ)句拼接完整才去執(zhí)行。這樣就會(huì)減少對(duì)數(shù)據(jù)庫(kù)的操作 */ + "?,?,?,?,?,?,?,?)"; /* * prepareStatement這個(gè)方法會(huì)將SQL語(yǔ)句加載到驅(qū)動(dòng)程序conn集成程序中, * 但是并不直接執(zhí)行,而是當(dāng)它調(diào)用execute()方法的時(shí)候才真正執(zhí)行; */ PreparedStatement psmt = con.prepareStatement(sql); // 先對(duì)應(yīng)SQL語(yǔ)句,給SQL語(yǔ)句傳遞參數(shù) 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()); } //執(zhí)行SQL語(yǔ)句 psmt.execute(); } /** * 刪除圖書信息 */ public void delBook(int ID) throws SQLException{ // 首先拿到數(shù)據(jù)庫(kù)的連接 Connection con=DBUtil.getConnection(); String sql="" + "DELETE FROM tb_books "+ // 參數(shù)用?表示,相當(dāng)于占位符 "WHERE ID = ?"; // 預(yù)編譯sql語(yǔ)句 PreparedStatement psmt = con.prepareStatement(sql); // 先對(duì)應(yīng)SQL語(yǔ)句,給SQL語(yǔ)句傳遞參數(shù) psmt.setInt(1, ID); // 執(zhí)行SQL語(yǔ)句 psmt.execute(); } /** * 更新圖書信息 */ public void changeBook(Book book) throws SQLException{ // 首先拿到數(shù)據(jù)庫(kù)的連接 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 = ? " // 參數(shù)用?表示,相當(dāng)于占位符 + "where ID = ?"; // 預(yù)編譯sql語(yǔ)句 PreparedStatement psmt = con.prepareStatement(sql); // 先對(duì)應(yīng)SQL語(yǔ)句,給SQL語(yǔ)句傳遞參數(shù) 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()); // 執(zhí)行SQL語(yǔ)句 psmt.execute(); } /** * 查詢書籍信息 */ public List<Book> query() throws Exception{ Connection con = DBUtil.getConnection(); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select " // ISBN、書名、作者、圖書價(jià)格、出版社 + "ID, ISBN, book_name, book_author, book_price, published_house, " // 分類號(hào)、借書人姓名、借書人電話 + "book_category, borrower_name, borrower_phone " + "from tb_books"); List<Book> bookList = new ArrayList<Book>(); Book book = null; // 如果對(duì)象中有數(shù)據(jù),就會(huì)循環(huán)打印出來(lái) 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; // 如果對(duì)象中有數(shù)據(jù),就會(huì)循環(huán)打印出來(lái) 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{ // 首先拿到數(shù)據(jù)庫(kù)的連接 Connection con=DBUtil.getConnection(); String sql="UPDATE tb_books " // ISBN、圖書名稱、作者、價(jià)格 + "SET " // 借書人姓名、借書人電話 + "borrower_name = ?, borrower_phone = ? " // 參數(shù)用?表示,相當(dāng)于占位符 + "WHERE ID = ?"; // 預(yù)編譯sql語(yǔ)句 PreparedStatement psmt = con.prepareStatement(sql); // 先對(duì)應(yīng)SQL語(yǔ)句,給SQL語(yǔ)句傳遞參數(shù) psmt.setString(1, book.getBorrowerName()); psmt.setString(2, book.getBorrowerPhone()); psmt.setInt(3, book.getID()); // 執(zhí)行SQL語(yǔ)句 psmt.execute(); } }
重點(diǎn)內(nèi)容 :
JDBC進(jìn)行簡(jiǎn)單的數(shù)據(jù)庫(kù)增刪改查
詳細(xì)參考:https://www.cnblogs.com/Qian123/p/5339164.html#_labelTop
Model類(以Book為例)
package pers.cyz.model; /** * 圖書模型類,包含數(shù)據(jù)庫(kù)圖書表各對(duì)應(yīng)的字段get、set方法 * * @author 1651200111 陳彥志 */ public class Book { private int ID; // ISBN號(hào) private String ISBN; // 圖書名稱 private String bookName; // 圖書價(jià)格 private float price; // 圖書作者 private String author; // 出版社 private String publishedHouse; // 圖書分類號(hào) private String bookCategory; // 借書人姓名 private String borrowerName; // 借書人電話 private String borrowerPhone; /** * 獲取ID */ public int getID() { return ID; } /** * 設(shè)置ID */ public void setID(int iD) { ID = iD; } /** * 獲取ISBN */ public String getISBN() { return ISBN; } /** * 設(shè)置ISBN */ public void setISBN(String iSBN) { ISBN = iSBN; } /** * 獲取圖書名稱 */ public String getBookName() { return bookName; } /** * 設(shè)置圖書名稱 */ public void setBookName(String bookName) { this.bookName = bookName; } /** * 獲取圖書價(jià)格 */ public float getPrice() { return price; } /** * 設(shè)置圖書價(jià)格 */ public void setPrice(float price) { this.price = price; } /** * 獲取圖書作者 */ public String getAuthor() { return author; } /** * 設(shè)置圖書作者 */ public void setAuthor(String author) { this.author = author; } /** * 獲取出版社 */ public String getPublishHouse() { return publishedHouse; } /** * 設(shè)置出版社 */ public void setPublishHouse(String publishedHouse) { this.publishedHouse = publishedHouse; } /** * 獲取圖書分類信息 */ public String getBookCategory() { return bookCategory; } /** * 設(shè)置圖書分類信息 */ public void setBookCategory(String bookCategory) { this.bookCategory = bookCategory; } /** * 獲取借書人姓名 */ public String getBorrowerName() { return borrowerName; } /** * 設(shè)置借書人姓名 */ public void setBorrowerName(String borrowerName) { this.borrowerName = borrowerName; } /** * 獲取借書人電話 */ public String getBorrowerPhone() { return borrowerPhone; } /** * 設(shè)置借書人電話 */ public void setBorrowerPhone(String borrowerPhone) { this.borrowerPhone = borrowerPhone; } }
重點(diǎn)內(nèi)容 :
主要就是數(shù)據(jù)庫(kù)對(duì)應(yīng)表中各對(duì)應(yīng)的字段get、set方法
Eclipse技巧:
Shift + alt + s -> Generate Getters and Setters -> Select all -> Generate 自動(dòng)生成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; /** * 圖書信息行為控制類,包含增加圖書、刪除圖書 * 、 修改圖書、和初始化個(gè)人書庫(kù)管理窗體表格 * * @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; /** * 連接數(shù)據(jù)庫(kù)類,包含一個(gè)對(duì)外提供獲取數(shù)據(jù)庫(kù)連接的方法 * * @author 1651200111 陳彥志 */ public class DBUtil { // 數(shù)據(jù)庫(kù)連接路徑 private static final String URL = "jdbc:mysql://127.0.0.1:3306/db_books?" + "useUnicode = true & serverTimezone = GMT" // MySQL在高版本需要指明是否進(jìn)行SSL連接 + "& characterEncoding = utf8 & useSSL = false"; private static final String NAME = "root"; private static final String PASSWORD = "root"; private static Connection conn = null; // 靜態(tài)代碼塊(將加載驅(qū)動(dòng)、連接數(shù)據(jù)庫(kù)放入靜態(tài)塊中) static{ try { // 加載驅(qū)動(dòng)程序 Class.forName("com.mysql.cj.jdbc.Driver"); // 獲取數(shù)據(jù)庫(kù)的連接 conn = DriverManager.getConnection(URL, NAME, PASSWORD); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } // 對(duì)外提供一個(gè)方法來(lái)獲取數(shù)據(jù)庫(kù)連接 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; /** * 設(shè)置背景圖片類 * * @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); // 設(shè)置label的大小 labelBackground.setBounds(0,0,iconBookManageSystemBackground.getIconWidth() ,iconBookManageSystemBackground.getIconHeight()); // 將背景圖片標(biāo)簽放入桌面面板的最底層 frame.getLayeredPane().add(labelBackground,new Integer(Integer.MIN_VALUE)); // 將容器轉(zhuǎn)換為面板設(shè)置為透明 JPanel panel = (JPanel)container; panel.setOpaque(false); } }
看完上述內(nèi)容,你們對(duì)怎么在Java中利用MySQL實(shí)現(xiàn)一個(gè)圖書管理系統(tǒng)有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。