2012-09-07 84 views
0

此代碼在數據庫中搜索書籍或用戶時,搜索用戶時沒有任何問題,但當您搜索不存在的書時,會出現此錯誤:錯誤:結果集結束後

錯誤:結果年底成立

if(RB1.isSelected()==true) 
    { 

    Statement stmt = (Statement)conn.createStatement(); 
    String SQL1 = "select * from usernames"; 
    ResultSet rs1 = stmt.executeQuery(SQL1); 

    String ID =""; 

    while(rs1.next()) 
    {   
     ID = rs1.getString("UserID"); 
     if(UIorBItf.getText().compareTo(ID) == 0) 
     { 
      JOptionPane.showMessageDialog(null,rs1.getString("Full_Name") + 
      " is available","Query result",JOptionPane.INFORMATION_MESSAGE); 
      break; 
     } 
    } 

    if(UIorBItf.getText().compareTo(ID) != 0) 
    { 
     JOptionPane.showMessageDialog(null, UIorBItf.getText() +" is 
     not available","Query result",JOptionPane.INFORMATION_MESSAGE); 
    } 

    } 
    if(RB2.isSelected()==true) 
    { 
    //JOptionPane.showMessageDialog(null, UIorBItf.getText() +" Now 
    //You are inside Book search","Query result",JOptionPane.INFORMATION_MESSAGE); 

     Statement stmt2= (Statement)conn.createStatement(); 

     String SQL2 = "select * from books"; 

     ResultSet rs2 = stmt2.executeQuery(SQL2); 
     String ID =""; 

      while(rs2.next()) 
      { 
       ID = rs2.getString("BookID"); 
       if(ID.compareTo(UIorBItf.getText()) ==0) 
       { 
       JOptionPane.showMessageDialog(null,rs2.getString("Book_Name") + 
        " is available","Query result",JOptionPane.INFORMATION_MESSAGE); 
        break;  
       } 
      } 
      if(UIorBItf.getText().compareTo(ID)!=0) 
       { 
       JOptionPane.showMessageDialog(null,rs2.getString("Book_Name") + 
        " is not available","Query result",JOptionPane.INFORMATION_MESSAGE); 
       } 
    } 
    }catch(Exception e) 
    { 
        System.out.println("Error: " + e.getMessage()); 

    } 

回答

-1

後,你不能叫

rs2.getString("Book_Name") 

如果rs2.next()

犯規從查詢匹配任何

select * from books 
+0

我想我把UIorBItf.getText()而不是rs2.getString(「Book_Name」) 謝謝 – user1625324

0
if(UIorBItf.getText().compareTo(ID)!=0) 
{ 
             // problem is here 
             // from other code I am guessing you want UIorBItf.getText() 
             // instead of rs2.getString("Book_Name") 
    JOptionPane.showMessageDialog(null,rs2.getString("Book_Name") + 
     " is not available","Query result",JOptionPane.INFORMATION_MESSAGE); 
} 
0

的問題是在這裏:

if(UIorBItf.getText().compareTo(ID)!=0) 
{ 
    JOptionPane.showMessageDialog(null,rs2.getString("Book_Name") + 
    " is not available","Query result",JOptionPane.INFORMATION_MESSAGE); 
} 

你不能從這裏叫rs2.getString("Book_Name"),因爲你只在這裏結束時while環路完了。此時,您將已經移過最後一行,因爲rs2.next()已返回false