• JDBC和GUI实现图书管理系统


    1.介绍

            主要通过gui页面实现了与mysql数据库实现交互,跟以前写的图书管理系统相比就是从存在集合中变成了存在数据库中.

    下面我来介绍一下主要功能吧:        

    1.1.通过基本信息添加图书:

    1.2.通过ID修改图书

    1.3.通过ID查询图书

    1.4.通过ID删除图书

    2.构建数据库

    2.1建库

    create database wjq default charset utf8mb4;

    2.2建表

    1. create table books
    2. (
    3. book_id int auto_increment
    4. primary key,
    5. title varchar(100) not null,
    6. author varchar(100) not null,
    7. price decimal(10, 2) not null
    8. );

    3.java代码

    主要是代码简单,我就没必要高内聚了,我就全部写在同一个代码了

    1. import javax.swing.*;
    2. import java.awt.*;
    3. import java.awt.event.*;
    4. import java.sql.*;
    5. public class BookManagementSystem extends JFrame {
    6. private JTextField tfBookID;
    7. private JTextField tfTitle;
    8. private JTextField tfAuthor;
    9. private JTextField tfPrice;
    10. private JTextArea taResult;
    11. public BookManagementSystem() {
    12. setTitle("图书管理系统");
    13. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    14. setSize(400, 400);
    15. setLayout(new FlowLayout());
    16. JLabel lblBookID = new JLabel("图书ID:");
    17. tfBookID = new JTextField(10);
    18. JLabel lblTitle = new JLabel("书名:");
    19. tfTitle = new JTextField(10);
    20. JLabel lblAuthor = new JLabel("作者:");
    21. tfAuthor = new JTextField(10);
    22. JLabel lblPrice = new JLabel("价格:");
    23. tfPrice = new JTextField(10);
    24. JButton btnAdd = new JButton("增加");
    25. JButton btnDelete = new JButton("删除");
    26. JButton btnUpdate = new JButton("修改");
    27. JButton btnSearch = new JButton("查询");
    28. taResult = new JTextArea(10, 30);
    29. taResult.setEditable(false);
    30. add(lblBookID);
    31. add(tfBookID);
    32. add(lblTitle);
    33. add(tfTitle);
    34. add(lblAuthor);
    35. add(tfAuthor);
    36. add(lblPrice);
    37. add(tfPrice);
    38. add(btnAdd);
    39. add(btnDelete);
    40. add(btnUpdate);
    41. add(btnSearch);
    42. add(new JScrollPane(taResult));
    43. btnAdd.addActionListener(new ActionListener() {
    44. public void actionPerformed(ActionEvent e) {
    45. String bookID = tfBookID.getText();
    46. String title = tfTitle.getText();
    47. String author = tfAuthor.getText();
    48. String price = tfPrice.getText();
    49. if (!bookID.isEmpty() && !title.isEmpty() && !author.isEmpty() && !price.isEmpty()) {
    50. addBook(bookID, title, author, price);
    51. }
    52. // 清除文本框内容
    53. tfBookID.setText("");
    54. tfTitle.setText("");
    55. tfAuthor.setText("");
    56. tfPrice.setText("");
    57. }
    58. });
    59. btnDelete.addActionListener(new ActionListener() {
    60. public void actionPerformed(ActionEvent e) {
    61. String bookID = tfBookID.getText();
    62. if (!bookID.isEmpty()) {
    63. deleteBook(bookID);
    64. }
    65. // 清除文本框内容
    66. tfBookID.setText("");
    67. }
    68. });
    69. btnUpdate.addActionListener(new ActionListener() {
    70. public void actionPerformed(ActionEvent e) {
    71. String bookID = tfBookID.getText();
    72. String title = tfTitle.getText();
    73. String author = tfAuthor.getText();
    74. String price = tfPrice.getText();
    75. if (!bookID.isEmpty() && !title.isEmpty() && !author.isEmpty() && !price.isEmpty()) {
    76. updateBook(bookID, title, author, price);
    77. }
    78. // 清除文本框内容
    79. tfBookID.setText("");
    80. tfTitle.setText("");
    81. tfAuthor.setText("");
    82. tfPrice.setText("");
    83. }
    84. });
    85. btnSearch.addActionListener(new ActionListener() {
    86. public void actionPerformed(ActionEvent e) {
    87. String bookID = tfBookID.getText();
    88. String title = tfTitle.getText();
    89. String author = tfAuthor.getText();
    90. searchBooks(bookID, title, author);
    91. // 清除文本框内容
    92. tfBookID.setText("");
    93. tfTitle.setText("");
    94. tfAuthor.setText("");
    95. }
    96. });
    97. }
    98. private void addBook(String bookID, String title, String author, String price) {
    99. try {
    100. //注册JDBC的驱动
    101. Class.forName("com.mysql.cj.jdbc.Driver");
    102. // 连接数据库
    103. Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/wjq", "root", "1234");
    104. // 插入语句
    105. String insert = "INSERT INTO books (book_id, title, author, price) VALUES (?, ?, ?, ?)";
    106. // 创建PreparedStatement对象
    107. PreparedStatement pstmt = conn.prepareStatement(insert);
    108. pstmt.setString(1, bookID);
    109. pstmt.setString(2, title);
    110. pstmt.setString(3, author);
    111. pstmt.setString(4, price);
    112. // 执行插入
    113. int rowsAffected = pstmt.executeUpdate();
    114. if (rowsAffected > 0) {
    115. taResult.setText("添加成功!");
    116. } else {
    117. taResult.setText("添加失败!");
    118. }
    119. // 关闭连接
    120. pstmt.close();
    121. conn.close();
    122. } catch (SQLException ex) {
    123. ex.printStackTrace();
    124. } catch (ClassNotFoundException e) {
    125. throw new RuntimeException(e);
    126. }
    127. }
    128. private void deleteBook(String bookID) {
    129. try {
    130. // 连接数据库
    131. Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/wjq", "root", "1234");
    132. // 删除语句
    133. String delete = "DELETE FROM books WHERE book_id = ?";
    134. // 创建PreparedStatement对象
    135. PreparedStatement pstmt = conn.prepareStatement(delete);
    136. pstmt.setString(1, bookID);
    137. // 执行删除
    138. int rowsAffected = pstmt.executeUpdate();
    139. if (rowsAffected > 0) {
    140. taResult.setText("删除成功!");
    141. } else {
    142. taResult.setText("删除失败!");
    143. }
    144. // 关闭连接
    145. pstmt.close();
    146. conn.close();
    147. } catch (SQLException ex) {
    148. ex.printStackTrace();
    149. }
    150. }
    151. private void updateBook(String bookID, String title, String author, String price) {
    152. try {
    153. // 连接数据库
    154. Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/wjq", "root", "1234");
    155. // 更新语句
    156. String update = "UPDATE books SET title = ?, author = ?, price = ? WHERE book_id = ?";
    157. // 创建PreparedStatement对象
    158. PreparedStatement pstmt = conn.prepareStatement(update);
    159. pstmt.setString(1, title);
    160. pstmt.setString(2, author);
    161. pstmt.setString(3, price);
    162. pstmt.setString(4, bookID);
    163. // 执行更新
    164. int rowsAffected = pstmt.executeUpdate();
    165. if (rowsAffected > 0) {
    166. taResult.setText("修改成功!");
    167. } else {
    168. taResult.setText("修改失败!");
    169. }
    170. // 关闭连接
    171. pstmt.close();
    172. conn.close();
    173. } catch (SQLException ex) {
    174. ex.printStackTrace();
    175. }
    176. }
    177. private void searchBooks(String bookID, String title, String author) {
    178. try {
    179. // 连接数据库
    180. Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/wjq", "root", "1234");
    181. // 查询语句
    182. String query = "SELECT * FROM books WHERE (book_id = ? OR ? IS NULL) AND (title LIKE ? OR ? IS NULL) AND (author LIKE ? OR ? IS NULL)";
    183. // 创建PreparedStatement对象
    184. PreparedStatement pstmt = conn.prepareStatement(query);
    185. pstmt.setString(1, bookID);
    186. pstmt.setString(2, bookID);
    187. pstmt.setString(3, "%" + title + "%");
    188. pstmt.setString(4, title);
    189. pstmt.setString(5, "%" + author + "%");
    190. pstmt.setString(6, author);
    191. // 执行查询
    192. ResultSet rs = pstmt.executeQuery();
    193. // 处理查询结果
    194. StringBuilder sb = new StringBuilder();
    195. while (rs.next()) {
    196. String result = "图书ID: " + rs.getString("book_id") +
    197. "\t书名: " + rs.getString("title") +
    198. "\t作者: " + rs.getString("author") +
    199. "\t价格: " + rs.getString("price");
    200. sb.append(result).append("\n");
    201. }
    202. if (sb.length() > 0) {
    203. taResult.setText(sb.toString());
    204. } else {
    205. taResult.setText("未找到符合条件的图书!");
    206. }
    207. // 关闭连接
    208. rs.close();
    209. pstmt.close();
    210. conn.close();
    211. } catch (SQLException ex) {
    212. ex.printStackTrace();
    213. }
    214. }
    215. public static void main(String[] args) {
    216. SwingUtilities.invokeLater(new Runnable() {
    217. public void run() {
    218. new BookManagementSystem().setVisible(true);
    219. }
    220. });
    221. }
    222. }

    有什么不懂的,错误的欢迎私信和评论区指正和提问,谢谢大家 

  • 相关阅读:
    应用层使用select进行检测连接状态
    1.1 测控系统对测控电路的要求、测控电路中的重要指标
    实战Netty!基于私有协议,怎样快速开发网络通信服务?
    (C++)验证回文字符串
    WebDAV之葫芦儿·派盘本地个人云+Documents
    学习笔记-TP5反序列化利用
    纯JavaScript实现表白代码
    测试工具介绍||Postman的简单使用
    百度云下载不限速方式集合
    11月PMP考试考点安排,快看你在哪里考试!
  • 原文地址:https://blog.csdn.net/m0_74749208/article/details/133652350