Connection con = null ; PreparedStatement pstmt = null; try { con = this.getSqlMapClientTemplate().getDataSource().getConnection(); con.setAutoCommit(false); pstmt = con.prepareStatement("INSERT INTO AMS_BUSINESS_TODO(BUSINESS_TODO_ID, USER_ID, TABLE_ID, BUSINESS_LINK_ID, BUSINESS_LINK_SOURCE_ID, " + "ENTRY_ID, ENTRY_TITLE, TODO_BATCH_ID, TODO_STATE, HAPPEN_DATE, FILING_MODE, VALID_FLAG,TODO_SUBMIT_USER_ID)" //新增组织机构ID,角色ID以及提交人ID +"values(?,?,?,?,?,?,?,?,?,?,?,?,?)"); Long id = Long.valueOf(SequenceUtil.get_id(KeyConstant.AMS_BUSINESS_TODO_ID, list.size())); for (int i = 0; i < list.size(); i++) { list.get(i).setBusiness_todo_id(id + i); pstmt.setLong(1, id + i); pstmt.setLong(2, list.get(i).getUser_id()); pstmt.setLong(3, list.get(i).getTable_id()); pstmt.setLong(4, list.get(i).getBusiness_link_id()); pstmt.setLong(5, list.get(i).getBusiness_link_source_id()); pstmt.setLong(6, list.get(i).getEntry_id()); pstmt.setString(7, list.get(i).getEntry_title()); pstmt.setLong(8, list.get(i).getTodo_batch_id()); pstmt.setInt(9, list.get(i).getTodo_state()); pstmt.setTimestamp(10, list.get(i).getHappen_date()); pstmt.setInt(11, list.get(i).getFiling_mode()); pstmt.setInt(12, list.get(i).getValid_flag()); pstmt.setLong(13, list.get(i).getTodo_submit_user_id()); pstmt.addBatch(); } pstmt.executeBatch(); con.commit(); r.setMessage("导入完成"); r.setResult(id); r.setState(true); return r; } catch (SQLException e) { e.printStackTrace(); return R.falseState("导入失败"); }finally{ if(pstmt != null){ try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if(con != null){ try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } }如何给上述代码中的insert参数里面添加null值
时间: 2024-04-28 11:23:09 浏览: 54
connection 和 preparedstatement 的关闭问题
要给上述代码中的INSERT参数里面添加null值,可以使用setNull()方法。例如,如果要将第11个参数设置为null,可以在循环中添加以下代码:
if (list.get(i).getHappen_date() == null) {
pstmt.setNull(10, java.sql.Types.TIMESTAMP);
} else {
pstmt.setTimestamp(10, list.get(i).getHappen_date());
}
这将检查Happen_date是否为null,如果是,则使用setNull()方法将第10个参数设置为null。否则,将使用setTimestamp()方法将Happen_date设置为第10个参数的值。同样,您可以使用setNull()方法来设置其他参数为null。
阅读全文