我在Statement#executeUpdate()错误地执行SELECT查询时遇到了一个奇怪的行为.虽然Javadoc明确指出executeUpdate() throws SQLException如果给定的SQL语句产生ResultSet对象.但是当我执行时SELECT * from TABLE_NAME,我没有任何异常.相反,我得到的返回值与no相同.选择的行数,如果没有.小于等于10.如果不是.超过10,返回值始终为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);
}
Run Code Online (Sandbox Code Playgroud)
我正在使用Oracle 10g.
我在这里遗漏了什么,还是由司机来定义自己的行为?
这种行为绝对与Statement.executeUpdateAPI 相矛盾。有趣的是,
java.sql.Driver.jdbcCompliantAPI 表示“如果驱动程序通过了 JDBC 合规性测试,则只能在此处报告 true”。我测试过oracle.jdbc.OracleDriver.jdbcCompliant- 它返回 true。我也测试过com.mysql.jdbc.Driver.jdbcCompliant- 它返回 false。但在与您描述的情况相同的情况下,它会抛出
Exception in thread "main" java.sql.SQLException: Can not issue SELECT via executeUpdate().
看来 JDBC 驱动程序是不可预测的。