-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase_manage.java
More file actions
312 lines (287 loc) · 10.5 KB
/
database_manage.java
File metadata and controls
312 lines (287 loc) · 10.5 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package dictionary;
/**Điều chỉnh lại hàm delete
* thêm hàm add memo
*/
import java.sql.*;
import java.lang.String;
import java.text.SimpleDateFormat;
import java.util.*;
public class database_manage {
static Connection c = null;
static Statement stmt = null;
static ResultSet rs = null;
static PreparedStatement pstsm = null;
public static ArrayList<String> temporal_user;
public static ArrayList<String> table_list;
static {
try {
table_list = temporal_list("name");
temporal_user = temporal_list("user");
} catch (SQLException e) {
e.printStackTrace();
}
}
static public ArrayList<String> temporal_list(String table) throws SQLException {
set_database();
ArrayList<String> result = get_list(table);
return result;
}
/** khoi tao. */
public static void set_database() {
try {
c = DriverManager.getConnection("jdbc:sqlite:./src/main/resources/dict_avva.db");
c.setAutoCommit(true);
stmt = c.createStatement();
} catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
}
/** dong. */
public static void close_database() throws SQLException {
if(rs == null) {}
else {
rs.close();
}
if(pstsm == null){}
else {
pstsm.close();
}
stmt.close();
c.close();
}
/** tim kiem trong database goc, table name la av hoac va, tra ve [0] la nghia va [1] la comment. */
public static String[] search(String r, String table_name) throws SQLException{
String[] result = new String[2];
r = r.trim();
r = r.replaceAll("\\s+","");
result[0] = "";
result[1] = "";
rs = stmt.executeQuery(String.format("select * from %s where word = '%s'",table_name, r.toLowerCase()));
int i = 0;
while (rs.next()) {
if(i == 0) {
result[1] = result[1] + rs.getString("comment");
}
String drc = rs.getString("html");
result[0] = result[0] + drc + "\n";
i++;
}
if(result[0] == ""){
rs.close();
result[0] = "<h1 style=\"color:Tomato;\"> This word doesn't exist </h1>";
}
if(rs == null) {}
else
rs.close();
return result;
}
/** list cac tu.*/
public static String[] list_word() throws SQLException {
rs = stmt.executeQuery("select * from av");
Set<String> a = new LinkedHashSet<> ();
while(rs.next()) {
a.add(rs.getString("word"));
}
rs = stmt.executeQuery("select * from user");
while(rs.next()) {
a.add(rs.getString("word"));
}
String[] result = new String [a.size()];
result = a.toArray(result);
rs.close();
return result;
}
/** nguoi dung them vao table user
* neu da co tu do thi false
* nguoc lai thi them vao va return true
* da co them 1 thuoc tinh la temporal_user chua
* cac tu trong user o tren.*/
public static boolean user_add (String word, String mean) throws SQLException {
word = word.replace("\n","<br>");
word = word.trim();
word = word.toLowerCase();
if (temporal_user.contains(word)) {
//System.out.print("deo add dc");
return false;
} else {
mean = mean.replace("\n","<br>");
mean = mean.trim();
mean = mean + "<br>";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
pstsm = c.prepareStatement("INSERT INTO USER (word, definition, time) VALUES (?,?,?)");
pstsm.setString(1, word);
pstsm.setString(2, mean.toLowerCase());
pstsm.setString(3, sdf.format(timestamp));
pstsm.executeUpdate();
if (pstsm == null) {
} else {
pstsm.close();
}
return true;
}
}
/** nguoi dung chinh sua (chi doi voi table user)
* mean la y nghia cua tu do, sau khi
* nguoi dung da chinh sua .*/
public static void make_change (String word, String mean) throws SQLException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
word = word.replace("\n","<br>");
mean = mean.replace("\n","<br>");
word = word.trim();
mean = mean.trim();
pstsm = c.prepareStatement("UPDATE user SET DEFINITION = ?, time = ? WHERE word = ?");
pstsm.setString(1,mean.toLowerCase());
pstsm.setString(2, sdf.format(timestamp));
pstsm.setString(3,word.toLowerCase());
pstsm.executeUpdate();
if (pstsm == null){}
else {
pstsm.close();
}
}
/** tra ve y nghia cho nguoi dung chinh sua
* tham so la tu do.*/
public static String user_meaning(String word) throws SQLException {
rs = stmt.executeQuery(String.format("select * from user where word = '%s'",word));
rs.next();
String result = "";
result = rs.getString("definition");
result = result.replace("<br>","\n");
return result;
}
/**add word vao trong mot memo da duoc tao*/
public static void add_memo (String word, String memo) throws SQLException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
pstsm = c.prepareStatement(String.format("INSERT INTO %s (word, time) VALUES (?,?)",memo));
pstsm.setString(1,word.toLowerCase());
pstsm.setString(2,sdf.format(timestamp));
pstsm.executeUpdate();
if (pstsm == null){}
else {
pstsm.close();
}
}
/**comment to origin table
* co the la av hoac va. */
public static void comment(String s, String word, String table_name) throws SQLException {
s = s.replace("\n","<br>");
s = "<h4>" +s + "</h4>" + "<br>";
pstsm = c.prepareStatement(String.format("UPDATE %s SET COMMENT = ? WHERE word = ?",table_name));
pstsm.setString(1,s);
pstsm.setString(2,word);
pstsm.executeUpdate();
if (pstsm == null){}
else {
pstsm.close();
}
}
/**Xoa comment cua word trong table_name
* table_name co the la av hoac va.*/
public static void delete_comment(String word, String table_name) throws SQLException {
pstsm = c.prepareStatement(String.format("UPDATE %s SET comment = NULL WHERE word = ?", table_name));
pstsm.setString(1,word.toLowerCase());
pstsm.executeUpdate();
if (pstsm == null){}
else {
pstsm.close();
}
}
/**tra lai string comment cua word trong table_name, table_name la av hoac va
* dua no vao mot textarea de nguoi dung thay va chinh sua
* sau khi chinh sua, su dung ham comment de cap nhat lai comment moi.
*/
public static String comment_change(String word, String table_name) throws SQLException {
rs = stmt.executeQuery(String.format("select * from %s where word = '%s'",table_name, word.toLowerCase()));
rs.next();
String result = "";
result = rs.getString("comment");
result = result.replace("<h4>","");
result = result.replace("</h4>","");
result = result.replace("<br>","\n");
if(rs == null) {}
else
rs.close();
return result;
}
/** xoa trong cac table memo cua user (ko cho phep xoa add , av, va) */
public static void user_delete (String word, String table_name) throws SQLException {
pstsm = c.prepareStatement(String.format("DELETE FROM %s WHERE word = ?", table_name));
word = word.replace("\n", "<br>");
word = word.trim();
word = word.replaceAll("\\s+", " ");
pstsm.setString(1, word);
pstsm.executeUpdate();
if (pstsm == null) {
} else {
pstsm.close();
}
}
/**tim kiem trong table add.*/
public static String user_search(String r) throws SQLException {
String s="";
r = r.trim();
r = r.replaceAll("\\s+"," ");
rs = stmt.executeQuery(String.format("select * from user where word = '%s'", r.toLowerCase()));
while (rs.next()) {
String drc = rs.getString("definition");
s = s + drc;
}
if (s == "") {
return "<h1 style=\"color:Tomato;\"> This word doesn't exist </h1>";
}
if(rs == null) {}
else
rs.close();
return "<h4>" + s + "</h4>";
}
/** nhan tham so la ten table va can co mot list_name de truyen
* tra ve false neu nhom da ton tai
* them nhom va tra ve true trong TH nguoc lai.*/
public static boolean add_group(String s) throws SQLException {
s = s.replaceAll("\\s+","_");
if(table_list.contains(s)) {
return false;
}
else {
pstsm = c.prepareStatement("CREATE TABLE " + s + " (" +
" word TEXT," +
" time TEXT" +
");");
pstsm.executeUpdate();
pstsm = c.prepareStatement("Insert into name (word) values (?)");
pstsm.setString(1,s);
pstsm.executeUpdate();
if (pstsm == null) {
} else {
pstsm.close();
}
return true;
}
}
public static void delete_table(String table_name) throws SQLException {
table_name = table_name.replaceAll("\\s+","_");
pstsm = c.prepareStatement(String.format("Drop table %s", table_name));
pstsm.executeUpdate();
pstsm = c.prepareStatement("Delete from name where word = ?");
pstsm.setString(1,table_name);
pstsm.executeUpdate();
if(pstsm == null) {
} else {
pstsm.close();
}
}
/**tra ve mot arrayList cac word o mot table duoc chon. */
public static ArrayList<String> get_list(String table_name) throws SQLException {
rs = stmt.executeQuery(String.format("select * from %s",table_name));
ArrayList<String> s = new ArrayList<>();
while(rs.next()) {
s.add(rs.getString("word"));
}
rs.close();
return s;
}
}