使用数据库访问和Netbeans的员工的错误处理程序 [英] Error Handler for Employee using Database Access and Netbeans

查看:54
本文介绍了使用数据库访问和Netbeans的员工的错误处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您单击该按钮时,即使Employee不存在,也会显示MessageBox已删除数据。

When you click the button, even if the Employee does not exist, MessageBox is shown that the Data has been deleted.

if(evt.getSource()==btnDelete){
            try{
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con = DriverManager.getConnection("jdbc:odbc:table1");
            Statement st = con.createStatement();
            int empno = Integer.parseInt(txtDelete.getText());
            
            ResultSet rs = st.executeQuery("SELECT * FROM Activity where employee_number='"+empno+"'");
                int i = st.executeUpdate("DELETE FROM Activity where employee_number ='"+empno+"'");
            
                while(rs.next()){
                    ResultSet.getInt(empno);
           
            JOptionPane.showMessageDialog(null, "Row is deleted");
            txtDelete.setText("");
             txtAdd1.setText("");
            txtAdd2.setText("");
            txtAdd3.setText("");
            txtAdd4.setText("");
            con.close();
            st.close();
                }
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null, "Data not found");
        }
        }
    }

推荐答案

您已经在检索命令的答案了DB:



You are already retrieving an answer for your command to the DB:

int i = st.executeUpdate("DELETE FROM Activity where employee_number ='"+empno+"'");





返回值是行数受命令影响。当你删除它时,如果成功则返回-1:





The return value is the number of rows affected by the command. As you are deleting it should return -1 if successful:

if(i == -1){
 // celebrate! 
}


这篇关于使用数据库访问和Netbeans的员工的错误处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