1 |
1 |
package eu.dnetlib.data.claimsDemo;
|
2 |
2 |
|
|
3 |
import com.sun.rowset.CachedRowSetImpl;
|
3 |
4 |
import org.apache.log4j.Logger;
|
4 |
5 |
|
|
6 |
import javax.sql.rowset.CachedRowSet;
|
5 |
7 |
import java.sql.*;
|
6 |
8 |
import java.util.ArrayList;
|
7 |
9 |
import java.util.List;
|
... | ... | |
79 |
81 |
}
|
80 |
82 |
|
81 |
83 |
/**
|
|
84 |
* Closes Database Statement.
|
|
85 |
*
|
|
86 |
* @throws SQLException
|
|
87 |
*/
|
|
88 |
public void closeStatement(Statement st) throws SQLException {
|
|
89 |
log.debug("Closing Statement...");
|
|
90 |
if (st != null) {
|
|
91 |
st.close();
|
|
92 |
}
|
|
93 |
|
|
94 |
}
|
|
95 |
|
|
96 |
/**
|
|
97 |
* Closes Database Resultset.
|
|
98 |
*
|
|
99 |
* @throws SQLException
|
|
100 |
*/
|
|
101 |
public void closeResultset(ResultSet rs) throws SQLException {
|
|
102 |
log.debug("Closing Resultset...");
|
|
103 |
if (rs != null) {
|
|
104 |
rs.close();
|
|
105 |
}
|
|
106 |
|
|
107 |
}
|
|
108 |
|
|
109 |
/**
|
82 |
110 |
* Closes Database connections.
|
83 |
111 |
*
|
84 |
112 |
* @throws SQLException
|
85 |
113 |
*/
|
86 |
114 |
public void closeConnection() throws SQLException {
|
|
115 |
log.debug("Closing Connection...");
|
|
116 |
if (connection != null) {
|
|
117 |
connection.close();
|
|
118 |
}
|
87 |
119 |
|
88 |
|
if (connection != null) {
|
89 |
|
connection.close();
|
90 |
|
}
|
91 |
|
|
92 |
120 |
}
|
93 |
121 |
|
94 |
122 |
/**
|
... | ... | |
150 |
178 |
*/
|
151 |
179 |
public ResultSet executeQuery(String command) throws SQLException, SQLStoreException {
|
152 |
180 |
log.debug(" Executing Query ... " + command);
|
|
181 |
|
|
182 |
Statement st = null;
|
|
183 |
ResultSet rs = null;
|
|
184 |
|
153 |
185 |
try{
|
154 |
186 |
this.getConnection();
|
155 |
|
Statement st = connection.createStatement();
|
|
187 |
st = connection.createStatement();
|
156 |
188 |
|
157 |
189 |
if (st.execute(command)) {
|
158 |
190 |
|
159 |
|
ResultSet rs = st.getResultSet();
|
|
191 |
rs = st.getResultSet();
|
|
192 |
CachedRowSet rowset = new CachedRowSetImpl();
|
|
193 |
rowset.populate(rs);
|
|
194 |
|
|
195 |
return rowset;
|
160 |
196 |
//st.close();
|
161 |
|
return rs;
|
|
197 |
//return rs;
|
162 |
198 |
} else {
|
163 |
199 |
log.error("Fail to execute command " + st.getWarnings());
|
164 |
200 |
throw new SQLStoreException("Fail to execute command " + st.getWarnings());
|
... | ... | |
166 |
202 |
}
|
167 |
203 |
|
168 |
204 |
} finally {
|
|
205 |
this.closeResultset(rs);
|
|
206 |
this.closeStatement(st);
|
169 |
207 |
this.closeConnection();
|
170 |
208 |
}
|
171 |
209 |
|
... | ... | |
180 |
218 |
* @return
|
181 |
219 |
* @throws SQLException
|
182 |
220 |
*/
|
183 |
|
public ResultSet executeQuery(String command, ArrayList<Object> values) throws SQLException, SQLStoreException {
|
|
221 |
public CachedRowSet executeQuery(String command, ArrayList<Object> values) throws SQLException, SQLStoreException {
|
184 |
222 |
log.debug(" Executing Query ...\n" + command +"\n"+values);
|
|
223 |
|
|
224 |
PreparedStatement st = null;
|
|
225 |
ResultSet rs = null;
|
|
226 |
|
185 |
227 |
try{
|
186 |
228 |
this.getConnection();
|
187 |
|
PreparedStatement st = connection.prepareStatement(command);
|
|
229 |
st = connection.prepareStatement(command);
|
188 |
230 |
|
189 |
231 |
int pos = 1;
|
190 |
232 |
|
... | ... | |
196 |
238 |
}
|
197 |
239 |
|
198 |
240 |
if (st.execute()) {
|
199 |
|
ResultSet rs = st.getResultSet();
|
200 |
|
// st.close();
|
201 |
|
return rs;
|
|
241 |
rs = st.getResultSet();
|
|
242 |
CachedRowSet rowset = new CachedRowSetImpl();
|
|
243 |
rowset.populate(rs);
|
|
244 |
|
|
245 |
return rowset;
|
|
246 |
//return rs;
|
202 |
247 |
} else {
|
203 |
248 |
log.error("Fail to execute command " + st.getWarnings());
|
204 |
249 |
throw new SQLStoreException("Fail to execute command " + st.getWarnings());
|
205 |
250 |
}
|
206 |
251 |
|
207 |
252 |
} finally {
|
|
253 |
this.closeResultset(rs);
|
|
254 |
this.closeStatement(st);
|
208 |
255 |
this.closeConnection();
|
209 |
256 |
}
|
|
257 |
/*
|
210 |
258 |
|
|
259 |
ResultSet rs = null;
|
|
260 |
PreparedStatement st = null;
|
|
261 |
this.getConnection();
|
|
262 |
try {
|
|
263 |
st = connection.prepareStatement(command);
|
|
264 |
int pos = 1;
|
|
265 |
|
|
266 |
for (Object v : values) {
|
|
267 |
|
|
268 |
st.setObject(pos, v);
|
|
269 |
|
|
270 |
pos++;
|
|
271 |
}
|
|
272 |
try {
|
|
273 |
st.execute();
|
|
274 |
try {
|
|
275 |
rs = st.getResultSet();
|
|
276 |
CachedRowSetImpl rowset = new CachedRowSetImpl();
|
|
277 |
rowset.populate(rs);
|
|
278 |
return rowset;
|
|
279 |
} finally {
|
|
280 |
try {
|
|
281 |
rs.close();
|
|
282 |
} catch (SQLException e) {
|
|
283 |
log.error("Cannot close result set", e);
|
|
284 |
}
|
|
285 |
}
|
|
286 |
} finally {
|
|
287 |
try {
|
|
288 |
st.close();
|
|
289 |
} catch (SQLException e) {
|
|
290 |
log.error("Fail to execute command " + st.getWarnings());
|
|
291 |
log.error("Cannot close statement", e);
|
|
292 |
}
|
|
293 |
}
|
|
294 |
} finally {
|
|
295 |
try {
|
|
296 |
log.debug("Closing Connection...");
|
|
297 |
this.closeConnection();
|
|
298 |
} catch (SQLException e) {
|
|
299 |
log.error("Cannot close connection", e);
|
|
300 |
}
|
|
301 |
}
|
|
302 |
*/
|
|
303 |
|
211 |
304 |
}
|
212 |
305 |
|
213 |
306 |
/**
|
... | ... | |
219 |
312 |
*/
|
220 |
313 |
public void executeUpdate(String command) throws SQLException {
|
221 |
314 |
log.debug(" Executing Query ..." + command);
|
|
315 |
|
|
316 |
Statement st = null;
|
|
317 |
|
222 |
318 |
try{
|
223 |
319 |
this.getConnection();
|
224 |
|
Statement st = connection.createStatement();
|
|
320 |
st = connection.createStatement();
|
225 |
321 |
st.executeUpdate(command);
|
226 |
322 |
//st.close();
|
227 |
323 |
} finally {
|
|
324 |
this.closeStatement(st);
|
228 |
325 |
this.closeConnection();
|
229 |
326 |
}
|
230 |
327 |
}
|
... | ... | |
254 |
351 |
*/
|
255 |
352 |
public boolean executeUpdate(String command, ArrayList<Object> values) throws SQLException {
|
256 |
353 |
log.debug(" Executing Query ..." + command);
|
257 |
|
log.debug(" Executing Query ..." + command);
|
|
354 |
|
|
355 |
PreparedStatement st = null;
|
|
356 |
|
258 |
357 |
try{
|
259 |
358 |
this.getConnection();
|
260 |
359 |
|
261 |
|
PreparedStatement st = connection.prepareStatement(command);
|
|
360 |
st = connection.prepareStatement(command);
|
262 |
361 |
|
263 |
362 |
int pos = 1;
|
264 |
363 |
|
... | ... | |
277 |
376 |
}
|
278 |
377 |
return !executed;
|
279 |
378 |
} finally {
|
|
379 |
this.closeStatement(st);
|
280 |
380 |
this.closeConnection();
|
281 |
381 |
}
|
282 |
382 |
}
|
SqlStore.java:
close ResultSet and Statement in finally clause
return CachedRowSet instead of ResultSet when needed (avoids problems with closed connection, nothing else changes because CachedRowSet extends ResultSet)