微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

sqlserver和sybase数据库插入表数据返回主键的不同

做个记录,以后碰到了也可以看看。 public int addUserLog(final UserLog userLog) { if(userLog==null) return -1; if(JdbcUtil.isSybaseIQ){ int key = -1; try{ String dateStr = UserLogUtil.getDateStr(new Date()); String values = userLog.getUserId()+","+userLog.getoperateType()+",'"+ userLog.getDescription()+"','"+dateStr+"'"; final String sql = "insert into ps_user_log (userId,operateType,description,createTime) values("+values+")"; Connection conn = this.getConnection(); Statement objstatement = conn.createStatement(); objstatement .executeUpdate(sql,Statement.RETURN_GENERATED_KEYS); ResultSet objResultSet = objstatement.getGeneratedKeys(); if (objResultSet.next()) { key = objResultSet.getInt(1); } return key; }catch (Exception e) { logger.error("addUserLog error : ",e); e.printstacktrace(); return key; } }else{ try{ final String sql = "insert into ps_user_log (userId,createTime) values(?,?,?)"; PreparedStatementCreator psc = new PreparedStatementCreator() { public PreparedStatement createPreparedStatement(Connection conn) throws sqlException { PreparedStatement ps=JdbcUtil.createPrepareStatement(conn,sql); ps.setInt(1,userLog.getUserId()); ps.setInt(2,userLog.getoperateType()); ps.setString(3,userLog.getDescription()); ps.setDate(4,new java.sql.Date(System.currentTimeMillis())); return ps; } }; KeyHolder kh = new GeneratedKeyHolder(); return getJdbcTemplate().update(psc,kh); }catch(Exception e){ e.printstacktrace(); return -1; } } } 顺便再提一句,sybase数据库中不能保存Boolean类型的数据,必须转换成int值,sqlserver则可以,但其保存在数据库的值也是按照int型来保存的,1:true 0:false

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