gpt4 book ai didi

java - 从 java 中的 executeBatch 返回的 PostgreSQL 结果集

转载 作者:行者123 更新时间:2023-11-29 13:18:06 28 4
gpt4 key购买 nike

我试图插入一个没有批量执行的值将返回我在查询中提到的值,但它在批量执行中不起作用。它只返回状态为 1 或 0 的整数数组。我的示例代码如下。

Connection connection = ConnectionManager.getInstance().getDBConnection();

String query = "insert into custom_attribute_mapping (product_id,attribute_id,attribute_values) values (49,22,'yyyyyyyyyyyyy') RETURNING attribute_mapping_id";

try (PreparedStatement prepStmt2 = connection
.prepareStatement(query )) {
int i =0;
ResultSet rs = prepStmt2.executeQuery();
while (rs.next()) {
System.out.println(rs.getInt(1));

}
} catch (Exception e) {

}

在上面的代码中,我得到了 attribute_mapping_id 的值。但在下面的代码中,executeBatch 的返回类型是一个整数数组,它只有插入状态。

Connection connection = ConnectionManager.getInstance().getDBConnection();

String query = "insert into custom_attribute_mapping (product_id,attribute_id,attribute_values) values (49,22,'yyyyyyyyyyyyy') RETURNING attribute_mapping_id";

try (PreparedStatement prepStmt2 = connection
.prepareStatement(query )) {
int i =0;
while(i<5){
i++;
prepStmt2.addBatch();
}
int[] rs = prepStmt2.executeBatch();
for(int r:rs){
System.out.println(r);
}

} catch (Exception e) {

}

谁能帮帮我。

最佳答案

使用 executeBatch() 时无法获取生成的值。它只会返回更新计数,甚至那些也不是很 clearly defined :

The elements in the array returned by the method executeBatch may beone of the following:

A number greater than or equal to zero -- indicates that the commandwas processed successfully and is an update count giving the number ofrows in the database that were affected by the command's execution

A value of SUCCESS_NO_INFO -- indicates that the command was processedsuccessfully but that the number of rows affected is unknown

If one ofthe commands in a batch update fails to execute properly, this methodthrows a BatchUpdateException, and a JDBC driver may or may notcontinue to process the remaining commands in the batch. However, thedriver's behavior must be consistent with a particular DBMS, eitheralways continuing to process commands or never continuing to processcommands. If the driver continues processing after a failure, thearray returned by the method BatchUpdateException.getUpdateCounts willcontain as many elements as there are commands in the batch, and atleast one of the elements will be the following:

A value of EXECUTE_FAILED -- indicates that the command failed toexecute successfully and occurs only if a driver continues to processcommands after a command fails

关于java - 从 java 中的 executeBatch 返回的 PostgreSQL 结果集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46212608/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com