Oracle preparestatement中addBatch方法 抛异常的问题:不支持此特性

对于preparestatement.addBatch()方法使用报异常的地方原因是不能使用prepareStatement接口去实现addBatch()多个sql语句,addBatch()方法不接受参数的输入(即使传入的参数也无法执行,没有重写这个方法因此不支持),你可以查看一下官方文档API,大概意思是说:addBatch()只是使用适当的setXXX()方法将设置的绑定参数添加到批处理中,(对于CallbleStatement或OracleCallableStatement对象也是一样)

For prepared statements, update batching is used to batch multiple executions of the same statement with different sets of bind parameters. For a PreparedStatement or OraclePreparedStatement object, the addBatch() method takes no input–it simply adds the operation to the batch using the bind parameters last set by the appropriate setXXX() methods. (This is also true for CallableStatement or OracleCallableStatement objects, but remember that in the Oracle implementation of standard update batching, you will probably see no performance improvement in batching callable statements.)

For example (again assuming a Connection instance conn):


PreparedStatement pstmt =
conn.prepareStatement(“INSERT INTO employees VALUES(?, ?)”);

pstmt.setInt(1, 2000);
pstmt.setString(2, “Milo Mumford”);
pstmt.addBatch();

pstmt.setInt(1, 3000);
pstmt.setString(2, “Sulu Simpson”);
pstmt.addBatch();

At this point, two operations are in the batch.

Because a batch is associated with a single prepared statement object, you can batch only repeated executions of a single prepared statement, as in this example.

可以使用createStatement来实现这个功能,但性能就不好说了。。。。

Statement stmt = conn.createStatement();

stmt.addBatch(“INSERT INTO emp VALUES(1000, ‘Joe Jones’)”);
stmt.addBatch(“INSERT INTO dept VALUES(260, ‘Sales’)”);
stmt.addBatch(“INSERT INTO emp_dept VALUES(1000, 260)”);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值