目录
题目:
要求:
设计表的参考步骤:
图书管理系统
~~~开始操作啦~~~
1、MySQL建库——library_system
2、在library_system库中创建三张表
3、到这里大概框架就完成啦,现在打开我们的 IDEA,与我们的数据库建立连接,注意新建一个项目,一定要使用maven哦,然后就创建相应的Java class文件就好了,见下文
a. 先创建DBUti (lJava class)对象,与数据库建立连接
b. 创建借阅记录(Record)对象
c. 创建借阅者(Reader)对象
d. 创建书籍(Book)对象
e. 创建上架书籍(PutAwayCommand)对象
f. 创建下架书籍(PutOffCommand)对象
g. 创建查看借阅记录(QueryAllRecords)对象
h. 创建借书(BorrowBookCommand)对象
源码地址:here 图书管理系统 简洁版
设计图书管理系统,包含学生和图书信息,且图书可以进行分类,学生可以在一个时间范围内借阅图书,并在这个时间范围内归还图书。
CREATE TABLE `library_system`.`books` (
`bid` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL COMMENT '书籍名称',
`count` INT NOT NULL COMMENT '存量',
`total` INT NOT NULL COMMENT '总量',
PRIMARY KEY (`bid`))
COMMENT = '书籍信息';
CREATE TABLE `library_system`.`readers` (
`rid` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL COMMENT '借阅者姓名',
PRIMARY KEY (`rid`))
COMMENT = '借阅者信息';
CREATE TABLE `library_system`.`records` (
`reid` INT NOT NULL AUTO_INCREMENT,
`bid` INT NOT NULL COMMENT '借阅书籍编号',
`rid` INT NOT NULL COMMENT '借阅者',
`borrowed_time` DATETIME NOT NULL DEFAULT current_timestamp COMMENT '借阅时间',
PRIMARY KEY (`reid`))
COMMENT = '借阅记录';
public class DBUtil {
// 由于 MysqlDataSource 对象只需要一份,所以,一个 static 就够了
private static final DataSource dataSource;
static {
MysqlDataSource db = new MysqlDataSource();
db.setServerName("localhost");
db.setPort(3306);
db.setUser("root");
db.setPassword("123456");
db.setDatabaseName("library_system");
db.setUseSSL(false);
db.setCharacterEncoding("utf-8");
db.setServerTimezone("Asia/Shanghai");
dataSource = db;
}
public static Connection connection() throws SQLException {
return dataSource.getConnection();
}
}
public class Record {
public int reid;
public int rid;
public int bid;
public String borrowed_time;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Record record = (Record) o;
return reid == record.reid;
}
@Override
public int hashCode() {
return Objects.hash(reid);
}
}
public class Reader {
public int rid;
public String name;
public String password;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Reader reader = (Reader) o;
return rid == reader.rid && Objects.equals(name, reader.name);
}
@Override
public int hashCode() {
return Objects.hash(rid, name);
}
}
public class Book {
public int bid;
public String name;
public int count;
public int total;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Book book = (Book) o;
return bid == book.bid && Objects.equals(name, book.name);
}
@Override
public int hashCode() {
return Objects.hash(bid, name);
}
}
public class PutAwayCommand {
public static void main(String[] args) throws SQLException {
//1.不存在登录
//2.提示用户输入本次操作的基本信息
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要上架的书名:");
String name = scanner.nextLine();
System.out.println("请输入本次要上架的书的数量:");
int count = scanner.nextInt();
System.out.println("DEBUG: 读取到用户输入的书名是 |" + name + "|");
System.out.println("DEBUG: 读取到用户输入的数量是 |" + count + "|");
// 3. 开始执行 SQL
// 3.1 先去执行 select
Integer bid = null;
try (Connection c = DBUtil.connection()) {
String sql = "select bid from books where name = ?";
try (PreparedStatement ps = c.prepareStatement(sql)) {
ps.setString(1,name);
System.out.println("DEBUG:执行SQL:" + ps);
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
//说明这里返回 1 行了
bid = rs.getInt("bid");
}
}
}
}
System.out.println("DEBUG:根据书名查询到的 bid = " + bid);
//根据 bid 是否为 null
if (bid == null) {
//通过 insert 进行上架
System.out.println("DEBUG: 使用 insert 进行上架");
try (Connection c = DBUtil.connection()) {
String sql = "insert into books (name, count, total) values (?, ?, ?)";
try (PreparedStatement ps = c.prepareStatement(sql)) {
ps.setString(1, name);
ps.setInt(2, count);
ps.setInt(3, count);
System.out.println("DEBUG: 执行 SQL: " + ps);
ps.executeUpdate();
}
}
}
else {
//通过 update 进行上架
System.out.println("DEBUG: 使用 update 进行上架");
try (Connection c = DBUtil.connection()) {
String sql = "update books set count = count + ?, total = total + ? where bid = ?";
try (PreparedStatement ps = c.prepareStatement(sql)) {
ps.setInt(1,count);
ps.setInt(2, count);
ps.setInt(3, bid);
System.out.println("DEBUG:执行 SQL:" + ps);
ps.executeUpdate();
}
}
}
System.out.println("上架成功");
}
}
public class PutOffCommand {
public static void main(String[] args) throws SQLException {
// 1.不需要的登录
// 2.读取用户输入
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要下架的书名:");
String name = scanner.nextLine();
System.out.println("请输入本次下架的书籍数量:");
int count = scanner.nextInt();
System.out.println("DEBUG: 读取到用户输入的书名是 |" + name + "|");
System.out.println("DEBUG: 读取到用户输入的数量是 |" + count + "|");
// 3.sql
// 3.1 select
// 根据用户输入 和 总量的关系,决定是 delect 还是 update
Integer bid = null;
Integer bookCount = null;
Integer total = null;
try (Connection c = DBUtil.connection()) {
String sql = "select bid, count, total from books where name = ?";
try (PreparedStatement ps = c.prepareStatement(sql)) {
ps.setString(1,name);
System.out.println("DEBUG: 执行 SQL: " + ps);
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
// Statement 和 ResultSet 还没有关闭,暂时不能执行其他的 SQL
bid = rs.getInt("bid");
bookCount = rs.getInt("count");
total = rs.getInt("total");
}
}
}
}
System.out.println("DEBUG: 根据书名查询到的 bid = " + bid);
System.out.println("DEBUG: 根据书名查询到的 bookCount = " + bookCount);
System.out.println("DEBUG: 根据书名查询到的 total = " + total);
if (bid == null) {
System.out.println("没有此书");
return;
}
if (count >= total) {
// delete
System.out.println("DEBUG: 使用 delete 下架");
try (Connection c = DBUtil.connection()) {
String sql = "delete from books where bid = ?";
try (PreparedStatement ps = c.prepareStatement(sql)) {
ps.setInt(1, bid);
System.out.println("DEBUG: 执行 SQL: " + ps);
ps.executeUpdate();
}
}
}else {
// update
System.out.println("DEBUG: 使用 update 下架");
try (Connection c = DBUtil.connection()) {
String sql = "update books set count = count - ?, total = total - ? where bid = ?";
// TODO: 存在一个 BUG,书籍的存量可能会出现负数
try (PreparedStatement ps = c.prepareStatement(sql)) {
ps.setInt(1, count);
ps.setInt(2, count);
ps.setInt(3, bid);
System.out.println("DEBUG: 执行 SQL: " + ps);
ps.executeUpdate();
}
}
}
}
}
public class QueryAllRecords {
public static void main(String[] args) throws SQLException {
try (Connection c = db.getConnection()) {
String sql = "select reid,rid,bid,borrowed_time from records order by reid";
try (PreparedStatement ps = c.prepareStatement(sql)) {
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
int reid = rs.getInt(1);
int rid = rs.getInt("rid");
String bid = rs.getString(3);
String borrowed_time = rs.getString("borrowed_time");
System.out.printf("%d,%d,%s,%s\n",reid,rid,bid,borrowed_time); //查询借阅记录操作
}
}
}
}
}
}
public class BorrowBookCommand {
public static void main(String[] args) throws SQLException {
// 1.登录
// 1.1 证明 你是你
// 1.2 权限(角色)
Scanner scanner = new Scanner(System.in);
System.out.println("请输入用户名:");
String username = scanner.nextLine();
// select * from readers where name = ?
int currentUserRid;
try (Connection c = DBUtil.connection()){
String sql = "select rid from readers where name = ?";
try (PreparedStatement ps = c.prepareStatement(sql)) {
ps.setString(1,username);
try (ResultSet rs = ps.executeQuery()) {
if (!rs.next()) {
System.out.println("登陆失败");
return;
}
currentUserRid = rs.getInt("rid");
}
}
}
System.out.println("登录成功");
System.out.print("请输入要借的书名: ");
String bookName = scanner.nextLine();
// select 书籍有无 && count > 0
// insert records + update books
int bid;
int count;
try (Connection c = DBUtil.connection()) {
String sql = "select bid,count from books where name = ?";
try (PreparedStatement ps = c.prepareStatement(sql)) {
ps.setString(1,bookName);
try (ResultSet rs = ps.executeQuery()) {
if (!rs.next()) {
System.out.println("查无此书");
return;
}
bid = rs.getInt("bid");
count = rs.getInt("count");
if (count == 0) {
System.out.println("书被借完了");
return;
}
}
}
}
//借书操作
try(Connection c = DBUtil.connection()) {
String sql = "update books set count = count - 1 where bid = ?";
try(PreparedStatement ps = c.prepareStatement(sql)) {
ps.setInt(1,bid);
ps.executeUpdate();
}
}
try (Connection c = DBUtil.connection()) {
String sql = "insert into records (rid, bid) values (?, ?)";
try (PreparedStatement ps = c.prepareStatement(sql)) {
ps.setInt(1, currentUserRid);
ps.setInt(2, bid);
ps.executeUpdate();
}
}
System.out.println("借书成功");
}
}
本小节完^_^