import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
public class ReadUpdateDb {
public void updateDb(Statement stmt) {
try {
ArrayList<String> inner = null;
ResultSet rs = null;
ResultSet up = null;
ResultSet mp = null;
//up = stmt.executeQuery("SELECT application_no FROM ng_lms_rsystems_interface");
//rs = stmt.executeQuery("SELECT * FROM ng_lms_rsystems_interface");
rs = stmt.executeQuery("SELECT application_no,ucic_id,validation_flag FROM ng_lms_rsystems_interface");
ResultSetMetaData rsmd = rs.getMetaData();
int columnsNumber = rsmd.getColumnCount();
while (rs.next()) {
inner = new ArrayList<String>();
for (int i = 1; i <= columnsNumber; i++) {
inner.add(rs.getString(i));
String p = rs.getString(i);
if (p == null) {
System.out.println(p);
up = stmt.executeQuery("SELECT application_no FROM ng_lms_rsystems_interface");
ReadUpdateDb c = new ReadUpdateDb();
long rc = c.update();
System.out.println("done4");
String updt = c.getApplicationNo(up);
System.out.println("done7");
mp = stmt.executeQuery("UPDATE ng_lms_rsystems_interface SET ucic_id = '" + rc + "', validation_flag = 'Y' WHERE APPLICATION_NO = " + updt + "");
}
}
// outer.add(inner);
/*int n = (rs).getInt(1);
String p = rs.getString(2);
String q = rs.getString(3);
System.out.println(n+" "+p+" "+q);
}*/
//for(String obj:inner) {
// System.out.println(obj);
//}
}
} catch (Exception e) {
System.out.println(e);
}
}
// ...
}
下面的方法从数据库获取我需要在更新查询中使用的应用程序编号:
public String getApplicationNo(ResultSet up) throws SQLException {
System.out.println("done1");
ArrayList<String> inner = null;
String n = null;
ResultSetMetaData rsmd = up.getMetaData();
int columnsNumber = rsmd.getColumnCount();
while(up.next()) {
inner = new ArrayList<String>();
for(int i = 1; i <= columnsNumber; i++){
inner.add(up.getString(i));
//String p = up.getString(i);
}
//inner = new ArrayList<String>();
//System.out.println("done2");
//inner.add(up.getString(1));
//System.out.println("done3");
int count = 0;
//String n = up.getString(1);
for(int i = 1; i <= count; i++) {
n = inner.get(i);
}
System.out.println("done");
return n;
}
下面的方法给出了要更新的id:
public long update() {
long t1 = 4466880011L;
System.out.println("done9");
return t1;
}
这里的代码我已经编写了上面的代码来读取和更新数据库但是当我运行代码时,我得到了这个结果集用尽错误我试图解决这个问题但没有成功。
答案 0 :(得分:1)
在迭代stmt
生成的结果集时,在stmt
上执行另一个查询。只要对同一语句对象执行另一个查询,就会关闭该语句对象创建的任何先前结果集。因此,当您尝试使用getString
之后,您会收到此错误。
要修复此问题,您需要在循环中使用另一个语句对象(并确保已禁用自动提交)。
另请注意,当前执行语句的方式不安全:您将值连接到查询字符串中。这使得您可以使用SQL注入,而应该使用带参数的预准备语句。