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
You need to call rs.acceptChanges(); instead of rs.updateRow();
try to replace rs.updateRow(); with the following:
I met the same issue as you , but I put
acceptChanges(cnnt)
at the end where the exception thrown out, as API stated.....
.....
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:I checked the document of
insertRow()
:Maybe you can get something from it.