Java 如何将一行插入到一个表中,使其具有带有CachedRowSet的增量id?
我试图在一个表中插入一行,该表的增量id为CachedRowSet(我使用的是Java wit Java DB),但得到了以下SQLException:Java 如何将一行插入到一个表中,使其具有带有CachedRowSet的增量id?,java,javadb,cachedrowset,Java,Javadb,Cachedrowset,我试图在一个表中插入一行,该表的增量id为CachedRowSet(我使用的是Java wit Java DB),但得到了以下SQLException: java.sql.SQLException:插入行失败 在 SQLState:null 错误代码:0 消息:插入行失败 下面是我的代码片段: private static String urlString = "jdbc:derby:testdb;create=true"; private static String userName = "t
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.");
这是一个复制品
不幸的是,这是因为:
抛出:
SQLException-如果发生数据库访问错误;结果集并发性为CONCUR_READ_ONLY,如果在光标不在插入行上时调用此方法,或如果插入行中并非所有不可为null的列都被赋予非null值,则在关闭的结果集上调用此方法
您最好使用JdbcRowSetImpl
如果删除
id
列的notnull
约束,则只能使用CachedRowSet
。不幸的是,这也意味着要删除PK约束。我认为这是一个太大的牺牲,但不是这样,就是没有缓存集
crs.updateNull(1);
crs.insertRow()代码>
其中1是你的主键。在MySQL中为我搜罗我在发帖之前已经检查了这个问题,似乎它真的没有“完美”的解决方案。嗯,有时候你不能有“完美”。)我有另一个想法,但我想它更不完美(