获取异常:java.sql.SQLException:对空结果集的非法操作

获取异常:java.sql.SQLException:对空结果集的非法操作,java,mysql,sqlexception,Java,Mysql,Sqlexception,下面是我的代码,基本上我实现了sqlquery,我希望如果结果出现在表中,那么程序继续使用if条件,否则如果结果为空,那么else条件将运行。 当我的查询结果为空集时,我遇到异常,我不知道如何解决此查询。在多次尝试调试之后,我发现了这个问题发生的地方。它出现在if-else语句中。 否则,状态代码: while(!bool){ try { rs = c.s.executeQuery("select * from employee where cn

下面是我的代码,基本上我实现了sqlquery,我希望如果结果出现在表中,那么程序继续使用if条件,否则如果结果为空,那么else条件将运行。 当我的查询结果为空集时,我遇到异常,我不知道如何解决此查询。在多次尝试调试之后,我发现了这个问题发生的地方。它出现在if-else语句中。 否则,状态代码:

while(!bool){
        try {
            rs = c.s.executeQuery("select * from employee where cnic = '"+s9+"'");
            rs.next();
            tempCnic = rs.getString("cnic");
            if(tempCnic.equals(s9)){
                JOptionPane.showMessageDialog(null, "This CNIC Number is Already Registered");
                s9 = JOptionPane.showInputDialog("Enter a correct CNIC Number:");
            } else if(tempCnic == null) {
                bool = true;
                c.s.executeUpdate("insert into employee values('"+s1+"','"+s2+"','"+s3+"','"+s4+"','"+s5+"','"+s6+"','"+s7+"','"+s8+"','"+s9+"','"+s10+"'");        
                JOptionPane.showMessageDialog(null, "Employee Added Successfully");
                new EmployeeMenu().setVisible(true);
                setVisible(false);
            }
        } catch (SQLException ex) {
            Logger.getLogger(AddEmployee.class.getName()).log(Level.SEVERE, null, ex);
        } catch(NumberFormatException ex){
            JOptionPane.showMessageDialog(null, "Please enter a valid Number");
        }
    }
整个代码:

JButton btnAdd = new JButton("Add");
btnAdd.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        conn c = new conn();
        String radio = null;
        
        if(male.isSelected()){ 
            radio = "Male";
        }
        else if(female.isSelected()){
            radio = "Female";
        }
     
    String s1 = (String)ID;
        String s2 = txtfirstname.getText();
        String s3 = txtlastname.getText();
        String s4 = txtage.getText();
        String s5 = radio;
        String s6 = (String)comboBox2.getSelectedItem();
        String s7 = txtsalary.getText();
        String s8 = txtphone.getText();
        String s9 = txtcnic.getText();    
        String s10 = txtemail.getText();
        
        ResultSet rs;
        String tempCnic;
        boolean bool = false;
        while(!bool){
            try {
                rs = c.s.executeQuery("select * from employee where cnic = '"+s9+"'");
                rs.next();
                tempCnic = rs.getString("cnic");
                if(tempCnic.equals(s9)){
                    JOptionPane.showMessageDialog(null, "This CNIC Number is Already Registered");
                    s9 = JOptionPane.showInputDialog("Enter a correct CNIC Number:");
                } else if(tempCnic == null) {
                    bool = true;
                    c.s.executeUpdate("insert into employee values('"+s1+"','"+s2+"','"+s3+"','"+s4+"','"+s5+"','"+s6+"','"+s7+"','"+s8+"','"+s9+"','"+s10+"'");        
                    JOptionPane.showMessageDialog(null, "Employee Added Successfully");
                    new EmployeeMenu().setVisible(true);
                    setVisible(false);
                }
            } catch (SQLException ex) {
                Logger.getLogger(AddEmployee.class.getName()).log(Level.SEVERE, null, ex);
            } catch(NumberFormatException ex){
                JOptionPane.showMessageDialog(null, "Please enter a valid Number");
            }
        }
    }
});

请帮帮我,告诉我我犯了什么错误。谢谢

rs.next()
返回布尔值-传统上,将进入
while()
if()
-例如
while(rs.next()){…}
if(rs.next()){…}
@RandyCasburn您的意思是我应该用
if(rs.next())
删除
if(tempCnic equals(s9))
,在下一个语句中,赋值为
tempCnic