2013-01-07 49 views
0

以下代碼顯示錯誤:ResultSet關閉後不允許操作 我還沒有關閉任何結果集,但它仍顯示錯誤。我在其他ResultSet中使用一個ResultSet,是否會導致問題?ResultSet關閉後不允許操作VendorError:0

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"); 

    } 

回答

2

每個語句只能打開一個結果集。所以如果你在同一個陳述中打開另一個,那麼以前打開的任何一個都會被隱式關閉。如果你仍然嘗試訪問它,那麼正是這個異常將被拋出。

基本上有2個解決方案,您的問題:

  1. 在同一連接上創建2個報表,讓他們每開一個結果集。

  2. 重寫和合並2個鬆散SQL查詢到一個單一的SQL查詢返回正是預期的效果,讓您無需創建多個報表。 SQL JOIN clause可能會派上用場。


無關到具體的問題,你在你的代碼已經另一個主要問題。它泄漏了數據庫資源。您並未明確關閉全部到目前爲止已斷開的連接,語句和結果集甚至不在finally區塊中。另見How often should Connection, Statement and ResultSet be closed in JDBC?

相關問題