I have run into a couple of problems while trying to convert an existing JDBC application to use HSQLDB version 2.2.9 (Currently the codebase runs successfully on MySQL, ORACLE and SQLServer, but an embedded database seemed like a good option too).
I will ask the questions one at a time and separately (although they all relate to the JDBC ResultSet.deleteRow()
method, supported since HSQLDB 2.0)
Why does rs.next()
return false after calling rs.deleteRow()
?
Here is a complete self contained code sample (including simple table creation, sample inserts, and deleting the table at the end):
int deletedRows=0;
try{
Connection c = DriverManager.getConnection("jdbc:hsqldb:mem:mytestdb",
"SA", "");
String createSQL =
"create table test (num INTEGER PRIMARY KEY, str VARCHAR(25))";
Statement createStmt = c.createStatement();
createStmt.execute(createSQL);
createStmt.close();
String ins = "insert into test (num,str) values (?,?)";
PreparedStatement pStmt = c.prepareStatement(ins);
for(int i=0; i<100; i++){
pStmt.setInt(1, i);
pStmt.setString(2, "String"+i);
pStmt.execute();
}
// there should now be 100 rows in the table
String select = "SELECT * FROM test";
PreparedStatement stmt = c.prepareStatement(select,
ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery();
rs.beforeFirst();
while(rs.next()){
int num = rs.getInt("num");
if((num%7)==0){
System.out.println("Deleting row:"+num);
rs.deleteRow();
deletedRows++;
}
}
Statement dropStmt = c.createStatement();
dropStmt.execute("drop table test;");
dropStmt.close();
} catch (SQLException sqle) {
System.out.println("Deleted "+deletedRows+
" rows before exception: "+sqle.getMessage());
sqle.printStackTrace();
}
When running the same code on MySQL database, the output shows that every 7th row is deleted:
Deleting row:0
Deleting row:7
...
Deleting row: 98
On HSQLDB, the output is:
Deleting row:0
The rs.next()
returns false after the first call to rs.deleteRow()
.
I cannot find any information in the HSQLDB javadoc. Does anyone have any ideas?