问题描述
public void depuploadadd(BranchVO branchVO, Connection con) throws BusinessException, SQLException {// TODO Auto-generated method stubPreparedStatement stmt = null;PreparedStatement stmt2 = null;ResultSet rs = null;BranchVO bVO = (BranchVO) branchVO ;try {String sql = "insert into contentdb("+ "draf_date,dep_name,draf_person,title,c_path,dep_check,checkyn,code,username)"+ " values(?,?,?,?,?,?,?,?,?)";String sql2 = "insert into messagedep (c_id,person_set,depart_id) values(?,?,?)";stmt = con.prepareStatement(sql);System.out.print(sql);stmt2 = con.prepareStatement(sql2);//绑定参数stmt.setString(1,branchVO.getDraf_date());stmt.setString(2,branchVO.getDep_name());stmt.setString(3,branchVO.getDraf_person());stmt.setString(4,branchVO.getTitle());stmt.setString(5,branchVO.getC_path());stmt.setString(6,branchVO.getDep_check());stmt.setString(7,branchVO.getCheckyn());stmt.setString(8,branchVO.getCode());stmt.setString(9,branchVO.getUsername());stmt2.setString(1,branchVO.getC_id()); stmt2.setString(2,branchVO.getDepart_id());stmt2.setString(3,branchVO.getDepart_name());stmt.execute();stmt2.execute();} catch (Exception e) {throw new BusinessException(e.getMessage());} finally {try {stmt.close();stmt2.close();} catch (Exception e) {e.printStackTrace();throw new BusinessException(e.getMessage());}}}我想把刚刚保存的第一个insert into contentdb里的自增字段c_id取出来,在保存到insert into messagedep里请问怎么写
解决方案
使用SCOPE_IDENTITY()会比较好,能防止高并发错误。
解决方案二:
二楼的确实有并发问题,其实还是Hibernate好 哈哈..
解决方案三:
select @@identity
解决方案四:
影响中有一个SQL 函数,是可以查询最新插入记录的。具体忘记了,你上网搜下!这是我找到一些。希望有帮助:http://www.cnblogs.com/narkissos/archive/2010/09/15/1826764.html