Java JDBC ResultSet::RefreshRow无法使用级联更新
我正在尝试使用Postgres作为DB来创建一个快速的JDBC应用程序,遇到了一个有趣的问题 我目前有两个表,表1和表2Java JDBC ResultSet::RefreshRow无法使用级联更新,java,postgresql,jdbc,resultset,Java,Postgresql,Jdbc,Resultset,我正在尝试使用Postgres作为DB来创建一个快速的JDBC应用程序,遇到了一个有趣的问题 我目前有两个表,表1和表2 CREATE TABLE table1 ( a character varying NOT NULL, b integer NOT NULL, CONSTRAINT table1_pkey PRIMARY KEY (b) ) CREATE TABLE table2 ( c character varying NOT NULL,
CREATE TABLE table1
(
a character varying NOT NULL,
b integer NOT NULL,
CONSTRAINT table1_pkey PRIMARY KEY (b)
)
CREATE TABLE table2
(
c character varying NOT NULL,
d integer,
CONSTRAINT table2_pkey PRIMARY KEY (c),
CONSTRAINT table2_d_fkey FOREIGN KEY (d),
REFERENCES table1(b) MATCH SIMPLE
ON UPDATE CSCADE ON DELETE CASCADE
)
作为我程序的后端,我正在选择ing*
,并从我的查询中保留结果集。每个表中有一行简单的值,它们是什么似乎无关紧要
我的语句是用标志ResultSet.TYPE\u SCROLL\u INSENSITIVE
和ResultSet.CONCUR\u UPDATE
创建的。虽然我也试过让你敏感
如果我尝试以下操作(假设结果集rs/rs2
有效并分别指向表1/2):
rs.first(); // move to the first row (only row)
rs.updateInt(2, 50); // update our primary key, which is also the cascading fk
// 50 could be any number
print(rs); // Will show the old value
rs.updateRow();
print(rs); // Will show the new value
rs2.refreshRow(); // make sure we get the latest data from table2
print(rs2); // will show the old data?
我希望看到由于级联而产生的新值。如果我退出并重新运行应用程序,不更改任何输入,那么它将打印正确的table2值。我猜这是由于重新运行SELECT
语句。如果我通过运行psql或pgadmin3查看该表,这些值似乎正在更改。因此看起来像是刷新w()没有带来最新的东西。有人知道为什么吗
我正在使用:
- java 1.6_29
- postgresql-9.1-901.jdbc4.jar
任何帮助都将不胜感激。我希望你能解决这个问题,因为这是一个老问题,但为了记录在案,我在这里发布了一个我尝试过的片段,它对我有用:
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery("SELECT * FROM table1");
Statement stmt2 = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs2 = stmt2.executeQuery("SELECT * FROM table2");
rs.first(); // move to the first row (only row)
rs.updateInt(2, 50); // update our primary key, which is also the
// cascading fk 50 could be any number
System.out.println(rs.getString(2)); // Prints the old value 12
rs.updateRow();
System.out.println(rs.getString(2)); // Prints the new value 50
rs2.first();
rs2.refreshRow(); // make sure we get the latest data from table2
System.out.println(rs2.getString(2)); // Prints the new value 50