1 61 62 package org.apache.commons.dbutils; 63 64 import java.sql.Connection ; 65 import java.sql.PreparedStatement ; 66 import java.sql.ResultSet ; 67 import java.sql.SQLException ; 68 import java.sql.Types ; 69 import java.util.Arrays ; 70 71 import javax.sql.DataSource ; 72 73 84 public class QueryRunner { 85 86 89 protected DataSource ds = null; 90 91 94 public QueryRunner() { 95 super(); 96 } 97 98 105 public QueryRunner(DataSource ds) { 106 super(); 107 this.ds = ds; 108 } 109 110 118 protected void fillStatement(PreparedStatement stmt, Object [] params) 119 throws SQLException { 120 121 if (params == null) { 122 return; 123 } 124 125 for (int i = 0; i < params.length; i++) { 126 if (params[i] != null) { 127 stmt.setObject(i + 1, params[i]); 128 } else { 129 stmt.setNull(i + 1, Types.OTHER); 130 } 131 } 132 } 133 134 137 public DataSource getDataSource() { 138 return this.ds; 139 } 140 141 155 protected PreparedStatement prepareStatement(Connection conn, String sql) 156 throws SQLException { 157 158 return conn.prepareStatement(sql); 159 } 160 161 172 public Object query( 173 Connection conn, 174 String sql, 175 Object param, 176 ResultSetHandler rsh) 177 throws SQLException { 178 179 return this.query(conn, sql, new Object [] { param }, rsh); 180 } 181 182 193 public Object query( 194 Connection conn, 195 String sql, 196 Object [] params, 197 ResultSetHandler rsh) 198 throws SQLException { 199 200 PreparedStatement stmt = null; 201 ResultSet rs = null; 202 Object result = null; 203 204 try { 205 stmt = this.prepareStatement(conn, sql); 206 this.fillStatement(stmt, params); 207 208 rs = this.wrap(stmt.executeQuery()); 209 210 result = rsh.handle(rs); 211 212 } catch (SQLException e) { 213 this.rethrow(e, sql, params); 214 215 } finally { 216 DbUtils.close(rs); 217 DbUtils.close(stmt); 218 } 219 220 return result; 221 } 222 223 233 public Object query(Connection conn, String sql, ResultSetHandler rsh) 234 throws SQLException { 235 236 return this.query(conn, sql, (Object []) null, rsh); 237 } 238 239 252 public Object query(String sql, Object param, ResultSetHandler rsh) 253 throws SQLException { 254 255 return this.query(sql, new Object [] { param }, rsh); 256 } 257 258 273 public Object query(String sql, Object [] params, ResultSetHandler rsh) 274 throws SQLException { 275 276 Connection conn = this.ds.getConnection(); 277 278 try { 279 return this.query(conn, sql, params, rsh); 280 281 } finally { 282 DbUtils.close(conn); 283 } 284 } 285 286 298 public Object query(String sql, ResultSetHandler rsh) throws SQLException { 299 return this.query(sql, (Object []) null, rsh); 300 } 301 302 315 protected void rethrow(SQLException cause, String sql, Object [] params) 316 throws SQLException { 317 318 StringBuffer msg = new StringBuffer (cause.getMessage()); 319 320 msg.append(" Query: "); 321 msg.append(sql); 322 msg.append(" Parameters: "); 323 324 if (params == null) { 325 msg.append("[]"); 326 } else { 327 msg.append(Arrays.asList(params)); 328 } 329 330 SQLException e = new SQLException (msg.toString()); 331 e.setNextException(cause); 332 333 throw e; 334 } 335 336 344 public void setDataSource(DataSource dataSource) { 345 this.ds = dataSource; 346 } 347 348 357 public int update(Connection conn, String sql) throws SQLException { 358 return this.update(conn, sql, (Object []) null); 359 } 360 361 371 public int update(Connection conn, String sql, Object param) 372 throws SQLException { 373 374 return this.update(conn, sql, new Object [] { param }); 375 } 376 377 386 public int update(Connection conn, String sql, Object [] params) 387 throws SQLException { 388 389 PreparedStatement stmt = null; 390 int rows = 0; 391 392 try { 393 stmt = this.prepareStatement(conn, sql); 394 this.fillStatement(stmt, params); 395 396 rows = stmt.executeUpdate(); 397 398 } catch (SQLException e) { 399 this.rethrow(e, sql, params); 400 401 } finally { 402 DbUtils.close(stmt); 403 } 404 405 return rows; 406 } 407 408 417 public int update(String sql) throws SQLException { 418 return this.update(sql, (Object []) null); 419 } 420 421 431 public int update(String sql, Object param) throws SQLException { 432 return this.update(sql, new Object [] { param }); 433 } 434 435 446 public int update(String sql, Object [] params) throws SQLException { 447 448 Connection conn = this.ds.getConnection(); 449 450 try { 451 return this.update(conn, sql, params); 452 453 } finally { 454 DbUtils.close(conn); 455 } 456 } 457 458 479 protected ResultSet wrap(ResultSet rs) { 480 return rs; 481 } 482 483 } 484 | Popular Tags |