gpt4 book ai didi

java - 使用java在jdbc中的每个循环中只插入一行

转载 作者:行者123 更新时间:2023-11-29 04:36:37 24 4
gpt4 key购买 nike

我有两个表,我需要为每个具有特定批处理的客户端循环并更新到另一个表。下面是我的代码

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);
}
}
}

这里它只插入每个客户端的第一批,其他批处理的客户端不会循环。我该如何解决这个问题?

最佳答案

在这段代码中

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

你递增了两次

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

关于java - 使用java在jdbc中的每个循环中只插入一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39868630/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com