forked from kongxin-github/Java_Library_Management_System
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBorrowRecords.java
More file actions
134 lines (115 loc) · 3.3 KB
/
BorrowRecords.java
File metadata and controls
134 lines (115 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package database;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Vector;
import javax.swing.JOptionPane;
public class BorrowRecords {
public BorrowRecords() {
}
//借阅图书
public static void Borrow(String user, int bookid, String bookname) {
Connection con = ConnectDatabase.connectDB();
PreparedStatement preSql;
String sqlStr = "insert into borrowrecords(user,bookid,bookname,borrowtime,returntime,status) values (?,?,?,now(),null,?)";
try {
preSql = con.prepareStatement(sqlStr);
preSql.setString(1, user);
preSql.setInt(2, bookid);
preSql.setString(3, bookname);
preSql.setString(4, "未还");
int ok = preSql.executeUpdate();
con.close();
ChangeBorrowState(bookid);
} catch (SQLException e) {
}
}
//借阅图书修改图书状态
private static void ChangeBorrowState(int bookid) {
Connection con = ConnectDatabase.connectDB();
PreparedStatement preSql;
String sqlStr = "update booktable set state=? where bookid = ?";
try {
preSql = con.prepareStatement(sqlStr);
preSql.setString(1, "外借");
preSql.setInt(2, bookid);
int ok = preSql.executeUpdate();
con.close();
} catch (SQLException e) {
}
}
//还书
public static void Return(String user, int bookid) {
Connection con = ConnectDatabase.connectDB();
PreparedStatement preSql;
String sqlStr = "select now()";
ResultSet rs;
Date date=null;
try {
preSql = con.prepareStatement(sqlStr);
rs = preSql.executeQuery();
while(rs.next()) {
date = rs.getDate(1);
}
sqlStr = "update borrowrecords set returntime = ? where bookid = ? and user = ? and status = ?";
preSql = con.prepareStatement(sqlStr);
preSql.setDate(1, date);
preSql.setInt(2, bookid);
preSql.setString(3, user);
preSql.setString(4, "未还");
int ok = preSql.executeUpdate();
sqlStr = "update borrowrecords set status = ? where bookid = ? and user = ? and status = ?";
preSql = con.prepareStatement(sqlStr);
preSql.setString(1, "已还");
preSql.setInt(2, bookid);
preSql.setString(3, user);
preSql.setString(4, "未还");
ok = preSql.executeUpdate();
con.close();
ChangeReturnState(bookid);
} catch (SQLException e) {
}
}
//还书图书修改图书状态
private static void ChangeReturnState(int bookid) {
Connection con = ConnectDatabase.connectDB();
PreparedStatement preSql;
String sqlStr = "update booktable set state=? where bookid = ?";
try {
preSql = con.prepareStatement(sqlStr);
preSql.setString(1, "在馆");
preSql.setInt(2, bookid);
int ok = preSql.executeUpdate();
con.close();
} catch (SQLException e) {
}
}
//比对还书
public static boolean comparison(String user,int bookid) {
Connection con = ConnectDatabase.connectDB();
PreparedStatement preSql;
ResultSet rs;
String sqlStr = "select * from borrowrecords where bookid = ? and status = ?";
try {
preSql = con.prepareStatement(sqlStr);
preSql.setInt(1, bookid);
preSql.setString(2, "未还");
rs = preSql.executeQuery();
while (rs.next()) {
String user2 = rs.getString(2);
if(user2.equals(user)) {
return true;
}else {
return false;
}
}
con.close();
return false;
} catch (SQLException e) {
return false;
}
}
}