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

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

Admin passwords set during installer stopped working, probably because of the recent change to DerbyWrapper where the fallback values were removed. During the installer the values in build.properties may not have propagated to global.properties yet, so need the derby host and derby port fallback values.

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