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);
}
}
As per suggestions below, I changed the loop by creating preparedstatement objects.