2014-01-28 62 views
2

我爲可滾動的結果集寫了一個簡單的代碼,但它給了我一個錯誤。結果集更新錯誤

代碼:

import java.sql.*; 
import java.util.Properties; 

public class Scrollable_Resultset { 

public static void main(String[] args) throws Exception{ 

    Properties p = new Properties(); 
    p.put("user", "system"); 
    p.put("password", "password"); 

    DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); 
    Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe",p); 

    Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); 

    ResultSet rs = stmt.executeQuery("select * from EMPLOYEE"); 

    System.out.println("Displaying all the rows of the result set forward"); 
    while(rs.next()) 
    { 
     System.out.println(rs.getInt(1)); 
     System.out.println(rs.getString(2)); 
     System.out.println(rs.getInt(3)); 
     System.out.println("--------------"); 
    }//end of while 
    System.out.println("****************"); 

    System.out.println("Printing resultset in reverse order"); 
    rs.last();//go to last row 
    while(rs.previous()) 
    { 
     System.out.println(rs.getInt(1)); 
     System.out.println(rs.getString(2)); 
     System.out.println(rs.getInt(3)); 
     System.out.println("--------------"); 
    }//end of while 
    System.out.println("****************"); 

    System.out.println("Printing only 1st row"); 
    rs.first(); 
    System.out.println(rs.getInt(1)); 
    System.out.println(rs.getString(2)); 
    System.out.println(rs.getInt(3)); 
    System.out.println("--------------"); 
    System.out.println("****************"); 

    System.out.println("Printing 3rd row of original data set"); 
    System.out.println(rs.getInt(1)); 
    System.out.println(rs.getString(2)); 
    System.out.println(rs.getInt(3)); 
    System.out.println("--------------"); 
    System.out.println("****************"); 

    System.out.print("No of rows before modification: "); 
    rs.last(); 
    System.out.println(rs.getRow()); 
    System.out.println("****************"); 

    System.out.println("Updating 3rd row"); 
    rs.absolute(3); 
    rs.updateInt(1, 999); 
    rs.updateString(2,"John"); 
    rs.updateInt(3, 99000); 
    rs.updateRow(); 
    System.out.println("****************"); 

    System.out.println("Inserting a new row"); 
    rs.moveToInsertRow(); 
    rs.updateInt(1, 99); 
    rs.updateString(2,"Michael"); 
    rs.updateInt(3, 909); 
    rs.insertRow(); 
    System.out.println("Deleting the 6th row"); 
    rs.absolute(6); 
    rs.deleteRow(); 
    System.out.println("****************"); 
    con.close(); 
} 

}

錯誤

Exception in thread "main" java.sql.SQLException: Invalid operation for read only resultset: updateInt 
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java) 
at oracle.jdbc.driver.BaseResultSet.updateInt(BaseResultSet.java) 
at jdbc.Scrollable_Resultset.main(Scrollable_Resultset.java:64) 

當我嘗試雖然我已經使用ResultSet.CONCUR_UPDATABLE更新數據提示錯誤。 任何人都可以讓我知道這是什麼原因嗎?

+0

你不能通過修改結果集來做你想要做的事情。你需要執行一個SQL查詢來修改數據。 –

+0

@GiovanniBotta Resultset'可以用來修改數據,但前提是ResultSet對於可更新結果集的敏感性,併發性和其他數據源需求已經滿載了。 –

+0

我不知道。我想你永遠都不會停止學習。謝謝!無論如何,根據@Pyranja的迴應,我不會信任JDBC驅動程序來做這種事情。編寫自定義查詢並不困難。其實我甚至不會使用它,但JPA。 –

回答

6

原因是您正在使用Select查詢,並試圖進行Update操作

ResultSet rs = stmt.executeQuery("select * from EMPLOYEE"); 

,另一點是「SELECT *」使得結果集只讀

如果你想執行更新操作,則需要指定中的列選擇查詢

+0

對我來說是+1問題出在「*」 – venergiac

+0

accepted.got it !!紐約州簡單爆炸 – Vivek

0

包括下列變化

ResultSet rs = stmt.executeQuery("select EMPLOYEE.* from EMPLOYEE"); 
0

有幾種可能性:

  • 壞司機:確保適當的驅動程序
  • 的 「SELECT * FROM EMPLOYEE」 使用wildchar和Oracle不知道列的確切順序,請指定列的確切列表和ROWID

    SELECT ROWID,...從ENPLOYEEË

2

Oracle JDBC documentation

如果給定查詢不適合請求的ResultSet類型,Oracle JDBC驅動程序將嘗試降級返回的ResultSet。在你的情況下,使用通配符*可能會阻止創建可更新的ResultSet(還有更多限制,請檢查文檔)。要驗證原因,您應該使用ResultSet的getWarnings()方法。如果必須降級,Oracle JDBC驅動程序應該設置警告。

建議的解決方法是使用一個表的別名,如:

SELECT t.* FROM employee t 

注意:如果你打算使用這種機制在(關鍵)應用程序,請務必仔細研究文檔。例如,通過ResultSets靜默更新會忽略衝突。從這個角度來看,如果在JDBC實現中存在更多的陷阱,我不會感到驚訝。