用oracle预处理语句执行批处理
我尝试使用以下代码添加批量预处理语句:
Connection c = ... PreparedStatement ps = c.prepareStatement(query1); ps.setObject(....) ... ps.addBatch(query2); // SqlException : Unsupported feature
是不是oracle jdbc驱动支持批处理,或者我做错了什么?
我正在使用oracle瘦驱动程序。 MANIFEST.MF Implementation-Version: 11.2.0.1.0
。
java.sql.SQLException: Unsupported feature at oracle.jdbc.driver.OraclePreparedStatement.addBatch(OraclePreparedStatement.java:9803) at oracle.jdbc.driver.OracleStatementWrapper.addBatch(OracleStatementWrapper.java:285) at org.jboss.resource.adapter.jdbc.WrappedStatement.addBatch(WrappedStatement.java:731) at
您正在使用query1
创建PreparedStatement
并将query2
添加到它不属于的已准备好的州语句中。
如果您正在使用PreparedStatement
,我建议使用PreparedStatement.addBatch()
方法。
PreparedStatement ps = c.prepareStatement(query1); ps.setObject(....); ps.addBatch(); //Voila
如果调用接受查询字符串的任何execute
, executeUpdate
, executeQuery
或addBatch
方法,JDBC规范明确要求PreparedStatement
(和CallableStatement
)实现抛出SQLException
。
例如,参见Statement.addBatch(String sql)
上的Javadoc:
抛出:
SQLException
– 如果发生数据库访问错误,则在关闭的Statement
上调用此方法,驱动程序不支持批量更新, 在PreparedStatement
或CallableStatement
该方法
(强调我的)
使用PreparedStatement
您只能使用setXXX
方法,然后使用addBatch()
为准备好的查询批量设置参数值(并为不同的参数值重复该参数值)。 您无法使用普通Statement
批量处理不同的查询。
使用PreparedStatement
批处理的方法大致如下:
try (PreparedStatement ps = c.prepareStatement(query1)) { while (moreParameterValueSets) { ps.setObject(....) //... ps.addBatch(); } ps.executeBatch(); }