我正在尝试使用连接器 mysql-connector-java-5.1.6-bin.jar
更新 MySql 数据库中的一批记录。根据这个answer ,驱动程序可能会在一条记录失败的情况下继续,或者它可能会抛出异常。
The JDBC driver may choose to attempt to execute all the statements in the batch if one statement fails or it may choose to stop executing statements in the batch once a statement fails.
根据 documentation在连接器 jar 中,它表示 continueOnError
默认为 true。
continueBatchOnError
Should the driver continue processing batch commands if one statement fails. The JDBC spec allows either way (defaults to 'true').
Default: true
Since version: 3.0.3
我想根据一些业务逻辑更改此属性值。那么如何在我的实现中(在我的 Java 代码中)将此属性设置为 true 或 false。
注意:我无法更改连接参数。我只是获取一个connection
对象并执行批量更新。
最佳答案
如果连接是 com.mysql.jdbc.JDBC4Connection
,您可以设置 continueBatchOnError
标志:
JDBC4Connection mysqlCon = (JDBC4Connection)con;
boolean current = mysqlCon.getContinueBatchOnError();
mysqlCon.setContinueBatchOnError(false);
否则(例如,如果连接被连接池实现包装)您可以使用 Connection.unwrap
获取 JDBC4Connection
。
关于java - 通过 Java 代码将 continueOnError 属性设置为 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36174745/