我爲可滾動的結果集寫了一個簡單的代碼,但它給了我一個錯誤。結果集更新錯誤
代碼:
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更新數據提示錯誤。 任何人都可以讓我知道這是什麼原因嗎?
你不能通過修改結果集來做你想要做的事情。你需要執行一個SQL查詢來修改數據。 –
@GiovanniBotta Resultset'可以用來修改數據,但前提是ResultSet對於可更新結果集的敏感性,併發性和其他數據源需求已經滿載了。 –
我不知道。我想你永遠都不會停止學習。謝謝!無論如何,根據@Pyranja的迴應,我不會信任JDBC驅動程序來做這種事情。編寫自定義查詢並不困難。其實我甚至不會使用它,但JPA。 –