1.课设要求描述
●实现图书信息、类别、出版社等信息的管理;
●实现读者信息、借阅证信息的管理;
●实现图书的借阅、续借、归还管理;
●实现超期罚款管理、收款管理;
●创建触发器,分别实现借书和还书时自动更新图书信息的在册数量;
●创建视图查询各种图书的书号、书名、总数和在册数; .
●创建存储过程查询指定读者借阅图书的情况;
●建立数据库相关表之间的参照完整性约束。
2.制作思路及基础讲解
此项目主要是用于完成大二下半学期的数据库课设,随手记录。技术面大二下之前只学过Java,MySQL/SQLServer。制作思路:首先是数据库作为存储数据的地方,编写根据课设要求编写好对应的建表语句和存储过程,视图,触发器等。然后编写好对应系统的Java代码,最后通过JDBC将Java代码与数据库连接起来。
部分学生可能学的比较浅,不理解mysql,sqlserver,java,c等一堆名词之间的含义。之前有同学找我做过这类似的课设。问的问题都比较抽象,例如用mysql做一个系统之类的。在此简单说一下我对于这些的理解,mysql,sqlserver都是数据库的一种,只不过是外国的所以都是用英文表示,大部分上课不听的同学看到这一堆英文都会比较懵,其实只需简单把数据库(mysql,SQLServer)理解成存储数据的地方,而java,c等语言是用来编写系统逻辑功能的代码。当然在数据库中的存储过程或者触发器什么的,这些功能也能在代码编程语言中通过特定的逻辑实现类似的功能,只不过我们在数据库中写好的话,免去了Java中代码的编写量,并且查询时速度会更快。
3.使用工具及语言详解
不同的学校要求不同,有的是sqlserver,有的是mysql,但其实这两者的区别在我看来区别不大,大部分的语句格式都是一样的,所以我选择了我熟悉的一种,mysql来进行编写。在语言方面,我大一的时候学过c,但是当时没有认真听,后来也慢慢都忘了,大二时学了Java,并且之后又系统性的学了一遍,所以选择来用Java语言编写。对于编译器我使用的是IDEA,版本随意,这个不影响,但需要注意IDEA不是免费的,可以去试用30天,也可以自行网上找方法安装破解版。数据库可视化工具使用的是Navicat,这个工具同样也不是免费的,但提供14天试用期,有长期需求也需要各位自行找破解版。
PS:这里又提到两个新名词,IDEA和Navicat,这不是编程语言,而是工具的名字,大家需要慢慢熟悉这些名词,简单来说就是他能让你的编程之路变得更顺畅,提供了可视化的界面,不需要一直在黑窗口中输入代码。
4.数据库建表代码
/*
Navicat Premium Data Transfer
Source Server : localhost_3306
Source Server Type : MySQL
Source Server Version : 80031
Source Host : localhost:3306
Source Schema : emp
Target Server Type : MySQL
Target Server Version : 80031
File Encoding : 65001
Date: 15/06/2024 08:40:52
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for admindt
-- ----------------------------
DROP TABLE IF EXISTS `admindt`;
CREATE TABLE `admindt` (
`id` bigint NOT NULL,
`username` varchar(30) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
`password` varchar(30) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
`againcode` char(30) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of admindt
-- ----------------------------
INSERT INTO `admindt` VALUES (1, 'admin', 'admin', 'admin');
INSERT INTO `admindt` VALUES (2, '2', '1464179427', '123456');
INSERT INTO `admindt` VALUES (3, '3', '1464179427', '123456');
-- ----------------------------
-- Table structure for books
-- ----------------------------
DROP TABLE IF EXISTS `books`;
CREATE TABLE `books` (
`ID` bigint NOT NULL AUTO_INCREMENT,
`ISBN` varchar(30) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
`TITLE` varchar(30) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
`PUBLISH` varchar(30) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
`STOCK` bigint NOT NULL,
PRIMARY KEY (`ID`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 39 CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of books
-- ----------------------------
INSERT INTO `books` VALUES (1, '978-7-5628-3717-6', '英语四级词汇速成词根', ' 华东理工大学出版社', 2);
INSERT INTO `books` VALUES (2, '345-3-5776-3455-2', '狼图腾', '中国人民出版社', 1);
INSERT INTO `books` VALUES (3, '301-1-5317-3991-2', '左慈传', '复旦大学出版社', 1);
INSERT INTO `books` VALUES (4, '564-2-7120-2311-1', 'C++语言程序设计', '清华大学出版社', 0);
INSERT INTO `books` VALUES (5, '121-5-3412-7120-8', '目送', '广西师范大学出版社', 1);
INSERT INTO `books` VALUES (6, '732-5-3313-6174-2', 'PHP实战开发', '清华大学出版社', 1);
INSERT INTO `books` VALUES (7, '321-5-3443-3121-8', '人才培养方案', '科技大学出版社', 1);
INSERT INTO `books` VALUES (8, '501-4-7211-6130-2', '谁动了我的奶酪', '公共艺术出版社', 1);
INSERT INTO `books` VALUES (9, '757-1-1453-8123-0', '小王子', '中央人民出版社', 0);
INSERT INTO `books` VALUES (10, '129-3-1482-7321-9', '戏说PHP', '则以出版社', 3);
INSERT INTO `books` VALUES (11, '191-7-9419-4123-8', '雷锋的故事', '中国人民出版社', 10);
INSERT INTO `books` VALUES (12, '624-5-1413-1127-8', '钢铁是怎样炼成的', '国际文学出版社', 3);
INSERT INTO `books` VALUES (13, '978-7-80740-931-1', '梁启超传.上', ' 上海文化出版社', 1);
INSERT INTO `books` VALUES (14, '978-7-5618-3724-5', '建筑地带A-Z', ' 天津大学出版社', 1);
INSERT INTO `books` VALUES (15, '978-7-5038-6414-8', '企业学习力研究及案例分析', '中国林业出版社', 1);
INSERT INTO `books` VALUES (16, '978-7-01-012704-0', '中国与全球金融风险报告', '人民出版社', 1);
INSERT INTO `books` VALUES (17, '978-7-04-032302-3', '复变函数与积分变换', '高等教育出版社', 1);
INSERT INTO `books` VALUES (18, '7-5091-0147-6 ', '穴位用药治百病', '人民军医出版社', 1);
INSERT INTO `books` VALUES (19, '978-7-122-22476-7', '图解水暖电工技能一本通', ' 化学工业出版社', 1);
INSERT INTO `books` VALUES (20, '978-7-5502-4465-8 ', '无言的宇宙:隐藏在24个数学公式背后的故事', '北京联合出版公司', 1);
INSERT INTO `books` VALUES (21, '978-7-5067-7614-1', '坚持走路的健身奇效', ' 中国医药科技出版社', 1);
INSERT INTO `books` VALUES (22, '978-7-122-22644-0', '员工不安全行为图集', '化学工业出版社', 1);
INSERT INTO `books` VALUES (23, '978-7-5067-7613-4', '冻龄美女的经络美容书', '中国医药科技出版社', 2);
INSERT INTO `books` VALUES (24, '978-7-122-23658-6', '徒手格斗一学就会', ' 化学工业出版社', 2);
INSERT INTO `books` VALUES (25, '978-7-122-24082-8', '酒类生产工艺与配方', '化学工业出版社', 1);
INSERT INTO `books` VALUES (26, '978-7-122-24021-7', '腻子与建筑涂料新技术', '化学工业出版社', 1);
INSERT INTO `books` VALUES (27, '978-7-5067-7571-7', '肾病', ' 中国医药科技出版社', 2);
INSERT INTO `books` VALUES (28, '978-7-122-24413-0', '时尚健身实务指导', '化学工业出版社', 1);
INSERT INTO `books` VALUES (29, '978-7-5067-7582-3', '支气管炎', ' 中国医药科技出版社', 1);
INSERT INTO `books` VALUES (30, '978-7-5027-7582-3', '致命锋刃:全球军用刀具50', '化学工业出版社', 1);
INSERT INTO `books` VALUES (31, '978-7-122-23622-7', '单片机应用技术入门', '化学工业出版社', 1);
INSERT INTO `books` VALUES (32, '978-7-122-23485-8', '环境化学物质风险评估方法与应用', '化学工业出版社', 1);
INSERT INTO `books` VALUES (33, '978-7-5537-4873-3', '王敬:能救命的刮痧书', ' 江苏凤凰科学技术出版社', 1);
INSERT INTO `books` VALUES (34, '978-7-5680-0979-9', 'Ruby元编程', '华中科技大学出版社', 1);
INSERT INTO `books` VALUES (35, '978-7-5390-5294-6', '图解舌诊:伸伸舌头百病消', ' 江西科学技术出版社', 1);
INSERT INTO `books` VALUES (36, '978-7-5060-7887-0', '民国遗民:章太炎传', '东方出版社', 1);
-- ----------------------------
-- Table structure for borrow
-- ----------------------------
DROP TABLE IF EXISTS `borrow`;
CREATE TABLE `borrow` (
`ID` bigint NOT NULL AUTO_INCREMENT,
`book` varchar(30) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
`reader` varchar(30) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
`borrow_date` date NOT NULL,
`return_date` date NOT NULL,
PRIMARY KEY (`ID`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 7 CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of borrow
-- ----------------------------
INSERT INTO `borrow` VALUES (1, '谁动了我的奶酪', '王五', '2016-12-05', '2016-12-16');
INSERT INTO `borrow` VALUES (2, '小王子', '张三', '2016-12-05', '2016-12-16');
INSERT INTO `borrow` VALUES (4, '戏说PHP', '李四', '2016-12-05', '2016-12-16');
INSERT INTO `borrow` VALUES (7, 'C++语言程序设计', '李四', '2024-06-11', '2024-06-12');
-- ----------------------------
-- Table structure for readers
-- ----------------------------
DROP TABLE IF EXISTS `readers`;
CREATE TABLE `readers` (
`ID` bigint NOT NULL AUTO_INCREMENT,
`name` varchar(50) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
`code` varchar(30) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
`major` varchar(30) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
`stu_id` varchar(30) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
`sex` varchar(2) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
`born` bigint NOT NULL,
`fine` bigint NULL DEFAULT NULL,
`date` date NOT NULL,
PRIMARY KEY (`ID`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 6 CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of readers
-- ----------------------------
INSERT INTO `readers` VALUES (1, '张三', 'qaz123', '软件工程', '20140040102', '男', 1997, 0, '2016-12-05');
INSERT INTO `readers` VALUES (2, '王五', 'uio123', '会计学专业', '20160103012', '女', 1997, 0, '2016-12-05');
INSERT INTO `readers` VALUES (3, '路十一', '123456', '计算机科学', '20150200101', '男', 1997, 0, '2016-12-05');
INSERT INTO `readers` VALUES (4, '李四', '654321', '计算机科学', '20150200102', '男', 1997, 0, '2016-12-05');
INSERT INTO `readers` VALUES (6, '测试', '123456', '测试专业', '2020012345', '男', 2000, 0, '2020-07-11');
-- ----------------------------
-- View structure for book_info
-- ----------------------------
DROP VIEW IF EXISTS `book_info`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `book_info` AS select `b`.`ISBN` AS `BookNumber`,`b`.`TITLE` AS `BookName`,`b`.`STOCK` AS `TotalQuantity`,ifnull((`b`.`STOCK` - (select count(0) from `borrow` where ((`b`.`ISBN` = `borrow`.`book`) and (`borrow`.`return_date` = 0)))),0) AS `AvailableQuantity` from `books` `b`;
-- ----------------------------
-- Procedure structure for GetReaderBorrowingInfo
-- ----------------------------
DROP PROCEDURE IF EXISTS `GetReaderBorrowingInfo`;
delimiter ;;
CREATE PROCEDURE `GetReaderBorrowingInfo`(IN readerID VARCHAR(30))
BEGIN
-- 定义一个变量来存储读者的借书记录总数
DECLARE totalBorrowCount INT DEFAULT 0;
-- 查询读者的借书记录总数
SELECT COUNT(*) INTO totalBorrowCount
FROM borrow
WHERE reader = readerID;
-- 输出读者的借书记录总数
SELECT totalBorrowCount AS TotalBorrowCount;
-- 如果读者有借书记录,查询详细信息
IF totalBorrowCount > 0 THEN
SELECT
b.ISBN AS BookISBN,
b.TITLE AS BookTitle,
borrow.borrow_date AS BorrowDate,
borrow.return_date AS ReturnDate
FROM borrow
INNER JOIN books b ON borrow.book = b.ISBN
WHERE borrow.reader = readerID
ORDER BY borrow.borrow_date DESC;
ELSE
-- 如果没有借书记录,输出相应的信息
SELECT 'No borrowing records found for this reader.' AS Message;
END IF;
END
;;
delimiter ;
-- ----------------------------
-- Triggers structure for table borrow
-- ----------------------------
DROP TRIGGER IF EXISTS `after_borrow`;
delimiter ;;
CREATE TRIGGER `after_borrow` AFTER INSERT ON `borrow` FOR EACH ROW BEGIN
-- 减少书籍的在册数量
UPDATE books
SET STOCK = STOCK - 1
WHERE ISBN = NEW.book;
END
;;
delimiter ;
-- ----------------------------
-- Triggers structure for table borrow
-- ----------------------------
DROP TRIGGER IF EXISTS `after_return`;
delimiter ;;
CREATE TRIGGER `after_return` AFTER UPDATE ON `borrow` FOR EACH ROW BEGIN
-- 检查是否是还书操作
IF OLD.return_date IS NULL AND NEW.return_date IS NOT NULL THEN
-- 增加书籍的在册数量
UPDATE books
SET STOCK = STOCK + 1
WHERE ISBN = NEW.book;
END IF;
END
;;
delimiter ;
SET FOREIGN_KEY_CHECKS = 1;
5.Java代码思路及结构
此处只放部分代码截图