Failed on insert row using CachedRowSet

对着背影说爱祢 提交于 2020-01-15 04:56:06

问题


I am using CachedRowSetImpl , I can get data from Database , BUT I can not insert .

this is the Code :

public class NewClass {

    static final String DATABASE_URL = "jdbc:derby://localhost:1527/TaskDB;create=true";
    static final String USERNAME = "user";
    static final String PASSWORD = "user";

    public static void main (String [] agr) throws SQLException
    {
        CachedRowSetImpl rs = new CachedRowSetImpl();
        rs.setUrl(DATABASE_URL);
        rs.setUsername(USERNAME);
        rs.setPassword(PASSWORD);

        rs.setCommand("SELECT * FROM TASKTABLE");
        rs.execute();

        rs.moveToInsertRow();
        rs.updateString("Column_Name","DataString");

        rs.insertRow();
        rs.moveToCurrentRow();
        rs.updateRow();
    }

}

it throw the Exception :

Exception in thread "main" java.sql.SQLException: Failed on insert row at com.sun.rowset.CachedRowSetImpl.insertRow(CachedRowSetImpl.java:5462) at NewClass.main(NewClass.java:32)

I have tried JdbcRowSetImpl Instead of CachedRowSetImpl , and it's work fine

UPDATE : I used this code to catch more Details about the Exceptions :

    catch(SQLException e) {
     do {
        System.out.println("SQLState:" + e.getSQLState());
        System.out.println("Error Code:" + e.getErrorCode());
        System.out.println("Message:" + e.getMessage());
        Throwable t = e.getCause();
        while(t != null) {
            System.out.println("Cause:" + t);
            t = t.getCause();
        }
        e = e.getNextException();
    } while (e != null);
}

and the Output is :

SQLState:null

Error Code:0

Message:Failed on insert row


回答1:


I met the same issue as you , but I put acceptChanges(cnnt) at the end where the exception thrown out, as API stated.

....

cachedSet.moveToInsertRow();
cachedSet.updateInt(1,12);
cachedSet.updateString(2,"Joe");
cachedSet.updateString(3,"abcde");
cachedSet.insertRow();
cachedSet.moveToCurrentRow();
cachedSet.acceptChanges(cnnt);

.....

If I ran it without the last row(acceptChanges(cnnt)), no exceptions are thrown out,but the DB is not updated.

If I ran it with the last one, there will be an exception the same as that you got. I checked , and got the document of acceptChanges() from API:

SQLException - if the cursor is on the insert row 

I checked the document of insertRow():

SQLException - if a database access error occurs; the result set concurrency is CONCUR_READ_ONLY, this method is called on a closed result set, if this method is called when the cursor is not on the insert row, or if not all of non-nullable columns in the insert row have been given a non-null value

Maybe you can get something from it.




回答2:


You need to call rs.acceptChanges(); instead of rs.updateRow();

try to replace rs.updateRow(); with the following:

try{
   jrs.acceptChanges();
}catch(SyncProviderException spe){
  //Conflict handling code.
}


来源:https://stackoverflow.com/questions/11596022/failed-on-insert-row-using-cachedrowset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!