2

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

Moe
  • 21
  • 1
  • 3
  • You need to examine the entire exception. See these instructions for how to do that: http://wiki.apache.org/db-derby/UnwindExceptionChain . Please update your question with full details once you've done this. – Bryan Pendleton Jul 22 '12 at 17:33
  • Thanks @BryanPendleton :) ,I updated my question , but it's looks there is no more Details – Moe Jul 22 '12 at 21:19
  • Try doing e.printStackTrace() inside that catch() block – Bryan Pendleton Jul 23 '12 at 13:41

2 Answers2

2

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.

Jack
  • 10,943
  • 13
  • 50
  • 65
Ellery Q.
  • 21
  • 3
0

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.
}
  • Thanks @ZhangZhongyi , I tried your code , but nothing change . becuz the problem cause by " rs.insertRow(); " . – Moe Aug 19 '12 at 01:34
  • try CachedRowSet jrs=RowSetProvider.newFactory().createCachedRowSet(). It is the last difference between your code and mine except the tables. – Zhang Zhongyi Aug 20 '12 at 07:12
  • well , Thanks @ZhangZhongyi for trying to help . I appreciate that – Moe Aug 26 '12 at 22:18
  • Ops press Enter by mistake , anyway , it still does not work , but it's ok I give up :) , and I am using JdbcRowSetImpl now and it works fine . Thanks again – Moe Aug 26 '12 at 22:21