2016-12-04 38 views
0
public void actionPerformed(ActionEvent e) { 
       try{ 
        String searchQuery = "select BookTitle,ISBN,Price,IsAvailable from BookDetails where BookTitle =?"; 
        PreparedStatement serPST = connector.prepareStatement(searchQuery); 
        serPST.setString(1,textTitleSearch.getText()); 
        ResultSet serResult = serPST.executeQuery(); 
        int noofbooks = 0; 
        while(serResult.next()){ 
         noofbooks++; 
        } 
        if (noofbooks!=0){ 
         textResultTitle.setText(serResult.getString("BookTitle")); 
         textResultISBN.setText(Integer.toString((serResult.getInt("ISBN")))); 
         textResultPrice.setText(Integer.toString((serResult.getInt("Price")))); 
         textAvailability.setText(serResult.getString("IsAvailable")); 
         } 
        else{ 
         JOptionPane.showMessageDialog(null,"Adjust the title"); 
        } 
        serPST.close(); 
        serResult.close(); 
        } 
       catch(Exception errSearch){ 
        JOptionPane.showMessageDialog(null, errSearch); 
        System.out.println(errSearch.getCause()); 

        } 
      } 

回答

1

在while循环记录,您将光标移动到最后一排,然后经过while循环您得到的结果从游标其中有没有记录?

你的代码应该是:

while(serResult.next()){ 
    noofbooks++; 
    textResultTitle.setText(serResult.getString("BookTitle"));      
    textResultISBN.setText(Integer.toString((serResult.getInt("ISBN"))));       
    textResultPrice.setText(Integer.toString((serResult.getInt("Price")))); 
    textAvailability.setText(serResult.getString("IsAvailable")); 
} 
if (noofbooks == 0){ 
    JOptionPane.showMessageDialog(null,"Adjust the title"); 
}