日期:2014-05-17 浏览次数:21830 次
String sql = "insert into user(name,password,email,birthday) values('abc','123','abc@sina.com','1978-08-08')";
Connection conn = JdbcUtil.getConnection();
PreparedStatement st = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS );
st.executeUpdate();
ResultSet rs = st.getGeneratedKeys(); //得到插入行的主键,结果集中只有一条记录
if(rs.next())
System.out.println(rs.getObject(1)); //rs.getInt(1)
注意:prepareStatement(sql,Statement.RETURN_GENERATED_KEYS );
hibernate的
Integer id = (Integer) getSession().save(transientInstance);