I am trying to make TABLE COMPB equivalent to TABLE COMPA by Update and Delete operations on Table COMPB based on Table COMPA. The code works perfectly fine while generating proper SQL. Problem is, the moment I put execute statement as below, while loop terminates in first iteration itself. Not able to understand this behavior of ResultSet in java. Any help here? Where I am going wrong?
//Comparision between Minus Table(seta) and COMPB (setc)
//ID Contains Primary Key of COMPB Table
ResultSet seta=stmt.executeQuery("select * from COMPA minus select * from COMPB");
while(seta.next())
{
String insert="";
String update="";
boolean contains=ArrayUtils.contains(ID, seta.getInt(1));
if (contains==true)
{
update="Update COMPB SET COMPB.EMPNAME='"+seta.getString(2)+"',COMPB.EMAILID='"+seta.getString(3)+"' where "+seta.getInt(1)+"=COMPB.EMPID";
stmt.executeUpdate(update);
System.out.println(update);
}
else
{
insert="Insert INTO COMPB values ("+seta.getInt(1)+" , '"+seta.getString(2)+"' , '"+seta.getString(3)+"')";
stmt.executeUpdate(insert);
System.out.println(insert);
}
}
There can be two possible reasons for this:
stmtobject : Result set object is mapped to statement that returned it (viaexecuteQuerymethod), and re-using the same statement object in the middle of iteration will make resultset invalid. You should create a new statement object and execute those queries with it.CONCUR_READ_ONLYresultSet(default) : Here's Oracle's documentation on result set. By default, it is not modifiable. You may need to useCONCUR_UPDATABLEresultset if records need modifying. Here is an example of that.