gpt4 book ai didi

java - PostgreSQL 错误更新表上的 ResultSet "No primary key found for table"w/key

转载 作者:行者123 更新时间:2023-11-29 14:10:11 24 4
gpt4 key购买 nike

我正在尝试对 ResultSet 进行更新,但出现异常,No primary key found for table nvp, on a table that has a primary key.

它是 PostgreSQL 9.6.1.0,jdbc 驱动程序版本是从他们的网站下载的 postgresql-9.4.1212.jar(JDBC42 Postgresql 驱动程序,版本 9.4.1212 来自 here)。

@Test
public void testUpdateableResultSet() throws Exception {
String url = "jdbc:postgresql://localhost:5432/dot";
Properties props = new Properties();
props.setProperty("user", "dot_test");
props.setProperty("password", "test_dot");
props.setProperty("currentSchema", "dot_test");

try(Connection conn = DriverManager.getConnection(url, props)) {
conn.setAutoCommit(false);
try(Statement s = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE)) {
s.execute("drop table if exists nvp");
s.execute("create table nvp (id int primary key, value text);");
s.execute("insert into nvp (id, value) values (1, 'one_'), (2, 'two_')");
}

try(PreparedStatement ps = conn.prepareStatement("select value from nvp",
ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = ps.executeQuery()) {
while(rs.next()) {
String s = rs.getString(1);
if(s.endsWith("_")) {
s = s.replace("_", "");
}
else {
s = s + "_";
}
rs.updateString(1, s); // line 28
System.out.println("row updated");
}
}
}
}

结果如下。

Testcase: testUpdateableResultSet(com.tekbot.lib.sql.SimpleTest):   Caused an ERROR
No primary key found for table nvp.
org.postgresql.util.PSQLException: No primary key found for table nvp.
at org.postgresql.jdbc.PgResultSet.isUpdateable(PgResultSet.java:1586)
at org.postgresql.jdbc.PgResultSet.checkUpdateable(PgResultSet.java:2722)
at org.postgresql.jdbc.PgResultSet.updateValue(PgResultSet.java:3056)
at org.postgresql.jdbc.PgResultSet.updateString(PgResultSet.java:1393)
at com.tekbot.lib.sql.SimpleTest.testUpdateableResultSet(SimpleTest.java:28)

这是一个错误吗?我错过了一步吗?

最佳答案

The primary key must be specified so that the result set is updateable

将您在第 17 行 上的查询更改为:

PreparedStatement ps = conn.prepareStatement("select id, value from nvp"...

关于java - PostgreSQL 错误更新表上的 ResultSet "No primary key found for table"w/key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41052361/

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