0

以下のコードはエラーを示しています: ResultSet が閉じられた後、操作は許可され ていません。まだエラーが表示されている結果セットを閉じていません。1 つの 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");

    }
4

1 に答える 1

2

ステートメントごとに開くことができる結果セットは 1 つだけです。したがって、同じステートメントで別のものを開くと、以前に開いたものは暗黙的に閉じられます。それでもアクセスしようとすると、まさにこの例外がスローされます。

あなたの問題には基本的に2つの解決策があります:

  1. 同じ接続で 2 つのステートメントを作成し、それぞれが 1 つの結果セットを開くようにします。

  2. 複数のステートメントを作成する必要がないように、2 つの緩やかな SQL クエリを書き直してマージし、目的の結果を正確に返す 1 つの SQL クエリにします。これには、 SQL JOIN 句が役立つ場合があります。


具体的な問題とは関係なく、コードに別の大きな問題があります。DBリソースがリークしています。これまでに開いたすべての接続、ステートメント、および結果セットを明示的に閉じているわけではなく、finallyブロック内にさえありません。JDBC で Connection、Statement、および ResultSet を閉じる頻度はどれくらいですか?も参照してください。.

于 2013-01-08T00:37:54.773 に答える