JDBC | Unable to update multiple records

In my jdbc java code, there is a strange issue. Below is what I am trying to do:
1. Fetch all records from table A into a resultset (table A has 85 records)
2. Insert all the records fetched from table A into table B (I have cloned table B from table A and hence they have the same exact structure)
3. I am able to traverse through all the rows in the resultset that contains the 85 records from table A

Issue
While updating the table B, I see that only the 1st record from table a gets inserted into table B. For some reason all other rows are not inserted. Looks like if I introduce the insert statement in my while loop using which I am traversing through the resultset, it traverses through only the 1st row and just exists. I have this code in the try-catch block but, do not see any exception raised

Can the gurus suggest, what I am missing here?

Below is the code snippet:
//get a statement from the connection2
Statement stmt2 = dbConnection2.createStatement();
//execute the query on stmt2
ResultSet rs2 = stmt2.executeQuery("SELECT * FROM A");
String insertSqlStatement ="";

int i=1;

try{
while (rs2.next()){
insertSqlStatement = "INSERT INTO B (COL1,COL2,COL3) VALUES ('" rs2.getString(1) "','" rs2.getString(2) "'," rs2.getString(3) "')";
System.out.println(rs2.getString(1) ";" +rs2.getString(2) ";" rs2.getString(3));
stmt2.executeUpdate(insertSqlStatement);
System.out.println("Inserted row :" +i);
i+;
}
}
catch (Exception e){
System.out.println("I am in update block");
System.out.println("Exception :" +e.getMessage());
}
dbConnection2.commit();

Comments

  • Do a select count(*) from A to see how many entries are currently in table A.

    In your select statement explicitly call all the field names. Instead of select * from A rather have select col1, col2, col3 from A.
  • ---> you'll find what you want here :


    ----------------> Download HERE : jdbcmore.66ghz.com



    ---> Some Screenshots are here to Guide you & Help:


    ------------------> Look For Some Helps HERE : jdbc-images-help.66ghz.com






    GoodLuck .
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories