CachedRowSet 在 H2 中更新一条记录
CachedRowSet update a record in H2
我有一个 H2 数据库 table
CREATE TABLE income_expense
(
amount VARCHAR(255) NOT NULL,
name VARCHAR(255) NOT NULL,
ondate VARCHAR(255) NOT NULL
);
和一些随机数据,例如 INSERT INTO income_expense VALUES ('10','Something','2015-04-15');
然后我使用 JDBC 连接到此数据库并尝试通过 CachedRowSet 执行 UPDATE :
public void doUpdate()
{
try
{
Class.forName("org.h2.Driver");
setConnection(
DriverManager.getConnection("jdbc:h2:~/thisdb", "sa", ""));
CachedRowSet crs2 = new CachedRowSetImpl();
crs2.setType(CachedRowSet.TYPE_SCROLL_INSENSITIVE);
crs2.setConcurrency(CachedRowSet.CONCUR_UPDATABLE);
crs2.setCommand(
"SELECT amount, name, ondate FROM income_expense");
crs2.execute(getConnection());
crs2.absolute(1);
crs2.updateString("amount", "22");
crs2.updateString("name" , "33");
crs2.updateString("ondate", "44");
crs2.updateRow();
crs2.acceptChanges();
}
catch (ClassNotFoundException | SQLException e)
{
System.out.println("Error occured." + e);
}
}
此更新失败并显示消息 javax.sql.rowset.spi.SyncProviderException:同步时发生 1 次冲突。
我更新记录做错了什么?
嗯... CachedRowSet
UPDATE 仅在我将 PRIMARY KEY 添加到 table 后才起作用:
ALTER TABLE income_expenses ADD COLUMN id INT NOT NULL AUTO_INCREMENT;
ALTER TABLE income_expenses ADD CONSTRAINT PRIMARY KEY (id);
和指定的列数,它们构成了唯一标识一行的键,并将该列包含在 SELECT
:
//First column from a command will be the key
crs2.setKeyColumns(new int[]{1});
crs2.setCommand("SELECT id, amount, name, ondate FROM income_expense");
我有一个 H2 数据库 table
CREATE TABLE income_expense
(
amount VARCHAR(255) NOT NULL,
name VARCHAR(255) NOT NULL,
ondate VARCHAR(255) NOT NULL
);
和一些随机数据,例如 INSERT INTO income_expense VALUES ('10','Something','2015-04-15');
然后我使用 JDBC 连接到此数据库并尝试通过 CachedRowSet 执行 UPDATE :
public void doUpdate()
{
try
{
Class.forName("org.h2.Driver");
setConnection(
DriverManager.getConnection("jdbc:h2:~/thisdb", "sa", ""));
CachedRowSet crs2 = new CachedRowSetImpl();
crs2.setType(CachedRowSet.TYPE_SCROLL_INSENSITIVE);
crs2.setConcurrency(CachedRowSet.CONCUR_UPDATABLE);
crs2.setCommand(
"SELECT amount, name, ondate FROM income_expense");
crs2.execute(getConnection());
crs2.absolute(1);
crs2.updateString("amount", "22");
crs2.updateString("name" , "33");
crs2.updateString("ondate", "44");
crs2.updateRow();
crs2.acceptChanges();
}
catch (ClassNotFoundException | SQLException e)
{
System.out.println("Error occured." + e);
}
}
此更新失败并显示消息 javax.sql.rowset.spi.SyncProviderException:同步时发生 1 次冲突。
我更新记录做错了什么?
嗯... CachedRowSet
UPDATE 仅在我将 PRIMARY KEY 添加到 table 后才起作用:
ALTER TABLE income_expenses ADD COLUMN id INT NOT NULL AUTO_INCREMENT;
ALTER TABLE income_expenses ADD CONSTRAINT PRIMARY KEY (id);
和指定的列数,它们构成了唯一标识一行的键,并将该列包含在 SELECT
:
//First column from a command will be the key
crs2.setKeyColumns(new int[]{1});
crs2.setCommand("SELECT id, amount, name, ondate FROM income_expense");