source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/DerbyWrapper.java@ 30232

Last change on this file since 30232 was 30232, checked in by ak19, 9 years ago

Modifications for testing in installer

File size: 22.7 KB
Line 
1/*
2 * DerbyWrapper.java
3 * Copyright (C) 2008 New Zealand Digital Library, http://www.nzdl.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19package org.greenstone.gsdl3.util;
20
21import java.io.File;
22import java.sql.Connection;
23import java.sql.DriverManager;
24import java.sql.PreparedStatement;
25import java.sql.ResultSet;
26import java.sql.SQLException;
27import java.sql.Statement;
28import java.util.ArrayList;
29import java.util.HashMap;
30import java.util.HashSet;
31
32import org.greenstone.gsdl3.service.Authentication;
33import org.greenstone.util.GlobalProperties;
34
35public class DerbyWrapper
36{
37 static final String PORT;
38 static final String DERBYSERVER;
39 static final String PROTOCOL;
40
41 // static code block to initialise the above
42 static {
43 // GlobalProperties won't be loaded at this point if running ant config-admin or ant config-user
44 // from the command line (both of which call ant update-userdb which in turn calls ModifyUsersDB.java)
45 // In such a case, the ant command will have set the system property (-Dgsdl3_writablehome)
46 // and passed this to ModifyUsersDB.java. Use that to load the GlobalProperties at this point
47
48 if(GlobalProperties.getGSDL3Home() == null) { // testing whether GlobalProperties is already loaded
49 String gsdl3_writablehome = System.getProperty("gsdl3.writablehome"); // set by 'ant update-userdb' cmd
50
51 try {
52 System.err.println("@@@@@ writablehome: " + gsdl3_writablehome);
53 GlobalProperties.loadGlobalProperties(gsdl3_writablehome);
54 } catch (Exception ex){
55 PORT = "1527";
56 DERBYSERVER = "localhost";
57 }
58 }
59
60 System.err.println("@@@@@ GlobalProperties.getGSDL3Home(): " + GlobalProperties.getGSDL3Home()); //test
61
62 // No more fallback values, use exactly what's propagated into global.properties from build.properties
63 // Fallback values are only for the installer to use
64 if (PORT == null) { // if these constants are not set yet because of exceptions
65 PORT = GlobalProperties.getProperty("derby.server.port", "1527");
66 DERBYSERVER = GlobalProperties.getProperty("derby.server", "localhost");
67 PROTOCOL = "jdbc:derby://"+DERBYSERVER+":"+PORT+"/"; // "jdbc:derby://localhost:1527"; // by default
68 }
69 System.err.println("@@@ PROTOCOL:" + PROTOCOL); //check in installer
70 }
71
72
73 static final String DRIVER = "org.apache.derby.jdbc.ClientDriver"; //"org.apache.derby.jdbc.EmbeddedDriver";
74 static final String USERSDB = "usersDB";
75 static final String USERS = "users";
76 static final String ROLES = "roles";
77 static final String DATA = "data";
78 private Connection conn = null;
79 private static String protocol_str;
80
81 public DerbyWrapper(String dbpath)
82 {
83 connectDatabase(dbpath, false);
84 }
85
86 public static void createDatabaseIfNeeded()
87 {
88 String usersDB_dir = GlobalProperties.getGSDL3Home() + File.separatorChar + "etc" + File.separatorChar + "usersDB";
89 protocol_str = PROTOCOL + usersDB_dir;
90 File usersDB_file = new File(usersDB_dir);
91 if (!usersDB_file.exists())
92 {
93 String etc_dir = GlobalProperties.getGSDL3Home() + File.separatorChar + "etc";
94 File etc_file = new File(etc_dir);
95 if (!etc_file.exists())
96 {
97 boolean success = etc_file.mkdir();
98 if (!success)
99 {
100 System.err.println("Couldn't create the etc dir under " + GlobalProperties.getGSDL3Home() + ".");
101 }
102 }
103 try
104 {
105 DerbyWrapper.createDatabase(DriverManager.getConnection(protocol_str + ";create=true"));
106 }
107 catch (Exception ex)
108 {
109 ex.printStackTrace();
110 }
111 }
112 }
113
114 public void connectDatabase(String dbpath, boolean create_database)
115 {
116 try
117 {
118 if (conn != null)
119 {
120 System.err.println("Connection already established, close the database first");
121 return;
122 }
123
124 Class.forName(DRIVER).newInstance();
125 protocol_str = PROTOCOL + dbpath;
126 if (create_database)
127 {
128 conn = DriverManager.getConnection(protocol_str + ";create=true");
129 }
130 else
131 {
132 conn = DriverManager.getConnection(protocol_str);
133 }
134 conn.setAutoCommit(false);
135 }
136 catch (Throwable e)
137 {
138 System.out.println("exception thrown:");
139 if (e instanceof SQLException)
140 {
141 printSQLError((SQLException) e);
142 }
143 else
144 {
145 e.printStackTrace();
146 }
147 }
148 }
149
150 public void closeDatabase()
151 {
152 try
153 {
154 conn.commit();
155 conn.close();
156 conn = null;
157 }
158 catch (SQLException e)
159 {
160 e.printStackTrace();
161 }
162 }
163
164 public static void shutdownDatabaseServer()
165 {
166
167 // shutdown the server if we're using an embedded derby
168 // if we're a derby client using the derby network server
169
170 if(!DRIVER.equals("org.apache.derby.jdbc.EmbeddedDriver")) {
171 return;
172 }
173
174 boolean gotSQLExc = false;
175 try
176 {
177 DriverManager.getConnection(PROTOCOL + ";shutdown=true");
178 }
179 catch (SQLException se)
180 {
181 // this is good (i.e. what Derby is designed to do on a successful shutdown)
182 gotSQLExc = true;
183 //System.out.println("Shutdown returned: " + se);
184 }
185 catch (Exception e)
186 {
187 e.printStackTrace();
188 }
189 if (!gotSQLExc)
190 {
191 System.err.println("Warning: Derby did not shut down normally");
192 }
193 }
194
195 public void clearUserData()
196 {
197 try
198 {
199 Statement state = conn.createStatement();
200 state.execute("drop table data");
201 state.execute("create table data (username varchar(40) not null, name varchar(128) not null, value clob, primary key (username, name))");
202 conn.commit();
203 state.close();
204 }
205 catch (SQLException e)
206 {
207 e.printStackTrace();
208 }
209 }
210
211 public void clearTrackerData()
212 {
213 try
214 {
215 Statement state = conn.createStatement();
216 state.execute("drop table usertracker");
217 state.execute("create table usertracker (username varchar(40) not null, collection varchar(128) not null, site varchar(128) not null, oid varchar(128) not null, time varchar(128) not null, action varchar(128) not null, primary key (username, time))");
218 conn.commit();
219 state.close();
220 }
221 catch (SQLException e)
222 {
223 e.printStackTrace();
224 }
225 }
226
227 public static void createDatabase(Connection conn)
228 {
229 try
230 {
231 Statement state = conn.createStatement();
232 state.execute("create table users (username varchar(40) not null, password varchar(40) not null, accountstatus varchar(10), comment varchar(100), email varchar(40), primary key(username))");
233 state.execute("create table roles (username varchar(40) not null, role varchar(40) not null, primary key (username, role))");
234 state.execute("create table data (username varchar(40) not null, name varchar(128) not null, value clob, primary key (username, name))");
235 state.execute("create table usertracker (username varchar(40) not null, collection varchar(128) not null, site varchar(128) not null, oid varchar(128) not null, time varchar(128) not null, action varchar(128) not null, primary key (username, time))");
236
237 state.execute("insert into " + USERS + " values ('admin', '" + Authentication.hashPassword("admin") + "', 'true', 'change the password for this account as soon as possible', '')");
238 state.execute("insert into " + ROLES + " values ('admin', 'administrator')");
239 state.execute("insert into " + ROLES + " values ('admin', 'all-collections-editor')");
240 conn.commit();
241 state.close();
242 conn.close();
243 }
244 catch (Exception ex)
245 {
246 ex.printStackTrace();
247 }
248 }
249
250 public void addUserAction(String username, String site, String collection, String oid, String action)
251 {
252 try
253 {
254 Statement state = conn.createStatement();
255 state.execute("INSERT INTO usertracker VALUES ('" + username + "', '" + collection + "', '" + site + "', '" + oid + "', '" + System.currentTimeMillis() + "', '" + action + "')");
256 conn.commit();
257 state.close();
258 }
259 catch (Exception ex)
260 {
261 ex.printStackTrace();
262 }
263 }
264
265 public ArrayList<HashMap<String, String>> getMostRecentUserActions(String site, String collection, String oid)
266 {
267 ArrayList<HashMap<String, String>> actions = new ArrayList<HashMap<String, String>>();
268
269 try
270 {
271 String query = "SELECT username, action, time FROM usertracker WHERE site = '" + site + "' and collection = '" + collection + "' and oid = '" + oid + "' ORDER BY time";
272 Statement state = conn.createStatement();
273 ResultSet rs = state.executeQuery(query);
274 conn.commit();
275
276 HashSet<String> usernamesSeen = new HashSet<String>();
277 while (rs.next())
278 {
279 String timeStr = rs.getString("time");
280 long time = Long.parseLong(timeStr);
281
282 if (System.currentTimeMillis() - time > 6000)
283 {
284 continue;
285 }
286
287 HashMap<String, String> action = new HashMap<String, String>();
288 if (!usernamesSeen.contains(rs.getString("username")))
289 {
290 action.put("username", rs.getString("username"));
291 action.put("action", rs.getString("action"));
292 actions.add(action);
293
294 usernamesSeen.add(rs.getString("username"));
295 }
296 }
297 state.close();
298
299 clearOldUserActions();
300 }
301 catch (Exception ex)
302 {
303 ex.printStackTrace();
304 }
305 return actions;
306 }
307
308 public void clearOldUserActions()
309 {
310 try
311 {
312 Statement state = conn.createStatement();
313 state.execute("DELETE FROM usertracker WHERE (CAST (time AS BIGINT)) < " + (System.currentTimeMillis() - 20000));
314 conn.commit();
315 state.close();
316 }
317 catch (Exception ex)
318 {
319 ex.printStackTrace();
320 }
321 }
322
323 public UserQueryResult listAllUsers() throws SQLException
324 {
325 UserQueryResult userQueryResult = new UserQueryResult();
326 String sql_list_all_user = "SELECT username, password, accountstatus, email, comment FROM " + USERS;
327
328 ArrayList<HashMap<String, String>> users = new ArrayList<HashMap<String, String>>();
329 Statement state = conn.createStatement();
330 ResultSet rs = state.executeQuery(sql_list_all_user);
331 conn.commit();
332 state.close();
333
334 while (rs.next())
335 {
336 HashMap<String, String> user = new HashMap<String, String>();
337 user.put("username", rs.getString("username"));
338 user.put("password", rs.getString("password"));
339 user.put("as", rs.getString("accountstatus"));
340 user.put("comment", rs.getString("comment"));
341 user.put("email", rs.getString("email"));
342
343 users.add(user);
344 }
345
346 for (HashMap<String, String> user : users)
347 {
348 ResultSet gs = state.executeQuery("SELECT role FROM " + ROLES + " WHERE username = '" + user.get("username") + "'");
349 String group = "";
350 while (gs.next())
351 {
352 if (!group.equals(""))
353 {
354 group += ",";
355 }
356 group += gs.getString("role");
357 }
358 userQueryResult.addUserTerm(user.get("username"), user.get("password"), group, user.get("as"), user.get("comment"), user.get("email"));
359 }
360
361 if (userQueryResult.getSize() == 0)
362 {
363 System.out.println("couldn't find any users");
364 return null;
365 }
366 else
367 {
368 return userQueryResult;
369 }
370 }
371
372 public boolean addUserData(String username, String name, String value)
373 {
374 //Check if we already have a value under this name
375 boolean found = false;
376 try
377 {
378 Statement state = conn.createStatement();
379 ResultSet rs = state.executeQuery("SELECT * FROM " + DATA + " WHERE username='" + username + "' AND name='" + name + "'");
380 conn.commit();
381 if (rs.next())
382 {
383 found = true;
384 }
385 else
386 {
387 found = false;
388 }
389 state.close();
390 }
391 catch (Exception ex)
392 {
393 System.out.println("exception thrown:");
394 if (ex instanceof SQLException)
395 {
396 printSQLError((SQLException) ex);
397 ex.printStackTrace();
398 }
399 else
400 {
401 ex.printStackTrace();
402 }
403
404 System.out.println("Error:" + ex.getMessage());
405 return false;
406 }
407
408 try
409 {
410 PreparedStatement stmt = null;
411 if (!found)
412 {
413 stmt = conn.prepareStatement("INSERT INTO " + DATA + " VALUES (?, ?, ?)");
414 stmt.setString(1, username);
415 stmt.setString(2, name);
416 stmt.setString(3, value);
417 stmt.executeUpdate();
418 }
419 else
420 {
421 stmt = conn.prepareStatement("UPDATE " + DATA + " SET value=? WHERE username=? AND name=?");
422 stmt.setString(1, value);
423 stmt.setString(2, username);
424 stmt.setString(3, name);
425 stmt.executeUpdate();
426 }
427 conn.commit();
428 stmt.close();
429 }
430 catch (Exception ex)
431 {
432 System.out.println("exception thrown:");
433 if (ex instanceof SQLException)
434 {
435 printSQLError((SQLException) ex);
436 }
437 else
438 {
439 ex.printStackTrace();
440 }
441
442 System.out.println("Error:" + ex.getMessage());
443 return false;
444 }
445 return true;
446 }
447
448 public String getUserData(String username, String name)
449 {
450 try
451 {
452 Statement state = conn.createStatement();
453 ResultSet rs = state.executeQuery("SELECT * FROM " + DATA + " WHERE username='" + username + "' AND name='" + name + "'");
454 conn.commit();
455 if (rs.next())
456 {
457 return rs.getString("value");
458 }
459 state.close();
460 }
461 catch (Exception ex)
462 {
463 System.out.println("exception thrown:");
464 if (ex instanceof SQLException)
465 {
466 printSQLError((SQLException) ex);
467 }
468 else
469 {
470 ex.printStackTrace();
471 }
472
473 System.out.println("Error:" + ex.getMessage());
474 }
475 return null;
476 }
477
478 public boolean addUser(String username, String password, String groups, String accountstatus, String comment, String email)
479 {
480 try
481 {
482 Statement state = conn.createStatement();
483 String sql_insert_user = "insert into " + USERS + " values ('" + username + "', '" + password + "', '" + accountstatus + "', '" + comment + "', '" + email + "')";
484 state.execute(sql_insert_user);
485
486 String[] groupArray = groups.split(",");
487 for (String g : groupArray)
488 {
489 String sql_insert_group = "insert into " + ROLES + " values ('" + username + "', '" + g + "')";
490 state.execute(sql_insert_group);
491 }
492
493 conn.commit();
494 state.close();
495 }
496 catch (Throwable e)
497 {
498 System.out.println("exception thrown:");
499 if (e instanceof SQLException)
500 {
501 printSQLError((SQLException) e);
502 }
503 else
504 {
505 e.printStackTrace();
506 }
507
508 System.out.println("Error:" + e.getMessage());
509 return false;
510 }
511
512 return true;
513 }
514
515 public boolean deleteUser(String del_username)
516 {
517 try
518 {
519 String sql_delete_user = "delete from " + USERS + " where username='" + del_username + "'";
520 String sql_delete_groups = "delete from " + ROLES + " where username='" + del_username + "'";
521 Statement state = conn.createStatement();
522 state.execute(sql_delete_user);
523 state.execute(sql_delete_groups);
524 conn.commit();
525 state.close();
526 }
527 catch (Throwable e)
528 {
529 System.out.println("exception thrown:");
530 if (e instanceof SQLException)
531 {
532 printSQLError((SQLException) e);
533 }
534 else
535 {
536 e.printStackTrace();
537 }
538 return false;
539 }
540 return true;
541 }
542
543 public boolean deleteAllUser() throws SQLException
544 {
545 try
546 {
547 Statement state = conn.createStatement();
548 state.execute("delete from " + USERS);
549 state.execute("delete from " + ROLES);
550 conn.commit();
551 state.close();
552 }
553 catch (Throwable e)
554 {
555 System.out.println("exception thrown:");
556 if (e instanceof SQLException)
557 {
558 printSQLError((SQLException) e);
559 }
560 else
561 {
562 e.printStackTrace();
563 }
564
565 return false;
566 }
567 return true;
568 }
569
570 public UserQueryResult findUser(String username, String password)
571 {
572 UserQueryResult userQueryResult = new UserQueryResult();
573
574 String sql_find_user = "SELECT username, password, accountstatus, comment, email FROM " + USERS;
575 String append_sql = "";
576
577 if (username != null)
578 {
579 append_sql = " WHERE username = '" + username + "'";
580 }
581 if (password != null)
582 {
583 if (append_sql.equals(""))
584 {
585 append_sql = " WHERE password = '" + password + "'";
586 }
587 else
588 {
589 append_sql += " and password = '" + password + "'";
590 }
591 }
592 if (!append_sql.equals(""))
593 {
594 sql_find_user += append_sql;
595 }
596
597 try
598 {
599 ArrayList<HashMap<String, String>> users = new ArrayList<HashMap<String, String>>();
600 Statement state = conn.createStatement();
601 ResultSet rs = state.executeQuery(sql_find_user);
602 while (rs.next())
603 {
604 HashMap<String, String> user = new HashMap<String, String>();
605 user.put("username", rs.getString("username"));
606 user.put("password", rs.getString("password"));
607 user.put("as", rs.getString("accountstatus"));
608 user.put("comment", rs.getString("comment"));
609 user.put("email", rs.getString("email"));
610
611 users.add(user);
612 }
613 conn.commit();
614
615 for (HashMap<String, String> user : users)
616 {
617 ResultSet gs = state.executeQuery("SELECT role FROM " + ROLES + " WHERE username = '" + user.get("username") + "'");
618
619 String group = "";
620 while (gs.next())
621 {
622 if (!group.equals(""))
623 {
624 group += ",";
625 }
626 group += gs.getString("role");
627 }
628
629 userQueryResult.addUserTerm(user.get("username"), user.get("password"), group, user.get("as"), user.get("comment"), user.get("email"));
630 }
631 state.close();
632 }
633 catch (Exception ex)
634 {
635 ex.printStackTrace();
636 return null;
637 }
638
639 if (userQueryResult.getSize() > 0)
640 {
641 return userQueryResult;
642 }
643 else
644 {
645 return null;
646 }
647 }
648
649 // findUser(null) will return all users, which is why a UserQueryResult
650 // (a vector of UserTermInfo) is returned
651 public UserQueryResult findUser(String username)
652 {
653 UserQueryResult userQueryResult = new UserQueryResult();
654
655 String sql_find_user = "SELECT username, password, accountstatus, comment, email FROM " + USERS;
656 String append_sql = "";
657
658 if (username != null)
659 {
660 append_sql = " WHERE username = '" + username + "'";
661 }
662 if (!append_sql.equals(""))
663 {
664 sql_find_user += append_sql;
665 }
666
667 ArrayList<HashMap<String, String>> users = new ArrayList<HashMap<String, String>>();
668
669 try
670 {
671 Statement state = conn.createStatement();
672 ResultSet rs = state.executeQuery(sql_find_user);
673 conn.commit();
674 while (rs.next())
675 {
676 HashMap<String, String> user = new HashMap<String, String>();
677 user.put("username", rs.getString("username"));
678 user.put("password", rs.getString("password"));
679 user.put("as", rs.getString("accountstatus"));
680 user.put("comment", rs.getString("comment"));
681 user.put("email", rs.getString("email"));
682
683 users.add(user);
684 }
685 state.close();
686
687 state = conn.createStatement();
688 for (HashMap<String, String> user : users)
689 {
690 ResultSet gs = state.executeQuery("SELECT role FROM " + ROLES + " WHERE username = '" + user.get("username") + "'");
691 conn.commit();
692
693 String group = "";
694 while (gs.next())
695 {
696 if (!group.equals(""))
697 {
698 group += ",";
699 }
700 group += gs.getString("role");
701 }
702
703 userQueryResult.addUserTerm(user.get("username"), user.get("password"), group, user.get("as"), user.get("comment"), user.get("email"));
704 }
705 state.close();
706 }
707 catch (Exception ex)
708 {
709 ex.printStackTrace();
710 }
711
712 if (userQueryResult.getSize() > 0)
713 {
714 return userQueryResult;
715 }
716 else
717 {
718 System.out.println("couldn't find the user");
719 return null;
720 }
721 }
722
723 public String modifyUserInfo(String username, String new_password, String groups, String accountstatus, String comment, String email)
724 {
725 try
726 {
727 String sql_modify_user_info = "update " + USERS + " set ";
728
729 boolean needComma = false;
730 if (new_password != null && !new_password.equals(""))
731 {
732 sql_modify_user_info += "password='" + new_password + "'";
733 needComma = true;
734 }
735
736 if (accountstatus != null && comment != null)
737 {
738 sql_modify_user_info += (needComma ? "," : "") + " accountstatus='" + accountstatus + "'" + ", comment='" + comment + "'";
739 needComma = true;
740 }
741
742 if (email != null)
743 {
744 sql_modify_user_info += (needComma ? "," : "") + " email='" + email + "'";
745 }
746
747 sql_modify_user_info += " where username='" + username + "'";
748 Statement state = conn.createStatement();
749 state.execute(sql_modify_user_info);
750
751 String sql_delete_groups = "delete from " + ROLES + " where username='" + username + "'";
752 state.execute(sql_delete_groups);
753
754 String[] groupsArray = groups.split(",");
755 for (String g : groupsArray)
756 {
757 String sql_insert_group = "insert into " + ROLES + " values ('" + username + "', '" + g + "')";
758 state.execute(sql_insert_group);
759 }
760
761 conn.commit();
762 state.close();
763 }
764 catch (Throwable e)
765 {
766 System.out.println("exception thrown:");
767 if (e instanceof SQLException)
768 {
769 printSQLError((SQLException) e);
770 }
771 else
772 {
773 e.printStackTrace();
774 }
775
776 return "Error:" + e.getMessage();
777 }
778 return "succeed";
779 }
780
781 public void db2txt()
782 {
783 System.err.println(db2txtString());
784 }
785
786 public String db2txtString()
787 {
788 //String db2txt = "Error in converting db2txt string.";
789 String db2txt = "";
790 try
791 {
792 String sql_list_all_user = "select username, password, accountstatus, comment, email from " + USERS;
793
794 Statement state = conn.createStatement();
795 ResultSet rs = state.executeQuery(sql_list_all_user);
796
797 ArrayList<HashMap<String, String>> infoMap = new ArrayList<HashMap<String, String>>();
798
799 while (rs.next())
800 {
801 HashMap<String, String> userMap = new HashMap<String, String>();
802 userMap.put("username", rs.getString("username"));
803 userMap.put("password", rs.getString("password"));
804 userMap.put("status", rs.getString("accountstatus"));
805 userMap.put("comment", rs.getString("comment"));
806 userMap.put("email", rs.getString("email"));
807 infoMap.add(userMap);
808 }
809 conn.commit();
810
811 StringBuffer buffer = new StringBuffer();//("-------------------------------------");
812 for (HashMap<String, String> user : infoMap)
813 {
814 ResultSet groupsSet = state.executeQuery("SELECT role FROM " + ROLES + " WHERE username = '" + user.get("username") + "'");
815 String returnedGroups = "";
816 while (groupsSet.next())
817 {
818 if (!returnedGroups.equals(""))
819 {
820 returnedGroups += ",";
821 }
822 returnedGroups += groupsSet.getString("role");
823 }
824 conn.commit();
825
826 buffer.append("USERNAME = " + user.get("username"));
827 buffer.append("\nPASSWORD = " + user.get("password"));
828 buffer.append("\nGROUPS = " + returnedGroups);
829 buffer.append("\nSTATUS = " + user.get("status"));
830 buffer.append("\nCOMMENT = " + user.get("comment"));
831 buffer.append("\nEMAIL = " + user.get("email"));
832 buffer.append("\n-------------------------------------\n");
833 }
834 db2txt = buffer.toString();
835
836 conn.commit();
837 state.close();
838 }
839 catch (Exception ex)
840 {
841 ex.printStackTrace();
842 }
843 finally
844 {
845 return db2txt;
846 }
847 }
848
849 static void printSQLError(SQLException e)
850 {
851 while (e != null)
852 {
853 System.out.println(e.toString());
854 e = e.getNextException();
855 }
856 }
857
858 public void clearUserDataWithPrefix(String username, String prefix)
859 {
860 try
861 {
862 Statement state = conn.createStatement();
863 state.execute("DELETE FROM data WHERE username = '" + username + "' AND SUBSTR(name, 1, " + prefix.length() + ") = '" + prefix + "'");
864 conn.commit();
865 state.close();
866 }
867 catch (Exception ex)
868 {
869 ex.printStackTrace();
870 }
871 }
872}
Note: See TracBrowser for help on using the repository browser.