-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsqlAction.java
153 lines (75 loc) · 2.21 KB
/
sqlAction.java
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package connect;
import java.sql.*;
import java.util.ArrayList;
import java.util.Calendar;
public class sqlAction{
public static Connection conn = jdbc.conn;
public static Statement statement = jdbc.statement;
public static ResultSet rs;
public static void executeSQL(String sql) throws SQLException {
if (conn == null) {
//try{
String Text = "连接无法找到,请先建立连接";
return;
}
else {
String[] commands = sql.replace('\n', ' ').split(";");
for (String aCommand: commands) {
if (aCommand.trim().toUpperCase().startsWith("SELECT")) {
processSQLSelect(aCommand);
}
else {
processSQLNonSelect(aCommand);
}
}
}
}
public static void processSQLSelect(String sql) throws SQLException {
try {
statement = conn.createStatement();
rs = statement.executeQuery(sql);
}catch (SQLException ex) {
ex.getStackTrace();
}
}
public static void processSQLNonSelect(String sql) {
try {
statement = conn.createStatement();
statement.executeUpdate(sql);
}
catch (SQLException ex) {
//?????
}
}
public static int getNum(String columnLabel){
int n = 0;
try {
n = rs.getInt(columnLabel);
}catch (SQLException ex) {
ex.getStackTrace();
}
return n;
}
public static String getString(String columnLabel) throws SQLException{
String s = "";
try {
s = rs.getString(columnLabel);
}catch (SQLException ex) {
ex.getStackTrace();
}
return s;
}
public static void updateReservation() throws SQLException {
Calendar now = Calendar.getInstance();
String today = "'"+now.YEAR+"-"+now.MONTH+"-"+now.DAY_OF_MONTH+"'";
String command = "";
sqlAction.executeSQL("SELECT r_id,r_date FROM reservation;");
while(sqlAction.rs.next()){
command += "UPDATE reservation SET distance = DATEDIFF('"+sqlAction.rs.getDate("r_date")
+"',"+ today +") WHERE r_id = "+sqlAction.rs.getInt("r_id")+";";
}
sqlAction.executeSQL(command);
command = "UPDATE reservation SET vaild = false WHERE distance <= 0;";
sqlAction.executeSQL(command);
}
}