当前位置:   article > 正文

JAVA图书管理系统完整代码实现(连接MYSQL数据库)_java图书管理系统数据的关联

java图书管理系统数据的关联

1.系统功能

本系统是控制台版java图书管理系统,实现了用户的注册登录,用户管理员的登录,图书管理员的登录,用户可以购买图书和查看图书购买记录,用户管理员可以对用户信息进行增删查改,图书管理员可以查看图书信息,以及上架新的图书和修改图书库存

本系统中存在的已知bug已解决,如遇到到其它bug请在评论区留言告知。

2.系统简要演示

3.数据库表的创建

新建数据库lib_dtb,然后在桌面建立lib_dtb.sql文件,写入以下sq语句,然后点击lib_dtb数据库中的表右键运行该sql文件,数据库表即导入成功,数据库成功创建。

  1. /*
  2. Navicat Premium Data Transfer
  3. Source Server : MySql
  4. Source Server Type : MySQL
  5. Source Server Version : 80036
  6. Source Host : localhost:3306
  7. Source Schema : lib_dtb
  8. Target Server Type : MySQL
  9. Target Server Version : 80036
  10. File Encoding : 65001
  11. Date: 17/05/2024 23:00:29
  12. */
  13. SET NAMES utf8mb4;
  14. SET FOREIGN_KEY_CHECKS = 0;
  15. -- ----------------------------
  16. -- Table structure for admin
  17. -- ----------------------------
  18. DROP TABLE IF EXISTS `admin`;
  19. CREATE TABLE `admin` (
  20. `id` int UNSIGNED NOT NULL AUTO_INCREMENT,
  21. `username` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
  22. `password` varchar(15) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
  23. PRIMARY KEY (`id`) USING BTREE,
  24. UNIQUE INDEX `username`(`username` ASC) USING BTREE
  25. ) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '管理员' ROW_FORMAT = Dynamic;
  26. -- ----------------------------
  27. -- Records of admin
  28. -- ----------------------------
  29. INSERT INTO `admin` VALUES (1, 'admin', '123456');
  30. -- ----------------------------
  31. -- Table structure for buy_table
  32. -- ----------------------------
  33. DROP TABLE IF EXISTS `buy_table`;
  34. CREATE TABLE `buy_table` (
  35. `name` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '用户姓名',
  36. `library` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '图书名字',
  37. `buy_time` datetime NOT NULL COMMENT '购买时间'
  38. ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '用户购买图书记录表' ROW_FORMAT = Dynamic;
  39. -- ----------------------------
  40. -- Records of buy_table
  41. -- ----------------------------
  42. INSERT INTO `buy_table` VALUES ('zs', '平凡世界', '2024-04-15 22:20:02');
  43. INSERT INTO `buy_table` VALUES ('zs', '平凡世界', '2024-04-16 23:25:57');
  44. INSERT INTO `buy_table` VALUES ('zs', '平凡世界', '2024-04-18 18:24:22');
  45. -- ----------------------------
  46. -- Table structure for libadmin
  47. -- ----------------------------
  48. DROP TABLE IF EXISTS `libadmin`;
  49. CREATE TABLE `libadmin` (
  50. `id` int UNSIGNED NOT NULL AUTO_INCREMENT,
  51. `username` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
  52. `password` varchar(15) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
  53. PRIMARY KEY (`id`) USING BTREE
  54. ) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '图书管理员' ROW_FORMAT = Dynamic;
  55. -- ----------------------------
  56. -- Records of libadmin
  57. -- ----------------------------
  58. INSERT INTO `libadmin` VALUES (1, 'admin', '654321');
  59. -- ----------------------------
  60. -- Table structure for library
  61. -- ----------------------------
  62. DROP TABLE IF EXISTS `library`;
  63. CREATE TABLE `library` (
  64. `id` int UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键id',
  65. `name` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '图书名字',
  66. `author` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '作者',
  67. `price` decimal(5, 2) NOT NULL COMMENT '图书价格,可以保留两位小数,总共5位',
  68. `count` int UNSIGNED NOT NULL COMMENT '图书数量',
  69. `Shelf_time` datetime NOT NULL,
  70. PRIMARY KEY (`id`) USING BTREE
  71. ) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '图书表' ROW_FORMAT = Dynamic;
  72. -- ----------------------------
  73. -- Records of library
  74. -- ----------------------------
  75. INSERT INTO `library` VALUES (1, '平凡世界', '路遥', 46.00, 0, '2024-04-15 22:02:59');
  76. -- ----------------------------
  77. -- Table structure for user
  78. -- ----------------------------
  79. DROP TABLE IF EXISTS `user`;
  80. CREATE TABLE `user` (
  81. `id` int UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键id',
  82. `username` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '用户名,唯一',
  83. `password` varchar(15) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '密码',
  84. PRIMARY KEY (`id`) USING BTREE,
  85. UNIQUE INDEX `username`(`username` ASC) USING BTREE
  86. ) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '用户表' ROW_FORMAT = Dynamic;
  87. -- ----------------------------
  88. -- Records of user
  89. -- ----------------------------
  90. INSERT INTO `user` VALUES (1, 'zs', '20021015');
  91. INSERT INTO `user` VALUES (2, 'lisi', '111');
  92. SET FOREIGN_KEY_CHECKS = 1;

4.建立数据库连接

导入jar包,首先建立一个lib文件夹,然后将jar包复制粘贴进去,在右键选择add as library

5.系统包结构如下

6.代码实现

Connect.java
  1. package com.java.main.sqlConnect;
  2. import java.sql.*;
  3. /*
  4. 数据库的连接
  5. */
  6. public class Connect {
  7. //查询表中的数据
  8. public static ResultSet queryAll(String sql) throws ClassNotFoundException, SQLException {
  9. Class.forName("com.mysql.cj.jdbc.Driver");
  10. Connection rt = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/lib_dtb", "root", "你的数据库密码");
  11. Statement st = rt.createStatement();
  12. ResultSet rs = st.executeQuery(sql);
  13. return rs;
  14. }
  15. //添加数据,删除数据,修改数据
  16. public static void edit(String sql) throws ClassNotFoundException, SQLException {
  17. Class.forName("com.mysql.cj.jdbc.Driver");
  18. Connection rt = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/lib_dtb", "root", "你的数据库密码");
  19. Statement st = rt.createStatement();
  20. st.executeUpdate(sql);
  21. }
  22. }
UserBuy.java
  1. package com.java.main.buyJavaBean;
  2. /*
  3. 购买记录javabean
  4. */
  5. public class UserBuy {
  6. private String name;
  7. private String libraryName;
  8. private String buyTime;
  9. public UserBuy() {
  10. }
  11. public UserBuy(String name, String libraryName, String buyTime) {
  12. this.name = name;
  13. this.libraryName = libraryName;
  14. this.buyTime = buyTime;
  15. }
  16. public String getName() {
  17. return name;
  18. }
  19. public void setName(String name) {
  20. this.name = name;
  21. }
  22. public String getLibraryName() {
  23. return libraryName;
  24. }
  25. public void setLibraryName(String libraryName) {
  26. this.libraryName = libraryName;
  27. }
  28. public String getBuyTime() {
  29. return buyTime;
  30. }
  31. public void setBuyTime(String buyTime) {
  32. this.buyTime = buyTime;
  33. }
  34. @Override
  35. public String toString() {
  36. return "UserBuy{" +
  37. "name='" + name + '\'' +
  38. ", libraryName='" + libraryName + '\'' +
  39. ", buyTime='" + buyTime + '\'' +
  40. '}';
  41. }
  42. }
Library.java
  1. package com.java.main.libraryJavaBean;
  2. /*
  3. 图书javabean
  4. */
  5. public class Library {
  6. private Integer id;
  7. private String name;
  8. private String author;
  9. private Double price;
  10. private Integer count;
  11. private String time;
  12. public Library() {
  13. }
  14. public Library(Integer id, String name, String author, Double price, Integer count, String time) {
  15. this.id = id;
  16. this.name = name;
  17. this.author = author;
  18. this.price = price;
  19. this.count = count;
  20. this.time = time;
  21. }
  22. public Integer getId() {
  23. return id;
  24. }
  25. public void setId(Integer id) {
  26. this.id = id;
  27. }
  28. public String getName() {
  29. return name;
  30. }
  31. public void setName(String name) {
  32. this.name = name;
  33. }
  34. public String getAuthor() {
  35. return author;
  36. }
  37. public void setAuthor(String author) {
  38. this.author = author;
  39. }
  40. public Double getPrice() {
  41. return price;
  42. }
  43. public void setPrice(Double price) {
  44. this.price = price;
  45. }
  46. public Integer getCount() {
  47. return count;
  48. }
  49. public void setCount(Integer count) {
  50. this.count = count;
  51. }
  52. public String getTime() {
  53. return time;
  54. }
  55. public void setTime(String time) {
  56. this.time = time;
  57. }
  58. @Override
  59. public String toString() {
  60. return "Library{" +
  61. "id=" + id +
  62. ", name='" + name + '\'' +
  63. ", author='" + author + '\'' +
  64. ", price=" + price +
  65. ", count=" + count +
  66. ", time='" + time + '\'' +
  67. '}';
  68. }
  69. }
Admin.java
  1. package com.java.main.perponJavaBean;
  2. /*
  3. 管理员javabean
  4. */
  5. public class Admin extends User {
  6. public Admin() {
  7. }
  8. public Admin(Integer id, String username, String password) {
  9. super(id, username, password);
  10. }
  11. }
User.java
  1. package com.java.main.perponJavaBean;
  2. /*
  3. 用户javabean
  4. */
  5. public class User {
  6. private Integer id;
  7. private String username;
  8. private String password;
  9. public User() {
  10. }
  11. public User(Integer id, String username, String password) {
  12. this.id = id;
  13. this.username = username;
  14. this.password = password;
  15. }
  16. public Integer getId() {
  17. return id;
  18. }
  19. public void setId(Integer id) {
  20. this.id = id;
  21. }
  22. public String getUsername() {
  23. return username;
  24. }
  25. public void setUsername(String username) {
  26. this.username = username;
  27. }
  28. public String getPassword() {
  29. return password;
  30. }
  31. public void setPassword(String password) {
  32. this.password = password;
  33. }
  34. @Override
  35. public String toString() {
  36. return "User{" +
  37. "id=" + id +
  38. ", username='" + username + '\'' +
  39. ", password='" + password + '\'' +
  40. '}';
  41. }
  42. }
AppRun.java
  1. package com.java.main.Screen;
  2. import com.java.main.perponJavaBean.Admin;
  3. import com.java.main.perponJavaBean.User;
  4. import com.java.main.sqlConnect.Connect;
  5. import com.java.main.utils.CodeUtil;
  6. import javax.swing.*;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.util.ArrayList;
  10. import java.util.Scanner;
  11. /**
  12. * @author leiGood浮生若梦
  13. * @since 2024-06-13 星期四 18:41:29
  14. * 用户注册登录
  15. * 管理员登录
  16. */
  17. public class AppRun {
  18. public AppRun() {
  19. System.out.println("=======================图书管理=======================");
  20. System.out.println("=====================欢迎您的到来!====================");
  21. System.out.println("=====================长风破浪会有时,==================");
  22. System.out.println("=====================直挂云帆济沧海。==================");
  23. }
  24. private static ArrayList<User> list = new ArrayList<>();
  25. public static void main(String[] args) throws SQLException, ClassNotFoundException {
  26. new AppRun();
  27. //存储用户集合
  28. list = new ArrayList<User>();
  29. //用户管理员
  30. ArrayList<Admin> UserAdmin = new ArrayList<>();
  31. //图书管理员
  32. ArrayList<Admin> libraryAdmin = new ArrayList<>();
  33. //获取数据库中的用户
  34. ResultSet rs1 = Connect.queryAll("select * from user");
  35. while (rs1.next()) {
  36. Integer id = rs1.getInt("id");
  37. String username = rs1.getString("username");
  38. String password = rs1.getString("password");
  39. list.add(new User(id, username, password));
  40. }
  41. //获取用户管理员
  42. ResultSet rs2 = Connect.queryAll("select * from admin");
  43. while (rs2.next()) {
  44. Integer id = rs2.getInt("id");
  45. String username = rs2.getString("username");
  46. String password = rs2.getString("password");
  47. UserAdmin.add(new Admin(id, username, password));
  48. }
  49. //获取图书管理员
  50. ResultSet rs3 = Connect.queryAll("select * from libAdmin");
  51. Scanner sc = new Scanner(System.in);
  52. while (rs3.next()) {
  53. Integer id = rs3.getInt("id");
  54. String username = rs3.getString("username");
  55. String password = rs3.getString("password");
  56. libraryAdmin.add(new Admin(id, username, password));
  57. }
  58. rs3.close();
  59. rs2.close();
  60. rs1.close();
  61. while (true) {
  62. System.out.println("请输入您的选择:1.登录 2注册 3用户管理管理员登录 4图书管理管理员登录 5退出");
  63. String numStr = sc.nextLine();
  64. try {
  65. int num = Integer.parseInt(numStr);
  66. switch (num) {
  67. case 1:
  68. Login(list);
  69. break;
  70. case 2:
  71. Register(list);
  72. break;
  73. case 3:
  74. UserAdminLogin(UserAdmin);
  75. break;
  76. case 4:
  77. LibraryAdminLogin(libraryAdmin);
  78. break;
  79. case 5:
  80. System.exit(WindowConstants.EXIT_ON_CLOSE);
  81. break;
  82. default:
  83. System.out.println("没有这个选项");
  84. break;
  85. }
  86. } catch (Exception e) {
  87. System.out.println("请输入数字,不能输入其它字符");
  88. }
  89. }
  90. }
  91. //图书管理员登录
  92. private static void LibraryAdminLogin(ArrayList<Admin> libraryAdmin) throws SQLException, ClassNotFoundException {
  93. Scanner sc = new Scanner(System.in);
  94. while (true) {
  95. System.out.println("请输入图书管理员账号");
  96. String username = sc.nextLine();
  97. int index = getLibraAdminIndex(libraryAdmin, username);
  98. if (index >= 0) {
  99. System.out.println("请输入图书管理员密码");
  100. String pwd = sc.nextLine();
  101. if (pwd.equals(libraryAdmin.get(index).getPassword())) {
  102. System.out.println("登陆成功!");
  103. com.java.main.Screen.LibraryAdmin.main();
  104. break;
  105. } else {
  106. System.out.println("密码输入错误,请重新输入~");
  107. }
  108. } else {
  109. System.out.println("图书管理员账号不存在~");
  110. }
  111. }
  112. }
  113. //用户管理员登录
  114. private static void UserAdminLogin(ArrayList<Admin> UserAdminList) throws SQLException, ClassNotFoundException {
  115. Scanner sc = new Scanner(System.in);
  116. while (true) {
  117. System.out.println("请输入用户管理员账号");
  118. String username = sc.nextLine();
  119. System.out.println("请输入用户管理员密码");
  120. String pwd = sc.nextLine();
  121. int adminIndex = getAdminIndex(UserAdminList, username);
  122. if (adminIndex >= 0) {
  123. if (UserAdminList.get(adminIndex).getUsername().equals(username) &&
  124. UserAdminList.get(adminIndex).getPassword().equals(pwd)) {
  125. System.out.println("登录成功!");
  126. UserAdmin.main();
  127. break;
  128. } else {
  129. System.out.println("密码输入错误!");
  130. }
  131. } else {
  132. System.out.println("该用户管理员账号不存在!");
  133. }
  134. }
  135. }
  136. //注册操作
  137. private static void Register(ArrayList<User> list) throws SQLException, ClassNotFoundException {
  138. while (true) {
  139. Scanner sc = new Scanner(System.in);
  140. System.out.println("请输入用户名:");
  141. String username = sc.next();
  142. //判断用户名是否存在
  143. int index = getIndex(list, username);
  144. if (index < 0) {
  145. //用户名不存在,可以执行注册
  146. System.out.println("请输入密码:");
  147. String pwd1 = sc.next();
  148. System.out.println("请再次输入密码:");
  149. String pwd2 = sc.next();
  150. if (pwd2.equals(pwd1)) {
  151. //两次密码输入一致
  152. String sql = "insert into user(username,password) values('" + username + "','" + pwd2 + "')";
  153. Connect.edit(sql);
  154. System.out.println("注册成功!");
  155. //刷新用户集合中的数据,在次进行查询获取用户数据
  156. //获取数据库中的用户
  157. ResultSet rs1 = Connect.queryAll("select * from user");
  158. while (rs1.next()) {
  159. Integer id = rs1.getInt("id");
  160. String uname = rs1.getString("username");
  161. String password = rs1.getString("password");
  162. list.add(new User(id, uname, password));
  163. }
  164. break;
  165. } else {
  166. //两次密码输入不一致
  167. System.out.println("两次密码输入不一致,请重新输入!");
  168. }
  169. } else {
  170. System.out.println("用户名已经存在,请重新输入~");
  171. }
  172. }
  173. }
  174. //登录操作
  175. private static void Login(ArrayList<User> list) throws SQLException, ClassNotFoundException {
  176. Scanner sc = new Scanner(System.in);
  177. while (true) {
  178. System.out.println("请输入用户名:");
  179. String username = sc.nextLine();
  180. //判断用户名是否存在
  181. int index = getIndex(list, username);
  182. if (index >= 0) {
  183. //用户名存在,执行下一步输入密码操作
  184. System.out.println("请输入密码:");
  185. String password = sc.nextLine();
  186. if (password.equals(list.get(index).getPassword())) {
  187. String codeStr = CodeUtil.getCode();
  188. System.out.println("当前验证码为:" + codeStr);
  189. System.out.println("请输入验证码:");
  190. String code = sc.nextLine();
  191. if (code.equalsIgnoreCase(codeStr)) {
  192. //密码输入正确
  193. System.out.println("登陆成功!");
  194. //传递用户登录的用户名
  195. new UserBuyLibrary(username).main();
  196. break;
  197. } else {
  198. System.out.println("验证码输入错误,请重新输入");
  199. }
  200. } else {
  201. System.out.println("密码输入错误,请重新输入");
  202. }
  203. } else {
  204. //用户名不存在
  205. System.out.println("用户名未注册,请注册后再来使用~");
  206. break;
  207. }
  208. }
  209. }
  210. //判断用户在登录或者注册时用户名是否存在,存在返回对应的索引,否则返回-1
  211. public static int getIndex(ArrayList<User> list, String username) {
  212. for (int i = 0; i < list.size(); i++) {
  213. if (username.equals(list.get(i).getUsername())) {
  214. //存在
  215. return i;
  216. }
  217. }
  218. //不存在
  219. return -1;
  220. }
  221. //判断用户管理员登录
  222. public static int getAdminIndex(ArrayList<Admin> list, String username) {
  223. for (int i = 0; i < list.size(); i++) {
  224. if (username.equals(list.get(i).getUsername())) {
  225. //存在
  226. return i;
  227. }
  228. }
  229. //不存在
  230. return -1;
  231. }
  232. //判断图书管理员登录
  233. public static int getLibraAdminIndex(ArrayList<Admin> list, String username) {
  234. for (int i = 0; i < list.size(); i++) {
  235. if (username.equals(list.get(i).getUsername())) {
  236. //存在
  237. return i;
  238. }
  239. }
  240. //不存在
  241. return -1;
  242. }
  243. }
LibraryAdmin.java
  1. package com.java.main.Screen;
  2. import com.java.main.libraryJavaBean.Library;
  3. import com.java.main.sqlConnect.Connect;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Timestamp;
  7. import java.text.SimpleDateFormat;
  8. import java.util.ArrayList;
  9. import java.util.Date;
  10. import java.util.Scanner;
  11. /*
  12. 图书管理员操作代码
  13. */
  14. public class LibraryAdmin {
  15. public LibraryAdmin() throws SQLException, ClassNotFoundException {
  16. //获取图书库存为0的书籍
  17. ResultSet rs = Connect.queryAll("select * from library where count='0'");
  18. while (rs.next()) {
  19. System.out.println("========================图书" + rs.getString("name") + "的库存为0了,请增加库存!!!" +
  20. "========================");
  21. }
  22. rs.close();
  23. }
  24. public static void main() throws SQLException, ClassNotFoundException {
  25. new LibraryAdmin();
  26. Scanner sc = new Scanner(System.in);
  27. //图书集合
  28. ArrayList<Library> list = new ArrayList<>();
  29. ResultSet rs = Connect.queryAll("select * from library");
  30. while (rs.next()) {
  31. Integer id = rs.getInt("id");
  32. String libraryName = rs.getString("name");
  33. String authorName = rs.getString("author");
  34. Double price = rs.getDouble("price");
  35. Integer count = rs.getInt("count");
  36. //获取上架时间
  37. Timestamp shelf_time = rs.getTimestamp("Shelf_time");
  38. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  39. String time = sdf.format(shelf_time);
  40. list.add(new Library(id, libraryName, authorName, price, count, time));
  41. }
  42. rs.close();
  43. System.out.println("请输入你的选择:1查看所有图书信息 2增加图书 3修改图书库存 4退出");
  44. String numStr = sc.nextLine();
  45. try {
  46. switch (Integer.parseInt(numStr)) {
  47. case 1:
  48. LookForInfo(list);
  49. break;
  50. case 2:
  51. InsertBook(list);
  52. break;
  53. case 3:
  54. EditBook(list);
  55. break;
  56. case 4:
  57. new AppRun();
  58. break;
  59. default:
  60. System.out.println("没有这个选项~");
  61. break;
  62. }
  63. } catch (Exception e) {
  64. System.out.println("请输入数字!不能输入其它字符~");
  65. }
  66. }
  67. //修改图书库存
  68. private static void EditBook(ArrayList<Library> list) throws SQLException, ClassNotFoundException {
  69. Scanner sc = new Scanner(System.in);
  70. while (true) {
  71. System.out.println("请输入要修改图书库存的图书名字:");
  72. String name = sc.nextLine();
  73. int index = getResult(list, name);
  74. if (index >= 0) {
  75. //存在
  76. System.out.println("请输入增加的库存数量:");
  77. Integer count1 = sc.nextInt();
  78. Integer count2 = list.get(index).getCount();
  79. Integer count = count1 + count2;
  80. String sql = "update library set count='" + count + "' where name='" + name + "'";
  81. Connect.edit(sql);
  82. System.out.println("库存添加成功!");
  83. main();
  84. break;
  85. } else {
  86. System.out.println("不存在该图书!请重新输入~");
  87. }
  88. }
  89. }
  90. //增加图书
  91. private static void InsertBook(ArrayList<Library> list) throws SQLException, ClassNotFoundException {
  92. Scanner sc = new Scanner(System.in);
  93. System.out.println("请输入要增加的图书名:");
  94. String name = sc.next();
  95. System.out.println("请输入作者名:");
  96. String author = sc.next();
  97. System.out.println("请输入价格:");
  98. Double price = sc.nextDouble();
  99. System.out.println("请输入图书数量:");
  100. Integer count = sc.nextInt();
  101. //获取上架时间
  102. Date d = new Date();
  103. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  104. String time = sdf.format(d);
  105. String sql = " insert into library(name, author, price, count, Shelf_time) values('" + name + "','" + author + "','" +
  106. price + "','" + count + "','" + time + "')";
  107. Connect.edit(sql);
  108. System.out.println("添加成功!");
  109. main();
  110. }
  111. //查询图书信息
  112. private static void LookForInfo(ArrayList<Library> list) throws SQLException, ClassNotFoundException {
  113. System.out.println("图书信息如下:");
  114. for (Library library : list) {
  115. System.out.println("图书编号:" + library.getId() + " 图书名字:《" + library.getName() + " 》 图书作者:" + library.getAuthor()
  116. + " 图书价格:" + library.getPrice() + " 图书库存" + library.getCount() + " 图书上架时间:" + library.getTime());
  117. }
  118. main();
  119. }
  120. //判断图书是否存在
  121. public static Integer getResult(ArrayList<Library> list, String name) {
  122. for (int i = 0; i < list.size(); i++) {
  123. if (name.equals(list.get(i).getName())) {
  124. //存在
  125. return i;
  126. }
  127. }
  128. return -1;
  129. }
  130. }
UserAdmin.java
  1. package com.java.main.Screen;
  2. import com.java.main.perponJavaBean.User;
  3. import com.java.main.sqlConnect.Connect;
  4. import javax.swing.*;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.util.ArrayList;
  8. import java.util.Scanner;
  9. import java.util.function.Consumer;
  10. /*
  11. 用户管理员
  12. 实现对用户信息的增删查改
  13. */
  14. public class UserAdmin {
  15. public static void main() throws SQLException, ClassNotFoundException {
  16. Scanner sc = new Scanner(System.in);
  17. //用户信息
  18. ArrayList<User> list = new ArrayList<>();
  19. String sql = "select * from user";
  20. //连接数据库
  21. ResultSet rs = Connect.queryAll(sql);
  22. while (rs.next()) {
  23. list.add(new User(rs.getInt("id"), rs.getString("username"), rs.getString("password")));
  24. }
  25. rs.close();
  26. System.out.println("请输入你的选择:1查看用户信息 2删除用户信息 3修改用户信息 4增加用户信息 5退出");
  27. String numStr = sc.nextLine();
  28. try {
  29. int num = Integer.parseInt(numStr);
  30. switch (num) {
  31. case 1:
  32. LookUser(list);
  33. break;
  34. case 2:
  35. Delete(list);
  36. break;
  37. case 3:
  38. UpdateUser(list);
  39. break;
  40. case 4:
  41. InsertUser(list);
  42. break;
  43. case 5:
  44. System.exit(WindowConstants.EXIT_ON_CLOSE);
  45. break;
  46. default:
  47. System.out.println("没有这个选项!");
  48. main();
  49. break;
  50. }
  51. } catch (Exception e) {
  52. System.out.println("请输入数字!");
  53. main();
  54. }
  55. }
  56. //修改用户信息
  57. private static void UpdateUser(ArrayList<User> list) throws SQLException, ClassNotFoundException {
  58. Scanner sc = new Scanner(System.in);
  59. while (true) {
  60. System.out.println("请输入要修改的用户id:");
  61. int id = sc.nextInt();
  62. boolean flag = getResult(list, id);
  63. if (flag) {
  64. //输入的id存在
  65. System.out.println("请输入修改后的用户名:");
  66. String username = sc.next();
  67. System.out.println("请修改后的密码:");
  68. String pwd = sc.next();
  69. String sql = "update user set username='" + username + "',password='" + pwd + "' where id=" + id;
  70. Connect.edit(sql);
  71. System.out.println("用户信息修改成功!");
  72. main();
  73. break;
  74. } else {
  75. System.out.println("输入的用户id在数据库中不存在,请重新输入~");
  76. }
  77. }
  78. }
  79. //增加用户信息
  80. private static void InsertUser(ArrayList<User> list) throws SQLException, ClassNotFoundException {
  81. Scanner sc = new Scanner(System.in);
  82. while (true) {
  83. System.out.println("请输入用户名:");
  84. String username = sc.nextLine();
  85. boolean flag = getUserFlag(list, username);
  86. if (!flag) {
  87. //输入的用户名在数据库不存在,执行增加操作
  88. System.out.println("请输入密码:");
  89. String pwd = sc.nextLine();
  90. String sql = "insert into user(username,password) values('" + username + "','" + pwd + "')";
  91. Connect.edit(sql);
  92. System.out.println("添加成功!");
  93. main();
  94. break;
  95. } else {
  96. System.out.println("输入的用户名在数据库中已经存在,请重新输入!");
  97. }
  98. }
  99. }
  100. //删除用户信息
  101. private static void Delete(ArrayList<User> list) throws SQLException, ClassNotFoundException {
  102. Scanner sc = new Scanner(System.in);
  103. while (true) {
  104. System.out.println("请输入你要删除的用户id");
  105. int id = sc.nextInt();
  106. boolean flag = getResult(list, id);
  107. if (flag) {
  108. //id存在
  109. String sql = "delete from user where id=" + id;
  110. Connect.edit(sql);
  111. System.out.println("删除成功!");
  112. main();
  113. break;
  114. } else {
  115. //id不存在
  116. System.out.println("输入的用户id不存在,请重新输入");
  117. }
  118. }
  119. }
  120. //查看用户信息
  121. private static void LookUser(ArrayList<User> list) throws SQLException, ClassNotFoundException {
  122. //遍历用户集合
  123. System.out.println("用户信息如下");
  124. list.forEach(new Consumer<User>() {
  125. @Override
  126. public void accept(User user) {
  127. System.out.println(user.getId() + " " + user.getUsername() + " " + user.getPassword());
  128. }
  129. });
  130. UserAdmin.main();
  131. }
  132. //在删除用户信息的时候,判断用户输入的id是否存在
  133. public static boolean getResult(ArrayList<User> list, int id) {
  134. for (int i = 0; i < list.size(); i++) {
  135. if (list.get(i).getId() == id) {
  136. //存在
  137. return true;
  138. }
  139. }
  140. //不存在
  141. return false;
  142. }
  143. //查询输入的用户名是否存在
  144. private static boolean getUserFlag(ArrayList<User> list, String username) {
  145. for (int i = 0; i < list.size(); i++) {
  146. if (username.equals(list.get(i).getUsername())) {
  147. //用户名存在
  148. return true;
  149. }
  150. }
  151. //不存在
  152. return false;
  153. }
  154. }
UserBuyLibrary.java
  1. package com.java.main.Screen;
  2. import com.java.main.buyJavaBean.UserBuy;
  3. import com.java.main.libraryJavaBean.Library;
  4. import com.java.main.sqlConnect.Connect;
  5. import javax.swing.*;
  6. import java.sql.*;
  7. import java.text.SimpleDateFormat;
  8. import java.util.ArrayList;
  9. import java.util.Date;
  10. import java.util.Scanner;
  11. import java.util.function.Consumer;
  12. /*
  13. 用户进行图书购买查询操作代码
  14. */
  15. public class UserBuyLibrary {
  16. private static String name;
  17. public UserBuyLibrary(String name) {
  18. this.name = name;
  19. System.out.println("欢迎来到图书商城");
  20. System.out.println("当前的登录用户:" + name);
  21. }
  22. public static void main() throws SQLException, ClassNotFoundException {
  23. //创建图书集合
  24. ArrayList<Library> list = new ArrayList<>();
  25. //用户购买记录
  26. ArrayList<UserBuy> buyList = new ArrayList<>();
  27. //连接数据库 将图书信息添加到集合中
  28. ResultSet rs1 = Connect.queryAll("select * from library");
  29. while (rs1.next()) {
  30. Integer id = rs1.getInt("id");
  31. String libraryName = rs1.getString("name");
  32. String authorName = rs1.getString("author");
  33. Double price = rs1.getDouble("price");
  34. Integer count = rs1.getInt("count");
  35. //获取上架时间
  36. Timestamp shelf_time = rs1.getTimestamp("Shelf_time");
  37. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  38. String time = sdf.format(shelf_time);
  39. list.add(new Library(id, libraryName, authorName, price, count, time));
  40. }
  41. rs1.close();
  42. //获取用户购买记录
  43. String sql = "select * from buy_table";
  44. ResultSet rs2 = Connect.queryAll(sql);
  45. while (rs2.next()) {
  46. Timestamp buyTime = rs2.getTimestamp("buy_time");
  47. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  48. String time = sdf.format(buyTime);
  49. buyList.add(new UserBuy(rs2.getString("name"), rs2.getString("library"), time));
  50. }
  51. rs2.close();
  52. Scanner sc = new Scanner(System.in);
  53. System.out.println("请输入你的选择 1.查看所有图书信息 2.购买书籍 3.查询购买记录 4.退出");
  54. try {
  55. int num = sc.nextInt();
  56. switch (num) {
  57. case 1:
  58. LookForLibraryInfo(list);
  59. break;
  60. case 2:
  61. BuyBook(list);
  62. break;
  63. case 3:
  64. FindBuy(buyList);
  65. break;
  66. case 4:
  67. System.exit(WindowConstants.EXIT_ON_CLOSE);
  68. break;
  69. default:
  70. System.out.println("没有这个选项");
  71. main();
  72. break;
  73. }
  74. } catch (Exception e) {
  75. System.out.println("请输入数字,不能输入其它字符~");
  76. main();
  77. }
  78. }
  79. //查询当前用户购买的图书信息
  80. private static void FindBuy(ArrayList<UserBuy> buyList) throws SQLException, ClassNotFoundException {
  81. System.out.println("购买记录如下所示:");
  82. int index = getBuyIndex(buyList, name);
  83. if (index >= 0) {
  84. buyList.stream().forEach(userBuy -> System.out.println("用户" + userBuy.getName() + "于" + userBuy.getBuyTime() + "购买了" + userBuy.getLibraryName()));
  85. } else {
  86. System.out.println("您还没有购买任何书籍");
  87. }
  88. main();
  89. }
  90. //购买书籍
  91. private static void BuyBook(ArrayList<Library> list) throws SQLException, ClassNotFoundException {
  92. Scanner sc = new Scanner(System.in);
  93. while (true) {
  94. System.out.println("请输入你要购买的图书名字");
  95. String libraryName = sc.next();
  96. int index = getIndex(list, libraryName);
  97. if (index >= 0) {
  98. //图书存在
  99. //判断图书数量是否大于0
  100. Integer count = list.get(index).getCount();
  101. if (count > 0) {
  102. //图书有剩余容量
  103. System.out.println("请输入购买数量:");
  104. Integer amount = sc.nextInt();
  105. //判断购买数量是否在图书库存范围内
  106. if (amount > 0 && amount <= list.get(index).getCount()) {
  107. //在库存范围内
  108. //获取剩余书籍数量
  109. Integer remainingCount = list.get(index).getCount() - amount;
  110. //更新数据库
  111. String sql = "update library set count='" + remainingCount + "'where name='" + libraryName + "'";
  112. Connect.edit(sql);
  113. System.out.println("购买成功!");
  114. //获取当前购买时间
  115. Date d = new Date();
  116. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  117. String datetime = sdf.format(d);
  118. //将用户名和图书名字插入数据库中
  119. Connect.edit("insert into buy_table values ('" + name + "','" + libraryName + "','" + datetime + "')");
  120. main();
  121. break;
  122. } else {
  123. System.out.println("超出库存范围,请重新输入~");
  124. }
  125. } else {
  126. //图书已经卖完了
  127. System.out.println("很抱歉,图书已经卖完了,请买别的书籍吧~");
  128. }
  129. } else {
  130. System.out.println("不存在该书籍!请重新输入~");
  131. }
  132. }
  133. }
  134. //查询书籍
  135. private static void LookForLibraryInfo(ArrayList<Library> list) throws SQLException, ClassNotFoundException {
  136. System.out.println("图书信息如下:");
  137. list.stream().forEach(new Consumer<Library>() {
  138. @Override
  139. public void accept(Library library) {
  140. System.out.println("图书编号:" + library.getId() + " 图书名字: 《" + library.getName() + "》 图书作者:" + library.getAuthor()
  141. + " 图书价格:" + library.getPrice() + " 图书库存" + library.getCount() + " 图书上架时间:" + library.getTime());
  142. }
  143. });
  144. main();
  145. }
  146. //判断图书名字在集合在是否存在
  147. public static int getIndex(ArrayList<Library> list, String name) {
  148. for (int i = 0; i < list.size(); i++) {
  149. if (name.equals(list.get(i).getName())) {
  150. return i;
  151. }
  152. }
  153. return -1;
  154. }
  155. //判断用户在是否存在在购买集合中
  156. private static int getBuyIndex(ArrayList<UserBuy> buyList, String name) {
  157. for (int i = 0; i < buyList.size(); i++) {
  158. if (name.equals(buyList.get(i).getName())) {
  159. return i;
  160. }
  161. }
  162. return -1;
  163. }
  164. }
CodeUtil.java
  1. package com.java.main.utils;
  2. import java.util.ArrayList;
  3. import java.util.Random;
  4. /*
  5. 验证码
  6. */
  7. public class CodeUtil {
  8. public static String getCode() {
  9. ArrayList<Character> list = new ArrayList<>();
  10. for (int i = 0; i < 26; i++) {
  11. list.add((char) ('a' + i));
  12. }
  13. for (int i = 0; i < 26; i++) {
  14. list.add((char) ('A' + i));
  15. }
  16. StringBuilder sb = new StringBuilder();
  17. for (int i = 0; i < 4; i++) {
  18. int index = new Random().nextInt(list.size());
  19. sb.append(list.get(index));
  20. }
  21. for (int i = 0; i < 2; i++) {
  22. int number = new Random().nextInt(10);
  23. sb.append(number);
  24. }
  25. return sb.toString();
  26. }
  27. }

7.Gitee

Java控制台图书管理系统: 本系统是Java控制台图书管理系统,实现了用户的注册登录,用户管理员的登录,图书管理员的登录,用户可以购买图书和查看图书购买记录,用户管理员可以对用户信息进行增删查改,图书管理员可以查看图书信息,以及上架新的图书和修改图书库存。icon-default.png?t=N7T8https://gitee.com/ouyanlei/consoleLibrarySystem.git

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/747781
推荐阅读
相关标签
  

闽ICP备14008679号