java - 通过 Java 代码将 continueOnError 属性设置为 true

标签 java mysql jdbc

我正在尝试使用连接器 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/

相关文章:

c# - 字节序 java/.net

java - 访问器方法和 Java 反射

php changeint 到一定程度

Java:数据库连接代码问题

sql-server - SQLServer异常: The conversion from timestamp to TIMESTAMP is unsupported.

java - Azure 构建管道,docker compose - 设置环境变量

java - 使用自定义类比 map 有什么好处?

mysql - 从日期时间获取单个日期或日期范围下的详细信息的 SQL 查询问题

mysql外键约束

java - 传递日期参数以创建日期范围查询