Java MySQL) 快三輸了不少怎麼回本81512073

覺着有用就點個贊哦~
加一Q一帶一你✅10319281✅邀一情一瑪✅33339333✅進【0 9 1 9 1x . c o m 】✅已助上千人成功翻盤,歡迎增長,溝通交流!html

一,功能

  1. 管理員登陸
  2. 圖書借閱信息管理
  3. 圖書信息管理
  4. 管理員更改密碼
  5. 退出系統

二,工具

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

3、效果圖:

登陸界面:java

主界面:mysql

借閱書籍管理:sql

我的書庫管理:數據庫

更改密碼:架構

4、數據庫設計

     1)圖書表數據庫設計

     2)用戶表工具

兩個數據表間沒有關聯:this

5、JAVA層次分析

(1)邏輯圖spa

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

 
-

6、主要Java代碼分析

Dao類(以BookDao爲例)

1.  package pers.cyz.dao;
    

3.  import java.sql.Connection;
    
4.  import java.sql.PreparedStatement;
    
5.  import java.sql.ResultSet;
    
6.  import java.sql.SQLException;
    
7.  import java.sql.Statement;
    
8.  import java.util.ArrayList;
    
9.  import java.util.List;
    

11.  import pers.cyz.model.Book;
    
12.  import pers.cyz.util.DBUtil;
    

14.  /**
    
15.   * 數據庫圖書表信息數據訪問對象類,包含增長圖書信息、刪除圖書信息
    
16.   * 、更新圖書信息、查詢圖書信息、查詢借閱信息和歸還圖書
    
17.   * 
    
18.   * @author 1651200111 陳彥志
    
19.   */
    
