下面的代码迭代while循环,并检索所有匹配的记录:
ResultSet rs3 = st.executeQuery("select id from fbprofiles where age<=(select age from fbprofiles where id="+user+") and id not in("+user+")");
String id;
query = "select id from fbprofiles where age<=(select age from fbprofiles where id="+user+") and id not in("+user+")";
System.out.println(query);
//ResultSet rs4;
while(rs3.next())
{
st = con.createStatement();
id = rs3.getString(1);
System.out.println("id: "+id);
/*query = "select name from fbpages where name in(select name from pagelikes_"+id+" where name not in('"+pagesLiked.toString()+"')) and category in("+category.toString()+")";
System.out.println(query);
rs4 = st.executeQuery(query);
while(rs4.next())
{
message += "<a href="+rs4.getString(1)+".html style=\"color:black\">"+rs4.getString(1)+"</a><br>";
pagesLiked.append(rs4.getString(1));
}*/
}
输出:
编号:2
编号:3
但是,当我删除注释时,循环仅循环一次,结果集仅包含1条记录,而不是2条记录。
我对所有查询都使用了不同和相同的结果集,这也给出了只有1条记录的相同输出。
当我检入SQL数据库(11g)后,所有查询均在5n正常工作。
当您在同一条语句上执行下一个查询时,您将关闭rs。
public static void main(String[] args) throws SQLException, Exception {
Connection con = ConnectionDefinition.getOracleConnection(); //my oracle connection
String q1 = "select object_name from user_objects";
PreparedStatement ps = con.prepareCall(q1);
ResultSet rs = ps.executeQuery();
while(rs.next()){
ResultSet rs2 = ps.executeQuery(q1);
if(rs.isClosed()){
System.err.println("FRIST RS IS CLOSED");
}
}
con.close();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句