觉着有用就点个赞哦~
加一Q一带一你✅10319281✅邀一情一玛✅33339333✅进【0 9 1 9 1x . c o m 】✅已助上千人成功翻盘,欢迎增加,沟通交流!
一,功能
- 管理员登录
- 图书借阅信息管理
- 图书信息管理
- 管理员更改密码
- 退出系统
二,工具
- 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为例)
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 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 bookList = new ArrayList();
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 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 bookList = new ArrayList();
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进行简单的数据库增删改查
详细参考:https://www.cnblogs.com/Qian123/p/5339164.html#_labelTop
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. }
重点内容 :
将图片标签放在窗体底层面板,然后将窗体转化为容器,将容器面板设为透明,背景图片就设置好了,之后就可以直接在该容器中添加组件