dafodil
dafodil

Reputation: 513

inserting only one row of each loop in jdbc using java

I have a two tables, where i need to loop for each client with specific batches and update to another table. below is my code

client Table
----------
client_id  
1
2
3

batch_meta_data_id Table
------------
client_id batch_id
1           12
1           13
2           14
2           15
2           16
3           17



Statement stmt = null;
stmt = conn.createStatement();
 sql = "SELECT client_id FROM client";
      ResultSet rs1 = stmt.executeQuery(sql);   
      while(rs1.next()){  
          if(rs1.next()){
              stmt = conn.createStatement();

              sql = "SELECT batch_meta_data_id FROM batchmetadata WHERE client_id = "+rs1.getInt("client_id");
              ResultSet batchSql = stmt.executeQuery(sql);  

              if(batchSql.next()){
                  stmt = conn.createStatement();
                  String sql1 = "INSERT INTO METRICS (client_id, batch_id, count)"
                           + "VALUES ( '"+rs1.getInt("client_id")+"','"+batchSql.getInt("batch_meta_data_id")+"',(SELECT count(*) FROM typist  where  client_id = '"+rs1.getInt("client_id")+"' and batch_meta_data_id = '"+batchSql.getInt("batch_meta_data_id")+"'))";
                  stmt.executeUpdate(sql1);     
              }
              }
      }

Here its inserting only first batch of each client,other batches of client are not getting looped. How could i solve this?

Upvotes: 1

Views: 135

Answers (1)

Scary Wombat
Scary Wombat

Reputation: 44834

In this code

while(rs1.next()){  
      if(rs1.next()){

You are incrementing twice

see

https://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html#next()

Upvotes: 4

Related Questions