Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 未找到数据时如何使用showMessageDialog_Java_Database_Search_Criteria - Fatal编程技术网

Java 未找到数据时如何使用showMessageDialog

Java 未找到数据时如何使用showMessageDialog,java,database,search,criteria,Java,Database,Search,Criteria,我正在制作一个小程序,它与数据库连接。现在我正在做一个搜索选项,你需要把你的搜索条件放在一个文本框中,然后点击“搜索”按钮。我成功地做到了这一点,只是现在我需要扩展它。 当您点击“搜索”按钮后未找到数据时,需要弹出一个ShowMessage对话框,其中显示“未找到与您的搜索条件匹配的数据” String naam = zoekField.getText(); try { DefaultComboBoxModel dier = new DefaultComboBoxModel();

我正在制作一个小程序,它与数据库连接。现在我正在做一个搜索选项,你需要把你的搜索条件放在一个文本框中,然后点击“搜索”按钮。我成功地做到了这一点,只是现在我需要扩展它。 当您点击“搜索”按钮后未找到数据时,需要弹出一个ShowMessage对话框,其中显示“未找到与您的搜索条件匹配的数据”

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());
}
我怎样才能做到这一点?如何将其集成到现有代码中

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());
}
这是我目前的搜索方法:

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

}

我认为在while外观之前检查rs.isLast()或rs.isAfterLast()应该会有所帮助。我不能100%确定哪个是正确的,但我认为应该是rs.isAfterLast()。

private void zoekbuttonationperformed(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());
}

非常感谢你,我的兄弟!