Loading

jdbc 占位符插入null值 NullPointerException

 

String sql = "insert into t_curriculum (name,academic_credit_type,qualifiedCredits" + 
",substandardCredits,t_curriculum_type_id,description,teacherName,createDate) values(?,?,?,?,?,?,?,now())";
PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
ps.setString(1, curriculum.getName());
ps.setInt(2, curriculum.getAcademic_credit_type());
ps.setDouble(3, curriculum.getQualifiedCredits());
ps.setDouble(4, curriculum.getSubstandardCredits());
ps.setLong(5, curriculum.getT_curriculum_type_id());
ps.setString(6, curriculum.getDescription());
ps.setString(7, curriculum.getTeacherName());
如何传参才不会NullPointerException,但我想传的那个参数它是null的,在不改动sql语句情况下如何实现?ps.setLong(5, curriculum.getT_curriculum_type_id());这一段报异常
 
这样子解决:
if( curriculum.getT_curriculum_type_id()==null){
ps.setNull(5, java.sql.Types.INTEGER);
}else{
ps.setLong(5, curriculum.getT_curriculum_type_id());
}
posted @ 2014-03-27 08:44  dai.sp  阅读(2338)  评论(0编辑  收藏  举报