以下代码显示错误: ResultSet关闭后不允许操作 我还没有关闭任何结果集仍然显示错误。我在其他ResultSet中使用一个ResultSet,这是否会导致问题??
int n=li10.getSelectedIndex();
final String n1=(String) dl10.elementAt(n);
String n2=t52.getText();
String n3="Select Budget,Count1 FROM User Where U_Name=' "+n2+"'";
String n4="Select Price From Player_Attributes Where Player_Name='"+n1+"'";
try{
Connection con=DriverManager.getConnection("jdbc:mysql://127.0.0.1/BFPL","root","ilovepepsi");
Statement st=con.createStatement();
ResultSet rs1=st.executeQuery(n4);
ResultSet rs=st.executeQuery(n3);
while(rs1.next()){
int pr=rs1.getInt("Price");
while(rs.next()){
int f=rs.getInt("Budget");
int f1=rs.getInt("Count1");
if(f1>0 && pr<f ){
try{
Connection con5=DriverManager.getConnection("jdbc:mysql://127.0.0.1/BFPL","root","ilovepepsi");
PreparedStatement st2=con5.prepareStatement("Update User_Team Set Player"+f1+"=? Where User_Name='"+n2+"'");
st2.setString(1,n1);
st2.executeUpdate();
JOptionPane.showMessageDialog(p13,"Table is updated");
}
catch(SQLException ae){
System.out.println("SQLException:"+ae.getMessage());
System.out.println("SQLState:"+ae.getSQLState());
System.out.println("VendorError:"+ae.getErrorCode());
JOptionPane.showMessageDialog(p13,"Error in submitting data!");
}
f1=f1-1;
f=f-pr;
try{
Connection con4=DriverManager.getConnection("jdbc:mysql://127.0.0.1/BFPL","root","ilovepepsi");
PreparedStatement st1=con4.prepareStatement("Update User Set Budget=? Count1=? Where U_Name='"+n2+"'");
st1.setInt(1,f);
st1.setInt(2,f1);
st1.executeUpdate();
con4.close();
JOptionPane.showMessageDialog(p13,"Data is successfully inserted in database");
}
catch(SQLException ae){
System.out.println("SQLException:"+ae.getMessage());
System.out.println("SQLState:"+ae.getSQLState());
System.out.println("VendorError:"+ae.getErrorCode());
JOptionPane.showMessageDialog(p13,"Error in submitting data!");
}
}
else{
JOptionPane.showMessageDialog(p13,"Either your budget is not sufficient or you have reached maximum limit of buying players");
}
答案 0 :(得分:2)
每个语句只能打开一个结果集。因此,如果您在同一个语句中打开另一个语句,那么任何先前打开的语句都将被隐式关闭。如果你仍然试图访问它,那么将抛出这个异常。
您的问题基本上有两种解决方案:
在同一个连接上创建2个语句,让它们分别打开一个结果集。
将2个松散的SQL查询重写并合并到单个SQL查询中,使完全所需的结果,这样您就不需要创建多个语句。 SQL JOIN clause可能会派上用场。
无关,您的代码中存在另一个主要问题。它正在泄漏数据库资源。您没有明确关闭所有到目前为止打开的连接,语句和结果集,甚至没有关闭finally
块。另请参阅How often should Connection, Statement and ResultSet be closed in JDBC?。