20.  public class BookDao {
    

23.      /**
    
24.   * 增長圖書信息
    
25.   */
    
26.      public void addBook(Book book) throws Exception{
    
27.          // 首先拿到數據庫的鏈接
    
28.          Connection con = DBUtil.getConnection();
    
29.          String sql="insert into tb_books"
    
30.                  // ISBN、書名、圖書價格、圖書做者、出版社
    
31.                  + "(ISBN, book_name, book_price, book_author, published_house,"
    
32.                  // 分類號、借書人姓名、借書人電話、借書日期,已借天數
    
33.                  + "book_category, borrower_name, borrower_phone) "
    
34.                  + "values("
    
35.                  /*
    
36.   * 參數用?表示,至關於佔位符,而後在對參數進行賦值。當真正執行時,
    
37.   * 這些參數會加載在SQL語句中,把SQL語句拼接完整纔去執行。這樣就會減小對數據庫的操做
    
38.   */
    
39.                  + "?,?,?,?,?,?,?,?)";
    
40.          /*
    
41.   * prepareStatement這個方法會將SQL語句加載到驅動程序conn集成程序中,
    
42.   * 可是並不直接執行,而是當它調用execute()方法的時候才真正執行;
    
43.   */
    
44.          PreparedStatement psmt = con.prepareStatement(sql);
    
45.          // 先對應SQL語句,給SQL語句傳遞參數
    
46.          psmt.setString(1, book.getISBN());
    
47.          psmt.setString(2, book.getBookName());
    
48.          psmt.setFloat(3, book.getPrice());
    
49.          psmt.setString(4, book.getAuthor());
    
50.          psmt.setString(5, book.getPublishHouse());
    
51.          psmt.setString(6, book.getBookCategory());
    

53.          if (book.getBorrowerName() == null || book.getBorrowerName() == "") {
    
54.              psmt.setString(7, null);
    
55.          }
    
56.          else {
    
57.                psmt.setString(7, book.getBorrowerName());
    
58.          }
    

60.          if (book.getBorrowerPhone() == null || book.getBorrowerPhone() == "") {
    
61.               psmt.setString(8, null);  
    
62.          }
    
63.          else {
    
64.              psmt.setString(8, book.getBorrowerPhone());  
    
65.          }
    

67.          //執行SQL語句
    
68.          psmt.execute();
    

70.      }
    

73.      /**
    
74.   * 刪除圖書信息
    
75.   */
    
76.        public void delBook(int ID) throws SQLException{
    
77.            // 首先拿到數據庫的鏈接
    
78.            Connection con=DBUtil.getConnection();
    
79.            String sql="" + 
    
80.                    "DELETE FROM tb_books "+               
    
81.                    // 參數用?表示,至關於佔位符
    
82.                    "WHERE ID = ?";
    
83.            // 預編譯sql語句
    
84.            PreparedStatement psmt = con.prepareStatement(sql);
    
85.            // 先對應SQL語句,給SQL語句傳遞參數
    
86.            psmt.setInt(1, ID);
    
87.            // 執行SQL語句
    
88.            psmt.execute();    
    
89.        }
    

92.      /**
    
93.   * 更新圖書信息
    
94.   */
    
95.      public void changeBook(Book book) throws SQLException{
    
96.          // 首先拿到數據庫的鏈接
    
97.          Connection con=DBUtil.getConnection();
    
98.          String sql="update tb_books "
    
99.                  + "set ISBN = ?, book_name = ?, book_price = ?, book_author = ?"
    
100.                  + ",published_house = ?, book_category = ?, borrower_name = ?, borrower_phone = ? "
    
101.                  // 參數用?表示,至關於佔位符 
    
102.                  + "where ID = ?";
    
103.          // 預編譯sql語句
    
104.          PreparedStatement psmt = con.prepareStatement(sql);
    
105.          // 先對應SQL語句,給SQL語句傳遞參數
    
106.          psmt.setString(1, book.getISBN());
    
107.          psmt.setString(2, book.getBookName());
    
108.          psmt.setFloat(3, book.getPrice());
    
109.          psmt.setString(4, book.getAuthor());
    
110.          psmt.setString(5, book.getPublishHouse());
    
111.          psmt.setString(6, book.getBookCategory());
    
112.          if (book.getBorrowerName().equals("")) {
    
113.              psmt.setString(7, null);
    
114.          }
    
115.          else {
    
116.               psmt.setString(7, book.getBorrowerName());
    
117.          }
    

119.          if (book.getBorrowerPhone().equals("")) {
    
120.              psmt.setString(8, null);
    
121.          }
    
122.          else {
    
123.               psmt.setString(8, book.getBorrowerPhone());
    
124.          }
    
125.          psmt.setInt(9, book.getID());
    
126.          // 執行SQL語句
    
127.          psmt.execute();    
    
128.      }
    

132.      /**
    
133.   * 查詢書籍信息
    
134.   */
    
135.      public List<Book> query() throws Exception{           
    
136.          Connection con = DBUtil.getConnection();            
    
137.          Statement stmt = con.createStatement();           
    
138.          ResultSet rs = stmt.executeQuery("select "
    
139.                  // ISBN、書名、做者、圖書價格、出版社
    
140.                  + "ID, ISBN, book_name, book_author, book_price, published_house, "
    
141.                  // 分類號、借書人姓名、借書人電話
    
142.                  + "book_category, borrower_name, borrower_phone "
    
143.                  + "from tb_books");          
    
144.          List<Book> bookList = new ArrayList<Book>();           
    
145.          Book book = null;       
    
146.          // 若是對象中有數據,就會循環打印出來
    
147.          while (rs.next()){               
    
148.              book = new Book();         
    
149.              book.setID(rs.getInt("ID"));
    
150.              book.setISBN(rs.getString("ISBN"));
    
151.              book.setBookName(rs.getString("book_name"));           
    
152.              book.setAuthor(rs.getString("book_author"));
    
153.              book.setPrice(rs.getFloat("book_price"));
    
154.              book.setPublishHouse(rs.getString("published_house"));
    
155.              book.setBookCategory(rs.getString("book_category"));
    
156.              book.setBorrowerName(rs.getString("borrower_name"));
    
157.              book.setBorrowerPhone(rs.getString("borrower_phone"));
    
158.              bookList.add(book);            
    
159.          }           
    
160.          return bookList;      
    
161.      }
    

164.      /**
    
165.   * 查詢借閱信息
    
166.   * 
    
167.   * @return
    
168.   *         bookList
    
169.   */
    
170.      public List<Book> borrowQuery() throws Exception{           
    
171.          Connection con = DBUtil.getConnection();            
    
172.          Statement stmt = con.createStatement();           
    
173.          ResultSet rs = stmt.executeQuery(""
    
174.                  // ID、書名、借書人姓名、借書人電話
    
175.                  + "SELECT ID, book_name, borrower_name, borrower_phone "
    
176.                  + "FROM tb_books "
    
177.                  + "WHERE borrower_name IS NOT NULL"
    
178.                  );          
    
179.          List<Book> bookList = new ArrayList<Book>();           
    
180.          Book book = null;         
    
181.          // 若是對象中有數據,就會循環打印出來
    
182.          while (rs.next()){               
    
183.              book = new Book();         
    
184.              book.setID(rs.getInt("ID"));
    
185.              book.setBookName(rs.getString("book_name"));           
    
186.              book.setBorrowerName(rs.getString("borrower_name"));
    
187.              book.setBorrowerPhone(rs.getString("borrower_phone"));
    
188.              bookList.add(book);            
    
189.          }           
    
190.          return bookList;      
    
191.      }
    

193.      /**
    
194.   * 更新圖書信息,歸還圖書
    
195.   */
    
196.      public void returnBook(Book book) throws SQLException{
    
197.          // 首先拿到數據庫的鏈接
    
198.          Connection con=DBUtil.getConnection();
    
199.          String sql="UPDATE tb_books "
    
200.                  // ISBN、圖書名稱、做者、價格
    
201.                  + "SET "
    
202.                  // 借書人姓名、借書人電話
    
203.                  + "borrower_name = ?, borrower_phone = ? "
    
204.                  // 參數用?表示,至關於佔位符 
    
205.                  + "WHERE ID = ?";
    
206.          // 預編譯sql語句
    
207.          PreparedStatement psmt = con.prepareStatement(sql);
    
208.          // 先對應SQL語句,給SQL語句傳遞參數
    
209.          psmt.setString(1, book.getBorrowerName());
    
210.          psmt.setString(2, book.getBorrowerPhone());
    
211.          psmt.setInt(3, book.getID());
    
212.          // 執行SQL語句
    
213.          psmt.execute();    
    
214.      }
    

217.  }

重點內容 :

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

詳細參考:http://www.javashuo.com/article/p-hwcbsttj-a.html

Model類(以Book爲例)

1.  package pers.cyz.model;
    

3.  /**
    
4.   * 圖書模型類,包含數據庫圖書表各對應的字段get、set方法
    
5.   * 
    
6.   * @author 1651200111 陳彥志
    
7.   */
    
8.  public class Book {
    
9.      private int ID;
    
10.      // ISBN號
    
11.      private String ISBN;
    
12.      // 圖書名稱
    
13.      private String bookName;
    
14.      // 圖書價格
    
15.      private float price;
    
16.      // 圖書做者
    
17.      private String author;
    
18.      // 出版社
    
19.      private String publishedHouse;
    
20.      // 圖書分類號
    
21.      private String bookCategory;
    
22.      // 借書人姓名
    
23.      private String borrowerName;
    
24.      // 借書人電話
    
25.      private String borrowerPhone;
    

27.      /**
    
28.   * 獲取ID
    
29.   */
    
30.      public int getID() {
    
31.          return ID;
    
32.      }
    
33.      /**
    
34.   * 設置ID
    
35.   */
    
36.      public void setID(int iD) {
    
37.          ID = iD;
    
38.      }
    

40.      /**
    
41.   * 獲取ISBN
    
42.   */
    
43.      public String getISBN() {
    
44.          return ISBN;
    
45.      }
    
46.      /**
    
47.   * 設置ISBN
    
48.   */
    
49.      public void setISBN(String iSBN) {
    
50.          ISBN = iSBN;
    
51.      }
    

54.      /**
    
55.   * 獲取圖書名稱
    
56.   */
    
57.      public String getBookName() {
    
58.          return bookName;
    
59.      }
    
60.      /**
    
61.   * 設置圖書名稱
    
62.   */
    
63.      public void setBookName(String bookName) {
    
64.          this.bookName = bookName;
    
65.      }
    

68.      /**
    
69.   * 獲取圖書價格
    
70.   */
    
71.      public float getPrice() {
    
72.          return price;
    
73.      }
    
74.      /**
    
75.   * 設置圖書價格
    
76.   */
    
77.      public void setPrice(float price) {
    
78.          this.price = price;
    
79.      }
    

82.      /**
    
83.   * 獲取圖書做者
    
84.   */
    
85.      public String getAuthor() {
    
86.          return author;
    
87.      }
    
88.      /**
    
89.   * 設置圖書做者
    
90.   */
    
91.      public void setAuthor(String author) {
    
92.          this.author = author;
    
93.      }
    

96.      /**
    
97.   * 獲取出版社
    
98.   */
    
99.      public String getPublishHouse() {
    
100.          return publishedHouse;
    
101.      }
    
102.      /**
    
103.   * 設置出版社
    
104.   */
    
105.      public void setPublishHouse(String publishedHouse) {
    
106.          this.publishedHouse = publishedHouse;
    
107.      }
    

110.      /**
    
111.   * 獲取圖書分類信息
    
112.   */
    
113.      public String getBookCategory() {
    
114.          return bookCategory;
    
115.      }
    
116.      /**
    
117.   * 設置圖書分類信息
    
118.   */
    
119.      public void setBookCategory(String bookCategory) {
    
120.          this.bookCategory = bookCategory;
    
121.      }
    

124.      /**
    
125.   * 獲取借書人姓名
    
126.   */
    
127.      public String getBorrowerName() {
    
128.          return borrowerName;
    
129.      }
    
130.      /**
    
131.   * 設置借書人姓名
    
132.   */
    
133.      public void setBorrowerName(String borrowerName) {
    
134.          this.borrowerName = borrowerName;
    
135.      }
    

138.      /**
    
139.   * 獲取借書人電話
    
140.   */
    
141.      public String getBorrowerPhone() {
    
142.          return borrowerPhone;
    
143.      }
    
144.      /**
    
145.   * 設置借書人電話
    
146.   */
    
147.      public void setBorrowerPhone(String borrowerPhone) {
    
148.          this.borrowerPhone = borrowerPhone;
    
149.      }
    

152.  }

