2012-03-04 1 views
2

DB로 Postgres를 사용하여 빠른 JDBC 응용 프로그램을 작성하려고하는데 흥미로운 문제가 발생했습니다.JDBC ResultSet :: RefreshRow가 계단식 업데이트와 함께 작동하지 않습니다.

현재 테이블 1과 테이블 2의 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, 
     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 
) 

나는 SELECT*을 보내고 내 쿼리에서 ResultSet에 들고입니다. 각 테이블에는 간단한 값의 1 행이 있으며, 그 행의 내용은 중요하지 않습니다.

내 문장은 ResultSet.TYPE_SCROLL_INSENSITIVEResultSet.CONCUR_UPDATE 플래그로 생성됩니다. SCROLL_SENSITIVE도 시도했지만.

나는 ResultSet rs/rs2가 표/표 2는 각각 유효하고 점이다 assumgin은 다음 (시도 할 경우.

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 문을 다시 실행하는 것 같아요 .PGSQL 또는 pgadmin3 실행하여 테이블을 보면 그들은 값을 것 같다 그래서 refreshRow()가 최신 정보를 가져 오지 않는 것 같습니다. 이유가 무엇인지 알 수 있습니까?

내가 사용 :

  • 자바 1.6_29
  • PostgreSQL을-9.1-901.jdbc4.jar 어떤 도움을 주시면 감사하겠습니다

.

답변

2

나는 당신이 오래된 질문하지만, 단지 기록을 위해 내가 여기에 내가 시도하고 나를 위해 어떤 작업하는 조각을 게시 이후가 정리 얻었기를 바랍니다 :

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