source: greenstone3/trunk/src/java/org/greenstone/gsdl3/sql/derby/DerbyDBWrapper.java@ 16883

Last change on this file since 16883 was 16883, checked in by xiao, 16 years ago

add changes to work with the ActivityManagement.java

File size: 6.0 KB
Line 
1package org.greenstone.gsdl3.sql.derby;
2
3import org.greenstone.gsdl3.sql.*;
4
5
6import java.sql.Connection;
7import java.sql.ResultSet;
8import java.sql.ResultSetMetaData;
9import java.sql.SQLException;
10import java.sql.SQLWarning;
11import java.sql.Statement;
12
13import java.util.ArrayList;
14import java.util.HashMap;
15import java.util.Iterator;
16
17
18import org.greenstone.gsdl3.util.DBInfo;
19
20import org.apache.log4j.*;
21
22public class DerbyDBWrapper implements MetadataDBWrapper{
23 protected SQLServer sqlServer = null;
24 protected SQLStatements sqlState = null;
25 protected Connection connection = null;
26 protected Statement statement = null;
27 protected static Logger logger = Logger.getLogger(org.greenstone.gsdl3.sql.derby.DerbyDBWrapper.class.getName());
28
29
30 public DBInfo getInfo(String id){
31 if(sqlState == null) return null;
32 String dbInfoState = sqlState.getDBInfoStatement(id);
33 ArrayList result = executeQuery(dbInfoState);
34 if (result.size() == 0) return null;
35 DBInfo info = new DBInfo();
36 for(int i=0; i<result.size();i++){
37 HashMap arow = (HashMap)result.get(i);
38 Iterator ite = arow.keySet().iterator();
39 while(ite.hasNext()){
40 String key = (String)ite.next();
41 Object value = arow.get(key);
42 //logger.info(key + "=>" +value);
43 if (value == null || value.equals("")) continue;
44 info.addInfo(key.toLowerCase(),sqlState.undoDBSafe(value));
45 }
46 }
47
48 return info;
49 }
50
51 public void setSQLServer(SQLServer server){
52 sqlServer = server;
53 }
54
55 public void setSQLStatements(SQLStatements state){
56 sqlState = state;
57 }
58
59 public boolean openConnection(String databasepath){
60 connection = sqlServer.connect(databasepath);
61
62 if (connection == null) return false;
63
64 try{
65 connection.setAutoCommit(false);
66 statement = connection.createStatement();
67 }
68 catch(SQLException sqle){
69 logger.debug("Database Error occured when creating a statement object",sqle);
70 return false;
71 }
72 return true;
73 }
74
75 public boolean openAndCreateConnection(String databasepath){
76 connection = sqlServer.connectAndCreate(databasepath);
77
78 if (connection == null) {
79 logger.error("sql connection is null. database path="+databasepath);
80 return false;
81 }
82
83 try{
84 connection.setAutoCommit(true);
85 //by passing the two arguments, the ResultSet returned from executeQuery is updatable
86 statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
87 if (statement == null) {
88 logger.error("statement is null");
89 return false;
90 }
91
92 }
93 catch(SQLException sqle){
94 logger.debug("Database Error occured when creating a statement object",sqle);
95 return false;
96 }
97 return true;
98 }
99
100 public synchronized ArrayList executeQuery(String query_statement){
101 //the database hasn't been correct yet
102 ArrayList results = new ArrayList();
103 try{
104
105 if (statement == null){
106 logger.info("the database hasn't been correct yet");
107 return new ArrayList();
108 }
109
110 ResultSet rs = statement.executeQuery(query_statement);
111 ResultSetMetaData rsmd = rs.getMetaData();
112 int numOfColumns = rsmd.getColumnCount();
113 while(rs.next()){
114 HashMap arow = new HashMap();
115 for(int i = 1; i <= numOfColumns ; i++){
116 arow.put(rsmd.getColumnName(i).toLowerCase(), rs.getObject(i));
117 }
118 results.add(arow);
119 }
120 }
121 catch(SQLException sqle){
122 logger.debug("Database Error occured when executeQuery " + query_statement,sqle);
123 return results;
124 }
125 catch(Exception e){
126 logger.debug(e);
127 return null;
128 }
129 return results;
130 }
131 /**
132 * Same as executeQuery except the return type is ResultSet
133 * @param stat
134 * @return ResultSet of querying the statement 'stat'
135 */
136 public synchronized ResultSet queryResultSet(String stat){
137 ResultSet rs = null;
138 try{
139 if (statement == null){
140 logger.info("Null sql statement provided.");
141 return null;
142 }
143 logger.debug(stat);
144 rs = statement.executeQuery(stat);
145
146 logger.info("sql stat="+stat+ " result="+rs);
147 }
148 catch(SQLException sqle){
149 logger.info("Database Error occured when execute query " + stat, sqle);
150 return null;
151 }
152 catch(Exception e){
153 logger.info("Exception="+e);
154 return null;
155 }
156 logger.debug(" result="+rs);
157 return rs;
158 }
159 /**
160 * Used by create (table)
161 */
162 public synchronized boolean execute(String stat) {
163 boolean rs;
164 try{
165 if (statement == null){
166 logger.info("statement is null.");
167 return false;
168 }
169
170 rs = statement.execute(stat);
171// connection.commit();
172 }
173 catch(SQLException sqle){
174 logger.debug("Database Error occured when execute query " + stat, sqle);
175 return false;
176 }
177 catch(Exception e){
178 logger.debug(e);
179 return false;
180 }
181 return rs;
182 }
183 /**
184 * Used by insert, update, and delete
185 */
186 public synchronized boolean executeUpdate(String stat) {
187 int rs;
188 try{
189 if (statement == null){
190 logger.info("statement is null.");
191 return false;
192 }
193
194 //either the row count of INSERT, UPDATE OR DELETE statement, or 0 if the statement returns nothing
195 rs = statement.executeUpdate(stat);
196// connection.commit();
197 logger.info("sql stat="+stat+ " result="+rs);
198 }
199 catch(SQLException sqle){
200 logger.info("Database Error occured when execute query " + stat, sqle);
201 return false;
202 }
203 catch(Exception e){
204 logger.info("Exception="+e);
205 return false;
206 }
207 logger.info(" result="+rs);
208 return (rs==-1)? false : true;
209 }
210 /**
211 * Used by checking the existence of table
212 * Same as the method 'execute' except throws SQLException
213 */
214 public void check4Table(String stat) throws SQLException {
215 statement.executeQuery(stat);
216 }
217
218 public void closeConnection(String databasepath){
219 sqlServer.disconnect(databasepath);
220 }
221
222 }
Note: See TracBrowser for help on using the repository browser.