重點內容 :

主要就是數據庫對應表中各對應的字段get、set方法

    Eclipse技巧:

        Shift + alt + s  -> Generate Getters and Setters -> Select all -> Generate 自動生成set、get方法

Controller類(以BookAction爲例)

1.  package pers.cyz.controller;
    

3.  import java.util.List;
    

5.  import javax.swing.JTable;
    
6.  import javax.swing.JTextField;
    

8.  import pers.cyz.dao.BookDao;
    
9.  import pers.cyz.model.Book;
    

12.  /**
    
13.   * 圖書信息行爲控制類,包含增長圖書、刪除圖書
    
14.   * 、 修改圖書、和初始化我的書庫管理窗體表格
    
15.   * 
    
16.   * @author 1651200111 陳彥志
    
17.   */
    
18.  public class BookAction {
    

22.      /**
    
23.   * 初始化窗體表格
    
24.   * @return
    
25.   *         results
    
26.   */
    
27.      @SuppressWarnings("rawtypes")
    
28.      public Object[][] initializTable(String[] columnNames) throws Exception{
    
29.          BookDao bookDao = new BookDao();
    
30.          List list = bookDao.query();
    
31.          Object[][] results = new Object[list.size()][columnNames.length];
    

33.          for(int i = 0; i < list.size(); i++) {
    
34.              Book book = (Book)list.get(i);                
    

36.              results[i][0] = book.getID();
    
37.              results[i][1] = book.getBookName();
    
38.              results[i][2] = book.getAuthor();
    
39.              results[i][3] = book.getPrice();
    
40.              results[i][4] = book.getISBN();
    
41.              results[i][5] = book.getPublishHouse();
    
42.              results[i][6] = book.getBookCategory();    
    

44.              String borrowerName =  book.getBorrowerName();
    
45.              if (borrowerName == null) {    
    
46.                  borrowerName = "";    
    
47.                  results[i][7] = borrowerName;
    
48.              }
    
49.              else {    
    
50.                  results[i][7] = borrowerName;
    
51.              }
    

53.              String borrowerPhone = book.getBorrowerPhone();    
    
54.              if (borrowerPhone == null) {        
    
55.                  borrowerPhone = "";    
    
56.                  results[i][8] = borrowerPhone;
    
57.              }    
    
58.              else {    
    
59.                  results[i][8] = borrowerPhone;
    
60.              }
    
61.          }           
    
62.          return results;
    
63.  }
    

66.       /**
    
67.   * 添加圖書信息
    
68.   */
    
69.      public void addBookInformation (JTextField textFieldISBN, JTextField textFieldName
    
70.   ,JTextField textFieldPrice, JTextField textFieldAuthor, JTextField textFieldPublishedHouse
    
71.   , JTextField textFieldBookCategory, JTextField textFieldBorrowName
    
72.   , JTextField textFieldBorrowPhone) throws Exception {
    

74.          BookDao bookDao=new BookDao();
    
75.          Book book=new Book();     
    

77.          book.setISBN(textFieldISBN.getText());      
    
78.          book.setBookName(textFieldName.getText());
    
79.          float price = Float.parseFloat(textFieldPrice.getText());
    
80.          book.setPrice(price);
    
81.          book.setAuthor(textFieldAuthor.getText());
    
82.          book.setPublishHouse(textFieldPublishedHouse.getText());
    
83.          book.setBookCategory(textFieldBookCategory.getText());
    

85.          if (textFieldBorrowName.getText() == null ||textFieldBorrowName.getText() == "" ) {
    
86.              book.setBorrowerName(null);
    
87.          }
    
88.          else {
    
89.               book.setBorrowerName(textFieldBorrowName.getText());
    
90.          }
    

92.          if (textFieldBorrowPhone.getText() == null || textFieldBorrowPhone.getText() == "") {
    
93.              book.setBorrowerPhone(null);
    
94.          }
    
95.          else {
    
96.              book.setBorrowerPhone(textFieldBorrowPhone.getText());
    
97.          }
    

99.          //添加圖書
    
100.          bookDao.addBook(book);
    
101.      }
    

105.      /**
    
106.   * 刪除圖書信息
    
107.   */
    
108.      public void delBookInformation (JTable table) throws Exception {
    

110.          int selRow = table.getSelectedRow();
    
111.          int ID = Integer.parseInt(table.getValueAt(selRow, 0).toString());
    

113.          BookDao bookDao=new BookDao();
    
114.          Book book=new Book();     
    

116.          book.setID(ID);
    

118.          // 刪除圖書信息
    
119.          bookDao.delBook(ID);
    
120.      }
    

123.      /**
    
124.   * 修改圖書信息
    
125.   */
    
126.      public void changeBookInformation (JTextField textFieldISBN, JTextField textFieldName
    
127.   ,JTextField textFieldPrice, JTextField textFieldAuthor, JTextField textFieldPublishedHouse
    
128.   , JTextField textFieldBookCategory, JTextField textFieldBorrowerName
    
129.   , JTextField textFieldBorrowerPhone, JTable table) throws Exception{
    

131.          BookDao bookDao=new BookDao();
    
132.          Book book=new Book();     
    

134.          int selRow = table.getSelectedRow();
    
135.          int ID = Integer.parseInt(table.getValueAt(selRow, 0).toString());    
    
136.          book.setID(ID);
    

138.          book.setISBN(textFieldISBN.getText());      
    
139.          book.setBookName(textFieldName.getText());
    
140.          book.setAuthor(textFieldAuthor.getText());
    
141.          float price = Float.parseFloat(textFieldPrice.getText());
    
142.          book.setPrice(price);
    
143.          book.setPublishHouse(textFieldPublishedHouse.getText());
    
144.          book.setBookCategory(textFieldBookCategory.getText());
    
145.          book.setBorrowerName(textFieldBorrowerName.getText());
    
146.          book.setBorrowerPhone(textFieldBorrowerPhone.getText());
    

148.          //修改圖書
    
149.          bookDao.changeBook(book);       
    
150.      }
    

153.  }

