我实现了一个Java应用程序,该应用程序使用查询根据给定的ID集查询数据库:
select * from STUDENT where ID in (?)
一组ID将用于替换?。但是,偶尔我会收到一个例外:
?
Caused by: java.sql.SQLException: Numeric Overflow at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:70) at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:133) at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:199) at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:263) at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:271) at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:445) at oracle.jdbc.driver.NumberCommonAccessor.throwOverflow(NumberCommonAccessor.java:4319) at oracle.jdbc.driver.NumberCommonAccessor.getInt(NumberCommonAccessor.java:187) at oracle.jdbc.driver.OracleResultSetImpl.getInt(OracleResultSetImpl.java:712) at oracle.jdbc.driver.OracleResultSet.getInt(OracleResultSet.java:434)
经过一些测试,我意识到,如果将ID列表划分为多个较小的子列表,则异常停止发生。出于某种原因,jdbc不喜欢将太多的值放入IN (?)。我想知道是否有人曾经看过这个问题并对此有解释?由于此问题永远不会在生产环境中发生,而只会在本地环境(资源较少的本地环境)中发生,因此我怀疑这与服务器的资源有关。
IN (?)
谢谢
更新:我正在使用的源代码是:
// create a query private String getQueryString(int numOfParams) { StringBuilder out = new StringBuilder(); out.append("select * from STUDENT where ID in ("); for (int i = 0; i < numOfParams; i++) { if (i == numOfParams - 1) { out.append("?"); } else { out.append("?, "); } } out.append(")"); } // set parameters private void setParams(PreparedStatement ps, Set<String> params) { int index = 1; for (String param: params) { ps.setString(index++, param); } } public void queryStudent(Connection conn, Set<String> ids) throws Exception { String query = this.getQueryString(ids.size()); PreparedStatement ps = conn.prepareStatement(query); this.setParams(ps, ids); ps.executeQuery(); // do some operations with the result }
该问题是由GlassFish和应用程序之间的ojdbc驱动程序冲突引起的。为了修复它,我需要: