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

Last change on this file since 14312 was 14312, checked in by qq6, 17 years ago

updated modify user's info

File size: 8.2 KB
Line 
1package org.greenstone.gsdl3.util;
2
3import javax.swing.*;
4import java.io.File;
5import java.sql.Connection;
6import java.sql.DriverManager;
7import java.sql.ResultSet;
8import java.sql.SQLException;
9import java.sql.SQLWarning;
10import java.sql.Statement;
11import java.util.Properties;
12
13public class DerbyWrapper
14{
15 static final String PROTOCOL = "jdbc:derby:";
16 static final String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";
17 static final String USERSDB = "usersDB";
18 static final String USERS = "users";
19 private Connection conn=null;
20 private Statement state=null;
21 private String protocol_str;
22
23 public DerbyWrapper(){}
24 public DerbyWrapper(String dbpath){
25 connectDatabase(dbpath,false);
26 }
27
28 public void connectDatabase(String dbpath, boolean create_database){
29 try{
30 Class.forName(DRIVER).newInstance();
31 //System.out.println("Loaded the embedded driver.");
32 protocol_str = PROTOCOL + dbpath;
33 if (create_database){
34 conn = DriverManager.getConnection(protocol_str + ";create=true");
35 }else{
36 conn = DriverManager.getConnection(protocol_str);
37 }
38 state = conn.createStatement();
39 }catch (Throwable e){
40 System.out.println("exception thrown:");
41 if (e instanceof SQLException){
42 printSQLError((SQLException) e);
43 }else{
44 e.printStackTrace();
45 }
46 }
47 }
48
49 public void closeDatabase() {
50 state = null;
51 conn = null;
52 boolean gotSQLExc = false;
53 try{
54 //DriverManager.getConnection("jdbc:derby:;shutdown=true");
55 DriverManager.getConnection(protocol_str + ";shutdown=true");
56 }catch (SQLException se){
57 gotSQLExc = true;
58 }
59 if (!gotSQLExc){
60 System.out.println("Database did not shut down normally");
61 }
62 }
63
64 public void createDatabase()throws SQLException{
65 conn.setAutoCommit(false);
66 state.execute("create table users (username varchar(40) not null, password varchar(40) not null, groups varchar(500), accountstatus varchar(10), comment varchar(100), primary key(username))");
67 //ystem.out.println("table users created successfully!");
68 state.execute("insert into "+USERS+" values ('admin', 'admin', 'administrator,all-collections-editor', 'true', 'change the password for this account as soon as possible')");
69 conn.commit();
70 closeDatabase();
71 }
72
73 public UserQueryResult listAllUser()throws SQLException{
74 UserQueryResult userQueryResult=new UserQueryResult();
75 String sql_list_all_user="select username, password, groups, accountstatus, comment from " + USERS ;
76
77 ResultSet rs = state.executeQuery(sql_list_all_user);
78 while (rs.next()){
79 String returned_username= rs.getString("username");
80 String returned_password=rs.getString("password");
81 String returned_groups=rs.getString("groups");
82 String returned_accountstatus=rs.getString("accountstatus");
83 String returned_comment=rs.getString("comment");
84 userQueryResult.addUserTerm(returned_username,returned_password,returned_groups,returned_accountstatus,returned_comment);
85 }
86 if (userQueryResult.getSize()==0){
87 System.out.println("couldn't find any users");
88 return null;
89 }else{
90 return userQueryResult;
91 }
92 }
93
94 public String addUser(String username, String password, String groups, String accountstatus, String comment)throws SQLException{
95 conn.setAutoCommit(false);
96 String sql_insert_user="insert into " + USERS + " values ('"+username +"', '"+password+"', '"+groups+"', '"+accountstatus+"', '"+comment+"')";
97 try{
98 state.execute(sql_insert_user);
99 conn.commit();
100 }catch(Throwable e){
101 System.out.println("exception thrown:");
102 if (e instanceof SQLException){
103 printSQLError((SQLException) e);
104 }else{
105 e.printStackTrace();
106 }
107 closeDatabase();
108 System.out.println("Error:"+e.getMessage());
109 return "Error:"+e.getMessage();
110 }
111 return "succeed";
112 }
113
114 public String deleteUser(String del_username)throws SQLException{
115 conn.setAutoCommit(false);
116 String sql_delete_user="delete from " + USERS + " where username='"+del_username+"'";
117 try{
118 state.execute(sql_delete_user);
119 conn.commit();
120 }catch(Throwable e){
121 System.out.println("exception thrown:");
122 if (e instanceof SQLException){
123 printSQLError((SQLException) e);
124 }else{
125 e.printStackTrace();
126 }
127 closeDatabase();
128 return "Error:"+e.getMessage();
129 }
130 return "succeed";
131 }
132
133 public UserQueryResult findUser(String username, String password)throws SQLException{
134 UserQueryResult userQueryResult=new UserQueryResult();
135
136 conn.setAutoCommit(false);
137 String sql_find_user="SELECT username, password, groups, accountstatus, comment FROM "+USERS;
138 String append_sql="";
139
140 if (username!=null){
141 append_sql=" WHERE username = '" + username+ "'";
142 }
143 if (password!=null){
144 if (append_sql.equals("")){
145 append_sql=" WHERE password = '"+password+"'";
146 }else{
147 append_sql+=" and password = '"+password+"'";
148 }
149 }
150 if (!append_sql.equals("")){
151 sql_find_user+=append_sql;
152 }
153 ResultSet rs = state.executeQuery(sql_find_user);
154 while (rs.next()){
155 String returned_username= rs.getString("username");
156 System.out.println("returned_username :" + returned_username);
157 String returned_password=rs.getString("password");
158 System.out.println("returned_password :" + returned_password);
159 String returned_groups=rs.getString("groups");
160 System.out.println("returned_groups :" + returned_groups);
161 String returned_accountstatus=rs.getString("accountstatus");
162 System.out.println("returned_accountstatus :" + returned_accountstatus);
163 String returned_comment=rs.getString("comment");
164 System.out.println("returned_comment :" + returned_comment);
165 userQueryResult.addUserTerm(returned_username,returned_password,returned_groups,returned_accountstatus,returned_comment);
166 System.out.println(userQueryResult.toString());
167 }
168 conn.commit();
169 if (userQueryResult.getSize()==0){
170 System.out.println("couldn't find the user");
171 return null;
172 }else{
173 return userQueryResult;
174 }
175 }
176
177 public String modifyUserInfo(String username, String new_password,String groups, String accountstatus, String comment)throws SQLException{
178 conn.setAutoCommit(false);
179 String sql_modify_user_info="update " + USERS +" set ";
180 if (new_password!=null && !new_password.equals("")){
181 sql_modify_user_info += "password='" + new_password +"', ";
182 }
183
184 sql_modify_user_info += "groups='" + groups + "'" + ", accountstatus='" + accountstatus + "'" + ", comment='" + comment + "'" + " where username='" + username+"'";
185 try{
186 System.out.println(sql_modify_user_info);
187 state.execute(sql_modify_user_info);
188 conn.commit();
189 }catch(Throwable e){
190 System.out.println("exception thrown:");
191 if (e instanceof SQLException){
192 printSQLError((SQLException) e);
193 }else{
194 e.printStackTrace();
195 }
196 closeDatabase();
197 return "Error:"+e.getMessage();
198 }
199 return "succeed";
200 }
201
202 private void db2txt() throws SQLException{
203 UserQueryResult userQueryResult=new UserQueryResult();
204 String sql_list_all_user="select username, password, groups, accountstatus, comment from " + USERS ;
205 ResultSet rs = state.executeQuery(sql_list_all_user);
206
207 while (rs.next()){
208 String returned_username= rs.getString("username");
209 System.out.println("[" + returned_username+"]");
210 String returned_comment=rs.getString("comment");
211 System.out.println("<comment>" + returned_comment);
212 String returned_accountstatus=rs.getString("accountstatus");
213 System.out.println("<enabled>" + returned_accountstatus);
214 String returned_groups=rs.getString("groups");
215 System.out.println("<groups>" + returned_groups);
216 String returned_password=rs.getString("password");
217 System.out.println("<password>" + returned_password);
218 System.out.println("<username>" + returned_username);
219 System.out.println("");
220 System.out.println("----------------------------------------------------------------------");
221 }
222 conn.commit();
223 closeDatabase();
224 }
225
226 static void printSQLError(SQLException e)
227 {
228 while (e != null)
229 {
230 System.out.println(e.toString());
231 e = e.getNextException();
232 }
233 }
234
235 public static void main(String[] args) throws SQLException{
236 if (args.length!=1){
237 System.out.println("The path of usersDB has to be given!");
238 System.exit(0);
239 }
240 DerbyWrapper derbyWrapper=new DerbyWrapper(args[0]);
241 derbyWrapper.db2txt();
242 }
243}
Note: See TracBrowser for help on using the repository browser.