util類(以DBUtil爲例)

1.  package pers.cyz.util;
    

3.  import java.sql.Connection;
    
4.  import java.sql.DriverManager;
    
5.  import java.sql.SQLException;
    

7.  /**
    
8.   * 鏈接數據庫類,包含一個對外提供獲取數據庫鏈接的方法
    
9.   * 
    
10.   * @author 1651200111 陳彥志
    
11.   */
    
12.  public class DBUtil {
    

14.      // 數據庫鏈接路徑
    
15.      private static final String URL = "jdbc:mysql://127.0.0.1:3306/db_books?"
    
16.              + "useUnicode = true & serverTimezone = GMT"
    
17.              // MySQL在高版本須要指明是否進行SSL鏈接
    
18.              + "& characterEncoding = utf8 & useSSL = false";
    
19.      private static final String NAME = "root";
    
20.      private static final String PASSWORD = "root";
    
21.      private static Connection conn = null;
    

23.      // 靜態代碼塊(將加載驅動、鏈接數據庫放入靜態塊中)
    
24.       static{
    
25.              try {
    
26.                  // 加載驅動程序
    
27.                  Class.forName("com.mysql.cj.jdbc.Driver");
    
28.                  // 獲取數據庫的鏈接
    
29.                  conn = DriverManager.getConnection(URL, NAME, PASSWORD);
    
30.              } catch (ClassNotFoundException e) {
    
31.                  e.printStackTrace();
    
32.              } catch (SQLException e) {
    
33.                  e.printStackTrace();
    
34.              }
    
35.          }
    

37.       // 對外提供一個方法來獲取數據庫鏈接 
    
38.       public static Connection getConnection(){     
    
39.           return conn;       
    
40.       }
    

43.  }

