我有两个表,我需要为每个客户端循环使用特定批次并更新到另一个表。下面是我的代码
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);
}
}
}
这里只插入每个客户端的第一批,其他批次的客户端没有得到循环。 我怎么能解决这个问题?
答案 0 :(得分:4)
在此代码中
while(rs1.next()){
if(rs1.next()){
你正在增加两次
见
https://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html#next()