2016-12-04 9 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"); 
} 
関連する問題