如果记录存在,我试图更新一个值,否则将值插入数据库。但是,这不起作用。我写了下面的代码。
String sqlCheck = "Select * from [UAP].[dbo].[UAP_EMPLOYEE] where EMP_EMPLOYEE_ID = '"+empId+"' "; PreparedStatement prpStatementCheck = conn.prepareStatement(sqlCheck); prpStatementCheck.setString(1, EMP_EMPLOYEE_ID); ResultSet rsCheck=prpStatementCheck.executeQuery(); String check=null; while((rsCheck.next()) { if(rsCheck.next()>0) update statement; else Insert Statement; }
由于EMP_EMPLOYEE_ID字段是INTEGER类型,因此请使用setInt而不是setString:
setInt
setString
String sqlCheck = "Select * from [UAP].[dbo].[UAP_EMPLOYEE] where EMP_EMPLOYEE_ID = ?"; PreparedStatement prpStatementCheck = conn.prepareStatement(sqlCheck); prpStatementCheck.setInt(1, EMP_EMPLOYEE_ID);