程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

Java连接数据库实现图书馆管理系统(详细教程)

发布于2023-06-19 22:17     阅读(1516)     评论(0)     点赞(5)     收藏(3)


该功能用到的软件为 IDEA 、Navicat 、云服务器(非必须)

一、建立数据库

在自己的服务器或者电脑本机安装数据库系统,本次系统演示的数据库版本为5.6。

1.创建图书管理数据库library 字符集为:utf8 -utf8_general_ci

  1. /*
  2. Navicat Premium Data Transfer
  3. Source Server : local
  4. Source Server Type : MySQL
  5. Source Server Version : 50739
  6. Source Host : 121.37.205.242:3306
  7. Source Schema : library
  8. Target Server Type : MySQL
  9. Target Server Version : 50739
  10. File Encoding : 65001
  11. Date: 31/01/2023 11:23:05
  12. */
  13. SET NAMES utf8mb4;
  14. SET FOREIGN_KEY_CHECKS = 0;
  15. -- ----------------------------
  16. -- Table structure for amerce
  17. -- ----------------------------
  18. DROP TABLE IF EXISTS `amerce`;
  19. CREATE TABLE `amerce` (
  20. `rnumber` char(20) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '读者编号',
  21. `rname` varchar(35) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '读者名字',
  22. `bnumber` int(11) DEFAULT NULL COMMENT '图书编号',
  23. `bname` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '图书名称',
  24. `quantity` int(11) DEFAULT 0 COMMENT '数量',
  25. `bstime` datetime(0) DEFAULT NULL COMMENT '还书和借书时间',
  26. `money` int(11) DEFAULT NULL COMMENT '罚款金额'
  27. ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '罚款信息表' ROW_FORMAT = Compact;
  28. -- ----------------------------
  29. -- Table structure for books
  30. -- ----------------------------
  31. DROP TABLE IF EXISTS `books`;
  32. CREATE TABLE `books` (
  33. `number` int(11) NOT NULL AUTO_INCREMENT COMMENT '图书编号',
  34. `name` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '图书名称',
  35. `category` int(11) DEFAULT NULL COMMENT '图书类别',
  36. `author` varchar(35) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '作者',
  37. `press` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '出版社',
  38. `pdate` date DEFAULT NULL COMMENT '出版日期',
  39. `wdate` datetime(0) DEFAULT NULL COMMENT '入库日期',
  40. `books` int(11) DEFAULT NULL COMMENT '库存总数量',
  41. `extant` int(11) DEFAULT 0 COMMENT '现存数量',
  42. PRIMARY KEY (`number`) USING BTREE,
  43. UNIQUE INDEX `books_name`(`name`) USING BTREE,
  44. INDEX `books_category`(`category`) USING BTREE,
  45. CONSTRAINT `books_category` FOREIGN KEY (`category`) REFERENCES `category` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
  46. ) ENGINE = InnoDB AUTO_INCREMENT = 214 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '图书信息表' ROW_FORMAT = Compact;
  47. -- ----------------------------
  48. -- Table structure for borrow
  49. -- ----------------------------
  50. DROP TABLE IF EXISTS `borrow`;
  51. CREATE TABLE `borrow` (
  52. `rnumber` char(20) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '读者编号',
  53. `bnumber` int(11) DEFAULT NULL COMMENT '图书编号',
  54. `quantity` int(11) DEFAULT 1 COMMENT '数量',
  55. `btime` datetime(0) DEFAULT NULL COMMENT '借书时间'
  56. ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '借书信息表' ROW_FORMAT = Compact;
  57. -- ----------------------------
  58. -- Table structure for category
  59. -- ----------------------------
  60. DROP TABLE IF EXISTS `category`;
  61. CREATE TABLE `category` (
  62. `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '类别ID',
  63. `name` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '类别名称',
  64. PRIMARY KEY (`id`) USING BTREE
  65. ) ENGINE = InnoDB AUTO_INCREMENT = 51 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '图书类别表' ROW_FORMAT = Compact;
  66. -- ----------------------------
  67. -- Table structure for reader
  68. -- ----------------------------
  69. DROP TABLE IF EXISTS `reader`;
  70. CREATE TABLE `reader` (
  71. `number` char(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '学号',
  72. `name` varchar(35) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '读者名字',
  73. `sex` varchar(4) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  74. `kind` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '读者种类',
  75. `rdate` datetime(0) DEFAULT NULL COMMENT '登记时间',
  76. `password` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  77. PRIMARY KEY (`number`) USING BTREE
  78. ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '读者信息表' ROW_FORMAT = Compact;
  79. -- ----------------------------
  80. -- Table structure for still
  81. -- ----------------------------
  82. DROP TABLE IF EXISTS `still`;
  83. CREATE TABLE `still` (
  84. `rnumber` char(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '读者编号',
  85. `bnumber` int(11) NOT NULL COMMENT '图书编号',
  86. `quantity` int(11) DEFAULT 1 COMMENT '数量',
  87. `stime` datetime(0) DEFAULT NULL COMMENT '还书时间'
  88. ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '还书信息表' ROW_FORMAT = Compact;
  89. -- ----------------------------
  90. -- View structure for view_aa
  91. -- ----------------------------
  92. DROP VIEW IF EXISTS `view_aa`;
  93. CREATE ALGORITHM = UNDEFINED DEFINER = `root`@`localhost` SQL SECURITY DEFINER VIEW `view_aa` AS select `b`.`rnumber` AS `rnumber`,`r`.`name` AS `rname`,`b`.`bnumber` AS `bnumber`,`bs`.`name` AS `bname`,`b`.`btime` AS `bstime` from ((`borrow` `b` join `books` `bs`) join `reader` `r`) where ((`b`.`rnumber` = `r`.`number`) and (`b`.`bnumber` = `bs`.`number`));
  94. -- ----------------------------
  95. -- Procedure structure for Bookreturn
  96. -- ----------------------------
  97. DROP PROCEDURE IF EXISTS `Bookreturn`;
  98. delimiter ;;
  99. CREATE DEFINER=`root`@`localhost` PROCEDURE `Bookreturn`(in Borrowid char(20),in bookid int ,quant int)
  100. begin
  101. declare cone int default 0;
  102. set cone =(select quantity from borrow
  103. where rnumber =Borrowid and bnumber=bookid and quantity>0
  104. ORDER BY btime limit 1)-quant;
  105. -- 向还书表插入信息
  106. INSERT INTO still(rnumber,bnumber,quantity,stime)
  107. VALUES(Borrowid,bookid,quant,now());
  108. -- 图书表数量+还书的数量
  109. update books set extant=extant+quant
  110. where number=bookid ;
  111. -- 还书后减去对应的数量
  112. update borrow set quantity=quantity-quant
  113. where rnumber =Borrowid and bnumber=bookid
  114. ORDER BY btime limit 1;
  115. -- while循环,如果借书表数值为负数,则循环减去正数的数值
  116. WHILE cone<0 DO
  117. set cone=cone+1;
  118. update borrow set quantity =quantity-1
  119. where quantity>0 and rnumber =Borrowid and bnumber=bookid
  120. ORDER BY btime limit 1;
  121. update borrow set quantity=quantity+1
  122. WHERE quantity<0 and rnumber =Borrowid and bnumber=bookid
  123. ORDER BY btime limit 1;
  124. END WHILE;
  125. -- 把等于0的数值删除
  126. delete from borrow
  127. where quantity=0;
  128. end
  129. ;;
  130. delimiter ;
  131. -- ----------------------------
  132. -- Procedure structure for Borrowbooks
  133. -- ----------------------------
  134. DROP PROCEDURE IF EXISTS `Borrowbooks`;
  135. delimiter ;;
  136. CREATE DEFINER=`root`@`localhost` PROCEDURE `Borrowbooks`(Borrowid char(20),bookid int ,quant int)
  137. begin
  138. INSERT INTO borrow(rnumber,bnumber,quantity,btime)
  139. VALUES(Borrowid,bookid,quant,now());
  140. -- update borrow set quantity=quantity+quant WHERE Borrowid=rnumber and bookid=bnumber;
  141. update books set extant=extant-quant
  142. where number=bookid;
  143. end
  144. ;;
  145. delimiter ;
  146. -- ----------------------------
  147. -- Procedure structure for Insertbooks
  148. -- ----------------------------
  149. DROP PROCEDURE IF EXISTS `Insertbooks`;
  150. delimiter ;;
  151. CREATE DEFINER=`root`@`localhost` PROCEDURE `Insertbooks`(b varchar(40),c int ,d varchar(35),e varchar(40),f date,h int)
  152. begin
  153. INSERT INTO books (number,`name`,category,author,press,pdate,wdate,books)
  154. VALUES (null,b,c,d,e,f,now(),h) ON DUPLICATE KEY UPDATE books=books+h;
  155. update books set extant = extant+h where `name`= b;
  156. end
  157. ;;
  158. delimiter ;
  159. -- ----------------------------
  160. -- Procedure structure for proc_2
  161. -- ----------------------------
  162. DROP PROCEDURE IF EXISTS `proc_2`;
  163. delimiter ;;
  164. CREATE DEFINER=`root`@`localhost` PROCEDURE `proc_2`()
  165. BEGIN
  166. drop view if exists view_aa;
  167. create view view_aa as
  168. select b.rnumber,r.`name` rname,b.bnumber,bs.`name` bname,b.btime as bstime from borrow b,books bs,reader r
  169. where b.rnumber=r.number and b.bnumber=bs.number;
  170. insert into amerce(rnumber,rname,bnumber,bname,money,bstime)
  171. select rnumber,rname,bnumber,bname,count(*) as money,bstime from view_aa;
  172. end
  173. ;;
  174. delimiter ;
  175. -- ----------------------------
  176. -- Procedure structure for repayment
  177. -- ----------------------------
  178. DROP PROCEDURE IF EXISTS `repayment`;
  179. delimiter ;;
  180. CREATE DEFINER=`root`@`%` PROCEDURE `repayment`(rnum char(20),bnum int ,quant int)
  181. begin
  182. declare cone int default 0;
  183. -- 减去对应的数量
  184. update amerce set quantity=quantity-quant
  185. where rnumber = rnum and bnumber = bnum
  186. ORDER BY bstime limit 1;
  187. -- 把负数赋值给cone
  188. select quantity into cone from amerce where quantity<0;
  189. -- while循环,如果cone为负数,则循环减去正数的数值
  190. WHILE cone<0 DO
  191. set cone=cone+1;
  192. -- 钱数 money-(money/quantity)
  193. update amerce set money=money-(money/quantity)
  194. where quantity>0 and rnumber = rnum and bnumber = bnum
  195. ORDER BY bstime limit 1;
  196. -- 正数 -1
  197. update amerce set quantity =quantity-1
  198. where quantity>0 and rnumber = rnum and bnumber = bnum
  199. ORDER BY bstime limit 1;
  200. -- 负数 +1
  201. update amerce set quantity=quantity+1
  202. WHERE quantity<0 and rnumber = rnum and bnumber = bnum
  203. ORDER BY bstime limit 1;
  204. END WHILE;
  205. -- 把等于0的数值删除
  206. delete from amerce where quantity=0;
  207. end
  208. ;;
  209. delimiter ;
  210. -- ----------------------------
  211. -- Procedure structure for updateMyTest
  212. -- ----------------------------
  213. DROP PROCEDURE IF EXISTS `updateMyTest`;
  214. delimiter ;;
  215. CREATE DEFINER=`root`@`localhost` PROCEDURE `updateMyTest`(quant int)
  216. BEGIN
  217. declare cone int default 0;
  218. set cone =(select quantity from borrow
  219. where quantity>0
  220. ORDER BY btime limit 1)-quant;
  221. SELECT * from borrow where quantity=cone;
  222. -- WHILE cone<0 DO
  223. -- set cone=cone+1;
  224. -- update borrow set quantity =quantity-1
  225. -- where quantity>0
  226. -- ORDER BY btime limit 1;
  227. -- update borrow set quantity=quantity+1
  228. -- WHERE quantity<0
  229. -- ORDER BY btime limit 1;
  230. -- END WHILE;
  231. end
  232. ;;
  233. delimiter ;
  234. -- ----------------------------
  235. -- Triggers structure for table still
  236. -- ----------------------------
  237. DROP TRIGGER IF EXISTS `trigger_still_amerce`;
  238. delimiter ;;
  239. CREATE DEFINER = `root`@`localhost` TRIGGER `trigger_still_amerce` BEFORE INSERT ON `still` FOR EACH ROW -- 行级触发器
  240. BEGIN
  241. replace into amerce(rnumber,rname,bnumber,bname,quantity,bstime,money)
  242. select borrow.rnumber rnumber,reader.name rname,
  243. borrow.bnumber bnumber,books.name bname,
  244. borrow.quantity quantity,reader.rdate bstime,
  245. 0.1*datediff(now(),borrow.btime)-30 money
  246. from borrow,reader,books
  247. where borrow.rnumber=reader.number and borrow.bnumber=books.number
  248. and datediff(now(),borrow.btime)>=30
  249. and books.number=borrow.bnumber;
  250. END
  251. ;;
  252. delimiter ;
  253. SET FOREIGN_KEY_CHECKS = 1;

2.设计好将要使用的sql语句

视图、存储过程和触发器,在上面创建数据库的时候建好了,下面是一下视图、存储过程和触发器的创建语句。

  1. -- 1、添加图书表书籍 -- 完成
  2. CREATE UNIQUE index books_name on books(name);
  3. delimiter //
  4. create procedure Insertbooks(b varchar(40),c int ,d varchar(35),e varchar(40),f date,h int)
  5. begin
  6. INSERT INTO books (number,`name`,category,author,press,pdate,wdate,books)
  7. VALUES (null,b,c,d,e,f,now(),h) ON DUPLICATE KEY UPDATE books=books+h;
  8. update books set extant = extant+h where `name`= b;
  9. end; //
  10. delimiter ;
  11. call Insertbooks('白鹿原','1','张三','人民出版社','2022-1-9','7');
  12. -- 2、添加图书类别信息 -- 完成
  13. INSERT INTO category()
  14. VALUES(1,'话剧');
  15. -- 3、添加读者信息 -- 完成
  16. INSERT INTO reader(number,name,sex,kind,rdate)
  17. VALUES('20215101020051','翠花','女','学生',now());
  18. -- 4、读者借书 -- 完成
  19. delimiter //
  20. create procedure Borrowbooks(Borrowid char(20),bookid int ,quant int)
  21. begin
  22. INSERT INTO borrow(rnumber,bnumber,quantity,btime)
  23. VALUES(Borrowid,bookid,quant,now());
  24. -- update borrow set quantity=quantity+quant WHERE Borrowid=rnumber and bookid=bnumber;
  25. update books set extant=extant-quant
  26. where number=bookid;
  27. end ; //
  28. delimiter ;
  29. Call Borrowbooks('20215101020051','203','50');
  30. -- 5、读者还书 -- 完成
  31. delimiter //
  32. create procedure Bookreturn(in Borrowid char(20),in bookid int ,quant int)
  33. begin
  34. declare cone int default 0;
  35. set cone =(select quantity from borrow
  36. where rnumber =Borrowid and bnumber=bookid and quantity>0
  37. ORDER BY btime limit 1)-quant;
  38. -- 向还书表插入信息
  39. INSERT INTO still(rnumber,bnumber,quantity,stime)
  40. VALUES(Borrowid,bookid,quant,now());
  41. -- 图书表数量+还书的数量
  42. update books set extant=extant+quant
  43. where number=bookid ;
  44. -- 还书后减去对应的数量
  45. update borrow set quantity=quantity-quant
  46. where rnumber =Borrowid and bnumber=bookid
  47. ORDER BY btime limit 1;
  48. -- while循环,如果借书表数值为负数,则循环减去正数的数值
  49. WHILE cone<0 DO
  50. set cone=cone+1;
  51. update borrow set quantity =quantity-1
  52. where quantity>0 and rnumber =Borrowid and bnumber=bookid
  53. ORDER BY btime limit 1;
  54. update borrow set quantity=quantity+1
  55. WHERE quantity<0 and rnumber =Borrowid and bnumber=bookid
  56. ORDER BY btime limit 1;
  57. END WHILE;
  58. -- 把等于0的数值删除
  59. delete from borrow
  60. where quantity=0;
  61. end ; //
  62. delimiter ;
  63. Call Bookreturn('20215101020051','203','5');
  64. drop procedure Borrowbooks;
  65. drop TRIGGER trigger_still_amerce;
  66. -- 6、读者还书时超过30天罚款 -- 完成
  67. -- 设置还书的时候触发,往罚款表插入数据
  68. delimiter //
  69. CREATE TRIGGER trigger_still_amerce
  70. BEFORE INSERT ON still FOR EACH ROW -- 行级触发器
  71. BEGIN
  72. replace into amerce(rnumber,rname,bnumber,bname,quantity,bstime,money)
  73. select borrow.rnumber rnumber,reader.name rname,
  74. borrow.bnumber bnumber,books.name bname,
  75. borrow.quantity quantity,reader.rdate bstime,
  76. 0.1*datediff(now(),borrow.btime)-30 money
  77. from borrow,reader,books
  78. where borrow.rnumber=reader.number and borrow.bnumber=books.number
  79. and datediff(now(),borrow.btime)>=30
  80. and books.number=borrow.bnumber;
  81. END; //
  82. delimiter ;
  83. show TRIGGERS;
  84. -- 7、图书废弃 -- 完成
  85. update books set books=books-1,extant=extant-1 where number='202';
  86. -- 8、图书下架 -- 完成
  87. delete from books where number='202';

3.创建项目

在IEDA中新建一个空项目 library

创建软件包 Module

创建三个类, JavaOK、JavaTest、JDBCUtils 。JavaOK=>运行语句 , JavaTest=> 存放方法 , JOBCUtils =>存放方法

4.导入mysql包(必要)

下载包:mysql网站:MySQL :: Download Connector/J

下拉选择Platform Independent

下载mysql-connector-j-8.0.32.tar.gz

点击Download下载

点击No thanks, just start my download.//不登陆下载

解压后就可以看到mysql-connector-j-8.0.32.jar,只有3m大小

或者可以在阿里云盘下载

Java连接数据库实现图书馆...系统(详细教程)https://www.aliyundrive.com/s/UTz8pNxobGK


在IEDA中导入刚刚下载的包:

文件-项目结构-模块-点击加号-1.JAR或目录-上传刚刚下载的jar包

导包工作完成,接下来可以正式编写代码了

5.测试连接数据库

先来一个简单的测试,看是否能成功连接

  1. package com.moyida.linkage;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. public class test {
  5. public static void main(String[] args) {
  6. Connection con;
  7. //
  8. String url = "jdbc:mysql://localhost:3306/library";
  9. String user = "root";
  10. String password = "root";
  11. try {
  12. //连接数据库,获得连接对象
  13. con = DriverManager.getConnection(url, user, password);
  14. if (!con.isClosed())
  15. System.out.println("成功连接数据库");
  16. } catch (Exception e) {
  17. e.printStackTrace();
  18. System.out.println("连接失败");
  19. }
  20. }
  21. }

运行栏显示”成功连接数据库“则表示连接成功了

如果显示”连接失败“,则没有连接成功,检查上述的步骤是否遗漏

测试完后可以将代码删除

6.JOBCUtili 页面

里边存入释放资源的方法

  1. //释放资源
  2. public static void close(Connection connection, Statement statement, ResultSet resultSet) {
  3. try {
  4. if (resultSet != null) {
  5. resultSet.close();
  6. resultSet = null;
  7. }
  8. if (statement != null) {
  9. statement.close();
  10. statement = null;
  11. }
  12. if (connection != null) {
  13. connection.close();
  14. connection = null;
  15. }
  16. } catch (SQLException e) {
  17. e.printStackTrace();
  18. }
  19. }

7.JavaTest 页面

这里先写好数据库连接方法,修改的时候数据库的时候只要修改一处就好了

ip地址修改成自己的

  1. static final String DB_URL = "jdbc:mysql://localhost:3306/library";
  2. static final String USER = "root"; //账号
  3. static final String PASS = "root"; //密码

这里为了省事儿,我把JavaTest页面的所有代码都放到这里

把IP地址和账号密码给成自己的就可以用了,根据方法可以看出连接数据库和执行sql语句的原理,用上面的main方法可以测试这些方法的可行性。

  1. package com.moyida.linkage;
  2. import java.lang.constant.Constable;
  3. import java.sql.*;
  4. public class JavaTest {
  5. public static void main(String[] args) {
  6. //查询用户信息
  7. // query_data("20215154515454", "456");
  8. //查询登录信息
  9. // System.out.println(UserType("20215154515454", "456"));
  10. //插入书籍到图书信息表
  11. // System.out.println("书名,类别,作者,出版社,出版日期,数量");
  12. // AddBooks("高原", "1", "小儿", "中华出版社", "2022-1-9", "5");
  13. //添加类别
  14. // AddCategories(5, "神话类");
  15. //3、添加读者信息
  16. // AddReaders("20544545446546", "劳务", "女", "2", "1112222");
  17. //4、读者借书
  18. // Call Borrowbooks('20215101020051','203','3');
  19. // BorrowBooks("20215101020051","203","3");
  20. //5、读者还书
  21. // AlsoBook("20215101020051","203","2");
  22. //-- 7、图书废弃
  23. //update books set books=books-1,extant=extant-1 where number='202';
  24. // BooksAbandoned("203");
  25. //-- 8、图书下架
  26. //delete from books where number='202';
  27. // Undercarriage("203");
  28. //查询图书信息
  29. // System.out.println(BookInformation("205"));
  30. // AllBookInformation();
  31. //欠费查询
  32. // System.out.println(ArrearageTable("20215101020051", "203"));
  33. //删除罚款账单
  34. // repayment("20215154515454","210");
  35. //查询读者信息表有无该账号
  36. // System.out.println(ReaderTable("2021510102005"));
  37. // 打印读者信息
  38. // PrintReader("20215101024620");
  39. }
  40. static final String DB_URL = "jdbc:mysql://localhost:3306/library";
  41. static final String USER = "root";
  42. static final String PASS = "root";
  43. /**
  44. * 图书下架
  45. *
  46. * @param number
  47. * @return
  48. */
  49. public static boolean Undercarriage(String number) {
  50. Connection conn = null;
  51. Statement stmt = null;
  52. try {
  53. //连接数据库,获得连接对象
  54. conn = DriverManager.getConnection(DB_URL, USER, PASS);
  55. //创建执行环境
  56. stmt = conn.createStatement();
  57. //执行sql语句,得到结果集 resultSet
  58. String sql = "delete from books where number='" + number + "'";
  59. System.out.println(sql);
  60. int resultSet = stmt.executeUpdate(sql);
  61. } catch (Exception e) {
  62. e.printStackTrace();
  63. }//释放资源
  64. finally {
  65. JDBCUtils.close(conn, stmt, null);
  66. }
  67. return false;
  68. }
  69. /**
  70. * 图书废弃
  71. *
  72. * @param number
  73. * @return
  74. */
  75. public static String BooksAbandoned(String number) {
  76. Connection connection = null;
  77. Statement statement = null;
  78. try {
  79. //连接数据库,获得连接对象
  80. connection = DriverManager.getConnection(DB_URL, USER, PASS);
  81. //创建执行环境
  82. statement = connection.createStatement();
  83. //执行sql语句,得到结果集 resultSet
  84. String sql = "update books set books=books-1,extant=extant-1 where number='" + number + "'";
  85. int resultSet = statement.executeUpdate(sql);
  86. } catch (Exception e) {
  87. e.printStackTrace();
  88. }//释放资源
  89. finally {
  90. JDBCUtils.close(connection, statement, null);
  91. }
  92. return number;
  93. }
  94. /**
  95. * 读者还书
  96. *
  97. * @param rnumber 读者编号
  98. * @param bnumber 图书编号
  99. * @param quantity 数量
  100. */
  101. static void AlsoBook(String rnumber, String bnumber, String quantity) {
  102. Connection connection = null;
  103. Statement statement = null;
  104. try {
  105. //连接数据库,获得连接对象
  106. connection = DriverManager.getConnection(DB_URL, USER, PASS);
  107. //创建执行环境
  108. statement = connection.createStatement();
  109. //执行sql语句,得到结果集 resultSet
  110. String sql = "Call Bookreturn(" + "'" + rnumber + "'" + "," + "'" + bnumber + "'" + "," + "'" + quantity + "'" + ")";
  111. System.out.println(sql);
  112. int resultSet = statement.executeUpdate(sql);
  113. } catch (Exception e) {
  114. e.printStackTrace();
  115. }//释放资源
  116. finally {
  117. JDBCUtils.close(connection, statement, null);
  118. }
  119. }
  120. /**
  121. * 欠费罚款查询
  122. *
  123. * @param rnumber
  124. * @param bnumber
  125. * @return
  126. */
  127. public static String ArrearageTable(String rnumber, String bnumber) {
  128. Connection connection = null;
  129. Statement statement = null;
  130. try {
  131. //连接数据库,获得连接对象
  132. connection = DriverManager.getConnection(DB_URL, USER, PASS);
  133. //创建执行环境
  134. statement = connection.createStatement();
  135. //执行sql语句,得到结果集 resultSet
  136. String sql = "SELECT sum(money) as arrearage FROM amerce WHERE rnumber =" + rnumber + " and bnumber= " + bnumber + " ORDER BY quantity";
  137. ResultSet resultSet = statement.executeQuery(sql);
  138. while (resultSet.next()) {
  139. String a = resultSet.getString("arrearage");
  140. return a;
  141. }
  142. // 完成后关闭
  143. resultSet.close();
  144. statement.close();
  145. connection.close();
  146. } catch (SQLException se) {
  147. // 处理 JDBC 错误
  148. se.printStackTrace();
  149. } catch (Exception e) {
  150. // 处理 Class.forName 错误
  151. e.printStackTrace();
  152. } finally {
  153. // 关闭资源
  154. try {
  155. if (statement != null) statement.close();
  156. } catch (SQLException se2) {
  157. }// 什么都不做
  158. try {
  159. if (connection != null) connection.close();
  160. } catch (SQLException se) {
  161. se.printStackTrace();
  162. }
  163. }
  164. return null;
  165. }
  166. /**
  167. * 通过学号打印借书信息表
  168. *
  169. * @param rnumber 读者编号
  170. */
  171. public static void NumberBookTable(String rnumber) {
  172. Connection conn = null;
  173. Statement stmt = null;
  174. try {
  175. //连接数据库,获得连接对象
  176. conn = DriverManager.getConnection(DB_URL, USER, PASS);
  177. //创建执行环境
  178. stmt = conn.createStatement();
  179. String sql = "SELECT * FROM borrow WHERE rnumber =" + rnumber;
  180. ResultSet rs = stmt.executeQuery(sql);
  181. while (rs.next()) {
  182. System.out.print(rs.getString("rnumber"));
  183. System.out.print("\t ,");
  184. System.out.print(rs.getString("bnumber"));
  185. System.out.print("\t ,");
  186. System.out.print(rs.getString("quantity"));
  187. System.out.print("\t ,");
  188. System.out.println(rs.getString("btime"));
  189. }
  190. // 完成后关闭
  191. rs.close();
  192. stmt.close();
  193. conn.close();
  194. } catch (SQLException se) {
  195. // 处理 JDBC 错误
  196. se.printStackTrace();
  197. } catch (Exception e) {
  198. // 处理 Class.forName 错误
  199. e.printStackTrace();
  200. } finally {
  201. // 关闭资源
  202. try {
  203. if (stmt != null) stmt.close();
  204. } catch (SQLException se2) {
  205. }// 什么都不做
  206. try {
  207. if (conn != null) conn.close();
  208. } catch (SQLException se) {
  209. se.printStackTrace();
  210. }
  211. }
  212. }
  213. /**
  214. * 打印借书信息表
  215. *
  216. * @param rnumber 读者编号
  217. * @param bnumber 书籍编号
  218. */
  219. public static void BookTable(String rnumber, String bnumber) {
  220. Connection conn = null;
  221. Statement stmt = null;
  222. try {
  223. //连接数据库,获得连接对象
  224. conn = DriverManager.getConnection(DB_URL, USER, PASS);
  225. //创建执行环境
  226. stmt = conn.createStatement();
  227. String sql = "SELECT * FROM borrow WHERE rnumber =" + rnumber + " and bnumber= " + bnumber;
  228. ResultSet rs = stmt.executeQuery(sql);
  229. while (rs.next()) {
  230. System.out.print(rs.getString("rnumber"));
  231. System.out.print("\t ,");
  232. System.out.print(rs.getString("bnumber"));
  233. System.out.print("\t ,");
  234. System.out.print(rs.getString("quantity"));
  235. System.out.print("\t ,");
  236. System.out.println(rs.getString("btime"));
  237. }
  238. // 完成后关闭
  239. rs.close();
  240. stmt.close();
  241. conn.close();
  242. } catch (SQLException se) {
  243. // 处理 JDBC 错误
  244. se.printStackTrace();
  245. } catch (Exception e) {
  246. // 处理 Class.forName 错误
  247. e.printStackTrace();
  248. } finally {
  249. // 关闭资源
  250. try {
  251. if (stmt != null) stmt.close();
  252. } catch (SQLException se2) {
  253. }// 什么都不做
  254. try {
  255. if (conn != null) conn.close();
  256. } catch (SQLException se) {
  257. se.printStackTrace();
  258. }
  259. }
  260. }
  261. /**
  262. * 删除罚款账单
  263. *
  264. * @param rnumber
  265. * @param bnumber
  266. */
  267. //delete amerce WHERE rnumber='20215154515454' and bnumber='210'
  268. public static void repayment(String rnumber, String bnumber) {
  269. Connection connection = null;
  270. Statement statement = null;
  271. try {
  272. //连接数据库,获得连接对象
  273. connection = DriverManager.getConnection(DB_URL, USER, PASS);
  274. //创建执行环境
  275. statement = connection.createStatement();
  276. //执行sql语句,得到结果集 resultSet
  277. String sql = "delete from amerce WHERE rnumber=" + rnumber + " and bnumber=" + bnumber;
  278. int resultSet = statement.executeUpdate(sql);
  279. } catch (Exception e) {
  280. e.printStackTrace();
  281. }//释放资源
  282. finally {
  283. JDBCUtils.close(connection, statement, null);
  284. }
  285. }
  286. /**
  287. * 读者借书
  288. *
  289. * @param rnumber 读者编号
  290. */
  291. public static void BorrowBooks(String rnumber, String bnumber, String quantity) {
  292. Connection connection = null;
  293. Statement statement = null;
  294. try {
  295. //连接数据库,获得连接对象
  296. connection = DriverManager.getConnection(DB_URL, USER, PASS);
  297. //创建执行环境
  298. statement = connection.createStatement();
  299. //执行sql语句,得到结果集 resultSet
  300. String sql = "Call Borrowbooks(" + "'" + rnumber + "'" + "," + "'" + bnumber + "'" + "," + "'" + quantity + "'" + ")";
  301. System.out.println(sql);
  302. int resultSet = statement.executeUpdate(sql);
  303. } catch (Exception e) {
  304. e.printStackTrace();
  305. }//释放资源
  306. finally {
  307. JDBCUtils.close(connection, statement, null);
  308. }
  309. }
  310. /**
  311. * 添加读者信息(注册)
  312. *
  313. * @param number
  314. * @param name
  315. * @param sex
  316. * @param kind
  317. * @param password
  318. */
  319. public static void AddReaders(String number, String name, String sex, String kind, String password) {
  320. Connection connection = null;
  321. Statement statement = null;
  322. try {
  323. //连接数据库,获得连接对象
  324. connection = DriverManager.getConnection(DB_URL, USER, PASS);
  325. //创建执行环境
  326. statement = connection.createStatement();
  327. //执行sql语句,得到结果集 resultSet
  328. // // INSERT INTO reader(number,name,sex,kind,rdate) VALUES('20215101020051','翠花','女','学生',now(),password);
  329. String sql = "INSERT INTO reader VALUES(" + "'" + number + "'" + "," + "'" + name + "'" + "," + "'" + sex + "'" + "," + "'" + kind + "'" + "," + "now()" + "," + "'" + password + "'" + ")";
  330. int resultSet = statement.executeUpdate(sql);
  331. } catch (Exception e) {
  332. e.printStackTrace();
  333. }//释放资源
  334. finally {
  335. JDBCUtils.close(connection, statement, null);
  336. }
  337. }
  338. /**
  339. * 2、添加图书类别信息
  340. *
  341. * @param id
  342. * @param name
  343. * @return
  344. */
  345. public static String AddCategories(int id, String name) {
  346. Connection connection = null;
  347. Statement statement = null;
  348. ResultSet resultSet = null;
  349. try {
  350. //连接数据库,获得连接对象
  351. connection = DriverManager.getConnection(DB_URL, USER, PASS);
  352. //创建执行环境
  353. statement = connection.createStatement();
  354. //执行sql语句,得到结果集 resultSet
  355. String sql = "INSERT INTO category() VALUES(" + id + "," + "'" + name + "'" + ")";
  356. resultSet = statement.executeQuery(sql);
  357. // int resultSet = statement.executeUpdate(sql);
  358. // 完成后关闭
  359. resultSet.close();
  360. statement.close();
  361. connection.close();
  362. } catch (Exception e) {
  363. e.printStackTrace();
  364. }//释放资源
  365. finally {
  366. JDBCUtils.close(connection, statement, resultSet);
  367. }
  368. return name;
  369. }
  370. /**
  371. * 添加书籍到图书信息表
  372. *
  373. * @param name 图书名称
  374. * @param category 图书类别
  375. * @param author 作者
  376. * @param press 出版社
  377. * @param pdate 出版日期
  378. * @param quantity 要存的数量
  379. * @return
  380. */
  381. public static String AddBooks(String name, String category, String author, String press, String pdate, String quantity) {
  382. Connection connection = null;
  383. ResultSet resultSet = null;
  384. Statement statement = null;
  385. try {
  386. //连接数据库,获得连接对象
  387. connection = DriverManager.getConnection(DB_URL, USER, PASS);
  388. //创建执行环境
  389. statement = connection.createStatement();
  390. //执行sql语句,得到结果集 resultSet
  391. String sql = "call Insertbooks(" + "'" + name + "'" + "," + "'" + category + "'" + "," + "'" + author + "'" + "," + "'" + press + "'" + "," + "'" + pdate + "'" + "," + "'" + quantity + "'" + ")";
  392. resultSet = statement.executeQuery(sql);
  393. // 完成后关闭
  394. resultSet.close();
  395. statement.close();
  396. connection.close();
  397. } catch (Exception e) {
  398. //错误处理
  399. e.printStackTrace();
  400. }//释放资源
  401. finally {
  402. JDBCUtils.close(connection, statement, resultSet);
  403. }
  404. return name;
  405. }
  406. /**
  407. * 查询所有图书信息
  408. *
  409. * @return 返回值
  410. */
  411. public static void AllBookInformation() {
  412. Connection conn = null;
  413. Statement stmt = null;
  414. try {
  415. //连接数据库,获得连接对象
  416. conn = DriverManager.getConnection(DB_URL, USER, PASS);
  417. //创建执行环境
  418. stmt = conn.createStatement();
  419. String sql = "SELECT * FROM books";
  420. ResultSet rs = stmt.executeQuery(sql);
  421. System.out.println("图书编号 图书名称 图书类别 作者 出版社 出版日期 入库日期 库存总数量 现存数量");
  422. while (rs.next()) {
  423. System.out.print(rs.getString("number") + "\t");
  424. System.out.print(rs.getString("name") + "\t");
  425. System.out.print(rs.getString("category") + "\t");
  426. System.out.print(rs.getString("author") + "\t");
  427. System.out.print(rs.getString("press") + "\t");
  428. System.out.print(rs.getString("pdate") + "\t");
  429. System.out.print(rs.getString("wdate") + "\t");
  430. System.out.print(rs.getString("books") + "\t");
  431. System.out.println(rs.getString("extant"));
  432. }
  433. // 完成后关闭
  434. rs.close();
  435. stmt.close();
  436. conn.close();
  437. } catch (SQLException se) {
  438. // 处理 JDBC 错误
  439. se.printStackTrace();
  440. } catch (Exception e) {
  441. // 处理 Class.forName 错误
  442. e.printStackTrace();
  443. } finally {
  444. // 关闭资源
  445. try {
  446. if (stmt != null) stmt.close();
  447. } catch (SQLException se2) {
  448. }// 什么都不做
  449. try {
  450. if (conn != null) conn.close();
  451. } catch (SQLException se) {
  452. se.printStackTrace();
  453. }
  454. }
  455. }
  456. /**
  457. * 打印读者信息
  458. */
  459. public static void PrintReader(String number){
  460. Connection conn = null;
  461. Statement stmt = null;
  462. try {
  463. //连接数据库,获得连接对象
  464. conn = DriverManager.getConnection(DB_URL, USER, PASS);
  465. //创建执行环境
  466. stmt = conn.createStatement();
  467. String sql = "SELECT * FROM reader WHERE number =" + number;
  468. ResultSet rs = stmt.executeQuery(sql);
  469. while (rs.next()) {
  470. System.out.print(rs.getString("number")+"\t");
  471. System.out.print(rs.getString("name")+"\t");
  472. System.out.print(rs.getString("sex")+"\t");
  473. System.out.print(rs.getString("kind")+"\t");
  474. System.out.print(rs.getString("rdate")+"\t");
  475. System.out.println(rs.getString("password"));
  476. }
  477. // 完成后关闭
  478. rs.close();
  479. stmt.close();
  480. conn.close();
  481. } catch (SQLException se) {
  482. // 处理 JDBC 错误
  483. se.printStackTrace();
  484. } catch (Exception e) {
  485. // 处理 Class.forName 错误
  486. e.printStackTrace();
  487. } finally {
  488. // 关闭资源
  489. try {
  490. if (stmt != null) stmt.close();
  491. } catch (SQLException se2) {
  492. }// 什么都不做
  493. try {
  494. if (conn != null) conn.close();
  495. } catch (SQLException se) {
  496. se.printStackTrace();
  497. }
  498. }
  499. }
  500. /**
  501. * 查询读者信息表有无该账号
  502. *
  503. * @return
  504. */
  505. public static Constable ReaderTable(String number) {
  506. Connection conn = null;
  507. Statement stmt = null;
  508. try {
  509. //连接数据库,获得连接对象
  510. conn = DriverManager.getConnection(DB_URL, USER, PASS);
  511. //创建执行环境
  512. stmt = conn.createStatement();
  513. String sql = "select number from reader where number=" + number;
  514. ResultSet rs = stmt.executeQuery(sql);
  515. if (rs.next()) {
  516. return false;
  517. }
  518. // 完成后关闭
  519. rs.close();
  520. stmt.close();
  521. conn.close();
  522. } catch (SQLException se) {
  523. // 处理 JDBC 错误
  524. se.printStackTrace();
  525. } catch (Exception e) {
  526. // 处理 Class.forName 错误
  527. e.printStackTrace();
  528. } finally {
  529. // 关闭资源
  530. try {
  531. if (stmt != null) stmt.close();
  532. } catch (SQLException se2) {
  533. }// 什么都不做
  534. try {
  535. if (conn != null) conn.close();
  536. } catch (SQLException se) {
  537. se.printStackTrace();
  538. }
  539. }
  540. return null;
  541. }
  542. /**
  543. * 查询图书信息
  544. *
  545. * @param number 输入的密码
  546. * @return 返回值
  547. */
  548. public static String BookInformation(String number) {
  549. Connection conn = null;
  550. Statement stmt = null;
  551. try {
  552. //连接数据库,获得连接对象
  553. conn = DriverManager.getConnection(DB_URL, USER, PASS);
  554. //创建执行环境
  555. stmt = conn.createStatement();
  556. String sql = "SELECT * FROM books WHERE number =" + number;
  557. ResultSet rs = stmt.executeQuery(sql);
  558. while (rs.next()) {
  559. String a = rs.getString("number");
  560. String a1 = ("\t");
  561. String b = rs.getString("name");
  562. String b1 = ("\t");
  563. String c = rs.getString("category");
  564. String c1 = ("\t");
  565. String d = rs.getString("author");
  566. String d1 = ("\t");
  567. String e = rs.getString("press");
  568. String e1 = ("\t");
  569. String f = rs.getString("pdate");
  570. String f1 = ("\t");
  571. String g = rs.getString("wdate");
  572. String g1 = ("\t");
  573. String h = rs.getString("books");
  574. String h1 = ("\t");
  575. String i = rs.getString("extant");
  576. return a + a1 + b + b1 + c + c1 + d + d1 + e + e1 + f + f1 + g + g1 + h + h1 + i;
  577. }
  578. // 完成后关闭
  579. rs.close();
  580. stmt.close();
  581. conn.close();
  582. } catch (SQLException se) {
  583. // 处理 JDBC 错误
  584. se.printStackTrace();
  585. } catch (Exception e) {
  586. // 处理 Class.forName 错误
  587. e.printStackTrace();
  588. } finally {
  589. // 关闭资源
  590. try {
  591. if (stmt != null) stmt.close();
  592. } catch (SQLException se2) {
  593. }// 什么都不做
  594. try {
  595. if (conn != null) conn.close();
  596. } catch (SQLException se) {
  597. se.printStackTrace();
  598. }
  599. }
  600. return null;
  601. }
  602. /**
  603. * 查询学生表信息
  604. *
  605. * @param number 输入的账号
  606. * @param password 输入的密码
  607. * @return 返回值
  608. */
  609. public static void query_data(String number, String password) {
  610. Connection conn = null;
  611. Statement stmt = null;
  612. try {
  613. //连接数据库,获得连接对象
  614. conn = DriverManager.getConnection(DB_URL, USER, PASS);
  615. //创建执行环境
  616. stmt = conn.createStatement();
  617. String sql = "select * from reader where number= " + "'" + number + "'" + " and password=" + "'" + password + "'";
  618. ResultSet rs = stmt.executeQuery(sql);
  619. while (rs.next()) {
  620. String a = rs.getString("number");
  621. String a1 = ("\t");
  622. String b = rs.getString("name");
  623. String b1 = ("\t");
  624. String c = rs.getString("sex");
  625. String c1 = ("\t");
  626. String d = rs.getString("kind");
  627. String d1 = ("\t");
  628. String e = rs.getString("rdate");
  629. String e1 = ("\t");
  630. String f = rs.getString("password");
  631. System.out.println(a + a1 + b + b1 + c + c1 + d + e + d1 + f);
  632. }
  633. // 完成后关闭
  634. rs.close();
  635. stmt.close();
  636. conn.close();
  637. } catch (SQLException se) {
  638. // 处理 JDBC 错误
  639. se.printStackTrace();
  640. } catch (Exception e) {
  641. // 处理 Class.forName 错误
  642. e.printStackTrace();
  643. } finally {
  644. // 关闭资源
  645. try {
  646. if (stmt != null) stmt.close();
  647. } catch (SQLException se2) {
  648. }// 什么都不做
  649. try {
  650. if (conn != null) conn.close();
  651. } catch (SQLException se) {
  652. se.printStackTrace();
  653. }
  654. }
  655. }
  656. /**
  657. * 判断用户类型,登录账号,返回1表示老师,返回2表示学生,返回null表示查无此人
  658. *
  659. * @param number 输入的学号
  660. * @return 返回值
  661. */
  662. public static String UserType(String number, String password) {
  663. Connection connection = null;
  664. Statement statement = null;
  665. try {
  666. //连接数据库,获得连接对象
  667. connection = DriverManager.getConnection(DB_URL, USER, PASS);
  668. //创建执行环境
  669. statement = connection.createStatement();
  670. //执行sql语句,得到结果集 resultSet
  671. String sql = "select kind from reader where number= " + "'" + number + "'" + " and password=" + "'" + password + "'";
  672. ResultSet resultSet = statement.executeQuery(sql);
  673. while (resultSet.next()) {
  674. String a = resultSet.getString("kind");
  675. return a;
  676. }
  677. // 完成后关闭
  678. resultSet.close();
  679. statement.close();
  680. connection.close();
  681. } catch (SQLException se) {
  682. // 处理 JDBC 错误
  683. se.printStackTrace();
  684. } catch (Exception e) {
  685. // 处理 Class.forName 错误
  686. e.printStackTrace();
  687. } finally {
  688. // 关闭资源
  689. try {
  690. if (statement != null) statement.close();
  691. } catch (SQLException se2) {
  692. }// 什么都不做
  693. try {
  694. if (connection != null) connection.close();
  695. } catch (SQLException se) {
  696. se.printStackTrace();
  697. }
  698. }
  699. return null;//这样可以使得调用方无需检查结果是否为null
  700. }
  701. }

8.JavaOK 页面

JavaOK是运行调用JavaTest和JOBCUtili方法的主页面,在这个类运行可以实现系统的管理,这个系统仍有不少的bug没修复,但是可以正常跑起来了,可以根据自己的理解,把代码经行完善和修复

下面是JavaOK页面的代码

  1. package com.moyida.linkage;
  2. import java.lang.constant.Constable;
  3. import java.util.Scanner;
  4. public class JavaOk {
  5. public static void main(String[] args) {
  6. Scanner sc = new Scanner(System.in);
  7. while (true) {
  8. System.out.println("=============首页==============");
  9. System.out.println("1、登录");
  10. System.out.println("2、注册");
  11. int reg = sc.nextInt();
  12. switch (reg) {
  13. case 1:
  14. //登录
  15. while (true) {
  16. System.out.println("=============登陆页面==============");
  17. System.out.println("请您输入账号");
  18. String acc = sc.next();
  19. System.out.println("请您输入密码");
  20. String pass = sc.next();
  21. String sum = JavaTest.UserType(acc, pass);
  22. if (sum == null) {
  23. System.out.println("对不起,查无此人,请重新输入");
  24. break;
  25. }
  26. // int a = Integer.parseInt(sum);
  27. // String a=sum;
  28. //判断用户类型,登录账号,返回1表示老师,返回2表示学生,返回null表示查无此人
  29. System.out.println("返回的数据是" + sum);
  30. switch (sum) {
  31. case "1":
  32. System.out.println("欢迎管理员登录");
  33. conservator(sc);
  34. break;
  35. case "2":
  36. System.out.println("欢迎学生登录");
  37. operate(sc, acc);
  38. break;
  39. default:
  40. System.out.println("对不起,查无此人");
  41. break;
  42. }
  43. break;
  44. }
  45. case 2:
  46. //注册
  47. Register(sc);
  48. break;
  49. default:
  50. System.out.println("您输入的命令错误,请重新输入!");
  51. break;
  52. }
  53. }
  54. }
  55. /**
  56. * 注册
  57. *
  58. * @param sc
  59. */
  60. private static void Register(Scanner sc) {
  61. System.out.println("=============注册页面==============");
  62. System.out.println("请输入要注册的账号(学号)");
  63. String number = sc.next();
  64. //查询数据库中有无该账号
  65. Constable aaa = JavaTest.ReaderTable(number);
  66. if (aaa == null) {
  67. //没有账号,可以进行
  68. System.out.println("请输入您的名字");
  69. String name = sc.next();
  70. System.out.println("请输入您的性别");
  71. String sex = sc.next();
  72. System.out.println("请输入您的类型 学生 2/管理员 1");
  73. int kind = sc.nextInt();
  74. System.out.println("请输入您的登录密码:");
  75. String pass = sc.next();
  76. //调用注册方法
  77. JavaTest.AddReaders(number, name, sex, String.valueOf(kind), pass);
  78. System.out.println("注册成功!");
  79. //打印注册信息
  80. System.out.println("您的注册信息如下:");
  81. JavaTest.PrintReader(number);
  82. }else {
  83. System.out.println("该账号已注册!");
  84. }
  85. }
  86. /**
  87. * 管理员操作页面
  88. *
  89. * @param sc
  90. */
  91. private static void conservator(Scanner sc) {
  92. while (true) {
  93. System.out.println("=============操作页面==============");
  94. System.out.println("0、退出");
  95. System.out.println("1、添加图书表书籍");
  96. System.out.println("2、添加图书类别信息");
  97. System.out.println("3、图书废弃");
  98. System.out.println("4、图书下架");
  99. System.out.println("5、查询全部图书信息");
  100. int i = sc.nextInt();
  101. switch (i) {
  102. case 0:
  103. System.out.println("已退出账号,欢迎再次登录!");
  104. return;
  105. case 1:
  106. System.out.println("=============添加书籍页面==============");
  107. System.out.println("图书名称");
  108. String name = sc.next();
  109. System.out.println("图书类别");
  110. String category = sc.next();
  111. System.out.println("作者");
  112. String author = sc.next();
  113. System.out.println("出版社");
  114. String press = sc.next();
  115. System.out.println("出版日期");
  116. String pdate = sc.next();
  117. System.out.println("要存的数量");
  118. String quantity = sc.next();
  119. String charu = JavaTest.AddBooks(name, category, author, press, pdate, quantity);
  120. System.out.println("添加成功");
  121. break;
  122. case 2:
  123. System.out.println("=============添加图书类别页面==============");
  124. System.out.println("输入类别编号");
  125. int leibie = sc.nextInt();
  126. System.out.println("输入类别名称");
  127. String name1 = sc.next();
  128. JavaTest.AddCategories(leibie, name1);
  129. System.out.println("添加成功");
  130. break;
  131. case 3:
  132. System.out.println("=============图书废弃页面==============");
  133. System.out.println("请输入要废弃图书的图书编号");
  134. String reduce = sc.next();
  135. System.out.println("您要废除的书籍是:");
  136. System.out.println(JavaTest.BookInformation(reduce));
  137. System.out.println("是否确认废除y/n");
  138. String rs = sc.next();
  139. switch (rs) {
  140. case "y":
  141. //可以废除
  142. String ff = JavaTest.BooksAbandoned(reduce);
  143. System.out.println(JavaTest.BookInformation(reduce));
  144. System.out.println("废弃成功,该图书减一");
  145. break;
  146. default:
  147. System.out.println("您取消废除,当前书籍继续保留~");
  148. }
  149. break;
  150. case 4:
  151. System.out.println("=============图书下架页面==============");
  152. System.out.println("请输入要下架图书的图书编号");
  153. String reduce1 = sc.next();
  154. System.out.println("您要下架的书籍是:");
  155. System.out.println(JavaTest.BookInformation(reduce1));
  156. System.out.println("是否确认把该图书下架y/n");
  157. String rs1 = sc.next();
  158. switch (rs1) {
  159. case "y":
  160. //可以废除
  161. String ff = JavaTest.BooksAbandoned(reduce1);
  162. System.out.println(JavaTest.Undercarriage(reduce1));
  163. System.out.println("下架成功,图书已从图书系统中去除");
  164. break;
  165. default:
  166. System.out.println("您取消废除,当前书籍继续保留~");
  167. }
  168. break;
  169. case 5:
  170. //查看所有书籍
  171. JavaTest.AllBookInformation();
  172. break;
  173. default:
  174. System.out.println("输入错误!");
  175. break;
  176. }
  177. }
  178. }
  179. /**
  180. * 学生操作页面
  181. *
  182. * @param sc
  183. */
  184. private static void operate(Scanner sc, String acc) {
  185. while (true) {
  186. System.out.println("=============学生操作页面==============");
  187. System.out.println("0、退出");
  188. System.out.println("1、查看所有书籍");
  189. System.out.println("2、借书");
  190. System.out.println("3、还书");
  191. String student = sc.next();
  192. switch (student) {
  193. case "0":
  194. System.out.println("已退出账号,欢迎再次登录!");
  195. return;
  196. case "1":
  197. //查看所有书籍
  198. JavaTest.AllBookInformation();
  199. break;
  200. case "2":
  201. //借书
  202. // public static boolean BorrowBooks(String rnumber, String bnumber, String quantity) {
  203. System.out.println("请输入要借的书籍编号");
  204. String Borrow = sc.next();
  205. System.out.println("您要借的书籍是:");
  206. System.out.println(JavaTest.BookInformation(Borrow));
  207. System.out.println("是否借该图书?y/n");
  208. String rs1 = sc.next();
  209. switch (rs1) {
  210. case "y":
  211. //可以借书
  212. System.out.println("您要借几本书?");
  213. String books = sc.next();
  214. JavaTest.BorrowBooks(acc, Borrow, books);
  215. System.out.println("借书完成,您的借书信息如下");
  216. //打印借书信息
  217. JavaTest.BookTable(acc, Borrow);
  218. System.out.println("");
  219. break;
  220. default:
  221. System.out.println("您取消借书操作,欢迎再次使用!");
  222. }
  223. break;
  224. case "3":
  225. //还书
  226. System.out.println("您可以还的书籍如下");
  227. JavaTest.NumberBookTable(acc);
  228. System.out.println("");
  229. System.out.println("请输入要还的书籍编号");
  230. String AlsoBook = sc.next();
  231. System.out.println("您要还的书籍是:");
  232. //打印借书信息
  233. JavaTest.BookTable(acc, AlsoBook);
  234. //再次确定归还
  235. System.out.println("是否还该图书?y/n");
  236. String rs2 = sc.next();
  237. switch (rs2) {
  238. case "y":
  239. //可以还书
  240. System.out.println("您要还此编号的几本书?");
  241. String books = sc.next();
  242. // 还书
  243. JavaTest.AlsoBook(acc, AlsoBook, books);
  244. //查询欠费账单有没有欠费
  245. String bill = JavaTest.ArrearageTable(acc, AlsoBook);
  246. if (bill == null) {
  247. //可以了
  248. System.out.println("还书完成,您还有此类书籍尚为归还");
  249. //打印借书信息
  250. JavaTest.BookTable(acc, AlsoBook);
  251. break;
  252. } else {
  253. //钱没还
  254. while (true) {
  255. System.out.println("您欠费的金额是" + bill);
  256. System.out.println("如已支付,请输入内容:支付完成");
  257. String ff = sc.next();
  258. String dd = "支付完成";
  259. if (ff.equals(dd)) {
  260. //删除欠费账单
  261. JavaTest.repayment(acc, AlsoBook);
  262. //打印借书信息
  263. System.out.println("还书完成,您还有此类书籍尚为归还");
  264. //打印借书信息
  265. JavaTest.BookTable(acc, AlsoBook);
  266. break;
  267. } else {
  268. System.out.println("支付失败,请完成支付");
  269. }
  270. }
  271. }
  272. }
  273. }
  274. }
  275. }
  276. }

9.运行

在IEDA的JavaOK页面,右键点击运行,就可以使用了。

10.资料

我在阿里云盘中存有该系统程序的源代码,可提供下载学习使用。

阿里网盘 链接

Java连接数据库实现图书馆...系统(详细教程)https://www.aliyundrive.com/s/UTz8pNxobGK

原文链接:https://blog.csdn.net/m0_74608954/article/details/128814365



所属网站分类: 技术文章 > 博客

作者:我爱java

链接:http://www.javaheidong.com/blog/article/674534/2fe166474cc95b8ac196/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

5 0
收藏该文
已收藏

评论内容:(最多支持255个字符)