I've been using DBUtils for a few weeks only so please excuse my green-ness...

I was wondering why this was possible in DBUtils without some sort of exception 
being thrown:

//---------------------------------------------
QueryRunner theRunner = new QueryRunner(myDataSource);

theRunner.query( "delete * from  sometable",
                 new Object[] {},
                 new ArrayListHandler() );
//---------------------------------------------

and similarly:

//---------------------------------------------
QueryRunner theRunner = new QueryRunner(myDataSource);

theRunner.update( "select somecol from sometable",
                  new Object[] {},
                  new ArrayListHandler() );
//---------------------------------------------

I looked through the DBUtils code and it seems that it relies on JDBC to throw a 
SQLException when executeUpdate() is called on a statement representing 
something other than an update, or when query() is called with something other 
than a "select" statement:

/***
 * Execute an SQL INSERT, UPDATE, or DELETE query.
 * 
 * @param conn The connection to use to run the query.
 * @param sql The SQL to execute.
 * @param params The query replacement parameters.
 * @return The number of rows updated.
 * @throws SQLException
 */
 public int update(Connection conn, String sql, Object[] params)
 throws SQLException {
   PreparedStatement stmt = null;
   int rows = 0;

   try {
      stmt = this.prepareStatement(conn, sql);
      this.fillStatement(stmt, params);

      rows = stmt.executeUpdate();
   } catch (SQLException e) {
      this.rethrow(e, sql, params);
   } finally {
      DbUtils.close(stmt);
   }
   return rows;
 }

The JDBC API docs for PreparedStatement.exectuteUpdate say to expect a 
SQLException if the method is called on a Statement wrapping anything that 
"produces a ResultSet object":

------------------------------------------------
public int executeUpdate(String sql) throws SQLException

Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE 
statement or an SQL statement that returns nothing, such as an SQL DDL 
statement.

Parameters:
sql - an SQL INSERT, UPDATE or DELETE statement or an SQL statement that returns 
nothing 

Returns: either the row count for INSERT, UPDATE or DELETE statements, or 0 for 
SQL statements that return nothing

Throws: SQLException - if a database access error occurs or the given SQL 
statement produces a ResultSet object
------------------------------------------------


So I'm puzzled that the code below, using JDBC directly, doesn't throw an 
exception... the query theStmt is bound to is a select that definitely produces 
a ResultSet:

String theSQL = "select somecol from sometable";
java.sql.PreparedStatement theStmt = theConnection.prepareStatement( theSQL );
theCount = theStmt.executeUpdate();

Seems like DbUtils is doing the right thing but the JDBC driver isn't throwing 
the SQLException it said it should throw in this case?

I'm using Oracle 8.i and the Oracle OCI JDBC driver.  I know this isn't a 
DbUtils question per se, but I was wondering if this was something any of you 
have encountered before, and if it's a simple mistake I'm making or a mistake 
I've made interpreting the documentation above.  I would expect to see an 
exception if I tried to run a select as an update or an update as a query.

thanks!
Hernan



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to