util類(以BackgroundImage爲例)

1.  package pers.cyz.util;
    

3.  import java.awt.Container;
    

5.  import javax.swing.ImageIcon;
    
6.  import javax.swing.JFrame;
    
7.  import javax.swing.JLabel;
    
8.  import javax.swing.JPanel;
    

10.  /**
    
11.   * 設置背景圖片類
    
12.   * 
    
13.   * @author 1651200111 陳彥志
    
14.   */
    
15.  public class BackgroundImage {
    

17.      public BackgroundImage(JFrame frame,Container container,String ImageName) {
    
18.          // 限定加載圖片路徑
    
19.          ImageIcon icon= new ImageIcon("res/" + ImageName);    
    

21.          final JLabel labelBackground = new JLabel();
    
22.          ImageIcon iconBookManageSystemBackground = icon;
    
23.          labelBackground.setIcon(iconBookManageSystemBackground);
    
24.          // 設置label的大小
    
25.          labelBackground.setBounds(0,0,iconBookManageSystemBackground.getIconWidth()
    
26.                  ,iconBookManageSystemBackground.getIconHeight());        
    
27.          // 將背景圖片標籤放入桌面面板的最底層
    
28.          frame.getLayeredPane().add(labelBackground,new Integer(Integer.MIN_VALUE));
    
29.          // 將容器轉換爲面板設置爲透明
    
30.          JPanel panel = (JPanel)container;
    
31.          panel.setOpaque(false);
    

33.      }
    

36.  }

重點內容 :

將圖片標籤放在窗體底層面板,而後將窗體轉化爲容器,將容器面板設爲透明,背景圖片就設置好了,以後就能夠直接在該容器中添加組件

相關文章
相關標籤/搜索