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

    }
4

3 回答 3

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);
}
于 2012-09-07T21:33:31.477 回答
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()has returned false

于 2012-09-07T21:34:12.080 回答
-1

你不能打电话

rs2.getString("Book_Name")

如果rs2.next()

与您的查询中的任何内容都不匹配

select * from books
于 2012-09-07T21:23:45.927 回答