2014-01-13 4 views
0

데이터베이스와 연결되어있는 작은 프로그램을 만들고 있습니다. 이제 검색 옵션을 만들고 있습니다. 검색 조건을 텍스트 상자에 넣은 다음 "검색"버튼을 눌러야합니다. 나는 이것을 성공적으로 해냈다. 지금은 이것을 연장 할 필요가있다. "검색"버튼을 누른 후 데이터가 발견되지 않으면 showMessageDialog에 "검색 기준과 일치하는 데이터가 없습니다"와 같은 팝업이 필요합니다.데이터가 없을 때 showMessageDialog를 사용하는 방법

어떻게 할 수 있습니까? 이 코드를 기존 코드에 어떻게 통합 할 수 있습니까?

private void zoekButtonActionPerformed(java.awt.event.ActionEvent evt) {           

    String naam = zoekField.getText(); 

    try { 
     DefaultComboBoxModel dier = new DefaultComboBoxModel(); 

     Connection conn = SimpleDataSourceV2.getConnection(); 

     Statement stat = conn.createStatement(); 

     ResultSet rs = stat.executeQuery("SELECT * FROM dier WHERE rnaam = '" + naam + "';"); 

     ModelItem item; 

     while (rs.next()) { 
      item = new ModelItem(); 
      item.roepnaam = rs.getString("rnaam"); 
      item.geslacht = rs.getString("gesl"); 
      item.snr = rs.getInt("snr"); 
      dier.addElement(item); 
     } 
     rs.close(); 

     stat.close(); 

     lijst.setModel(dier); 

    } catch (SQLException e) { 
     JOptionPane.showMessageDialog(this, e.getMessage()); 
    } 

}

답변

0

여기 도움이 당신의 동안 모양 전에 rs.isLast() 또는 rs.isAfterLast()에 대한 수표를 생각 :

이 지금까지 내 검색 방법이다. 나는 어느 것이 맞는지 100 % 확실하지는 않지만 rs.isAfterLast()이어야한다고 생각한다.

0

개인 무효 zoekButtonActionPerformed (java.awt.event.ActionEvent의의 EVT) {

String naam = zoekField.getText(); 

try { 
    DefaultComboBoxModel dier = new DefaultComboBoxModel(); 

    Connection conn = SimpleDataSourceV2.getConnection(); 

    Statement stat = conn.createStatement(); 

    ResultSet rs = stat.executeQuery("SELECT * FROM dier WHERE rnaam = '" + naam + "';"); 

    ModelItem item; 

    if(rs.next()) { 
     do{ 
      item = new ModelItem(); 
      item.roepnaam = rs.getString("rnaam"); 
      item.geslacht = rs.getString("gesl"); 
      item.snr = rs.getInt("snr"); 
      dier.addElement(item); 
     }while(rs.next()); 
    }else{ 
     JOptionPane.showMessageDialog(this, "No data found"); 
    } 
    rs.close(); 

    stat.close(); 

    lijst.setModel(dier); 

} catch (SQLException e) { 
    JOptionPane.showMessageDialog(this, e.getMessage()); 
} 
+0

아주 많이 내 동생 주셔서 감사합니다! –

관련 문제