Behavior of SELECT query using executeUpdate

java, jdbc, oracle

Solution

This behaviour is definetely contradicts `Statement.executeUpdate` API. What's interesting, `java.sql.Driver.jdbcCompliant` API says "A driver may only report true here if it passes the JDBC compliance tests". I tested `oracle.jdbc.OracleDriver.jdbcCompliant` - it returns true. I also tested `com.mysql.jdbc.Driver.jdbcCompliant` - it returns false. But in the same situation as you describe it throws

`Exception in thread "main" java.sql.SQLException: Can not issue SELECT via executeUpdate().`

It seems that JDBC drivers are unpredictable.

Problem

I have come across a strange behavior while executing a SELECT query using `Statement#executeUpdate()` by mistake. While the Javadoc clearly states that `executeUpdate() throws SQLException` if the given SQL statement produces a ResultSet object. But when I'm executing `SELECT * from TABLE_NAME`, I don't get any exception. Instead I'm getting an return value which is same as the no. of rows selected, if no. is less than or equal to 10. If the no. is more than 10, the return value is always 10. ``` Connection conn; Statement stmt; try { conn = getConnection(); stmt = conn.createStatement(); int count = stmt.executeUpdate("SELECT * from TABLE_NAME"); log.info("row count: " + count); } catch (SQLException e) { log.error(e); // handle exception } finally { DbUtils.closeQuietly(stmt); DbUtils.closeQuietly(conn); } ``` I am using Oracle 10g. Am I missing something here or is it up to the drivers to define their own behavior?

Original source