首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ResultSet关闭供应商错误后不允许操作:0

问ResultSet关闭供应商错误后不允许操作:0
EN

Stack Overflow用户
提问于 2013-01-07 23:31:21
回答 1查看 287关注 0票数 0

下面的代码显示错误:操作不允许,在ResultSet关闭之后,我还没有关闭任何结果集,它仍然显示错误。我在另一个ResultSet中使用一个ResultSet,这会引起问题吗??

代码语言:javascript
复制
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");

    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-08 00:37:54

每个语句只能打开一个结果集。因此,如果在同一语句上打开另一个语句,那么以前打开的任何语句都将隐式关闭。如果您仍然试图访问它,那么这个异常将被抛出。

对于您的问题,基本上有两种解决方案:

  1. 在相同的连接上创建两个语句,并让每个语句打开一个结果集。
  2. 重写两个松散SQL查询并将其合并到一个返回所需结果的SQL查询中,这样就不需要创建多个语句。SQL联接子句可能会在这方面派上用场。

与具体问题无关的,您的代码中还有另一个主要问题。它正在泄露DB资源。到目前为止,您还没有显式关闭所有打开的连接、语句和结果集,甚至在finally块中也没有。另见在JDBC中,连接、语句和ResultSet应该多久关闭一次?。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14205922

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档