1

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?

1
  • Shouldn't it be while(batchSql.next()) ? Commented Oct 5, 2016 at 8:14

1 Answer 1

4

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()

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.