我正在尝试将一行插入到具有 CachedRowSet 增量 id 的表中(我正在使用 Java wit Java DB),但是我得到了以下 SQLException:
java.sql.SQLException:在插入行时失败
...
SQLState:null
错误代码:0
消息:插入行失败
这是我的代码片段:
private static String urlString = "jdbc:derby:testdb;create=true";
private static String userName = "testUser";
private static String password = "testPassword";
...
CachedRowSet crs = new CachedRowSetImpl();
crs.setUrl(urlString);
crs.setUsername(userName);
crs.setPassword(password);
crs.setCommand("SELECT * FROM test_table");
crs.execute();
crs.moveToInsertRow();
crs.updateString("str_value", "testValue");
crs.insertRow();
crs.moveToCurrentRow();
crs.acceptChanges();
SQLException 是从crs.inertRow()
.
如果表没有自动增量 ID,则 CachedRowSet 可以正常工作。
我怎样才能成功地做到这一点?
其他细节:
我使用以下代码来创建我的表:
conn = DriverManager.getConnection(urlString, userName, password);
Statement stmt = conn.createStatement();
stmt.execute("CREATE TABLE test_table("
+ "id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),"
+ "str_value VARCHAR(20) NOT NULL,"
+ "CONSTRAINT primary_key PRIMARY KEY (id))");
System.out.println("Talbe test_table created.");