您的位置:首页 > 其它

通过getGeneratedKeys获取记录的主键

2013-08-24 22:43 375 查看
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
//建立连接
con= JDBCUtils.getConnection();
//创建语句
String sql="insert into t_user values('test','123',null,null)";
ps = con.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
ps.executeUpdate();
rs= ps.getGeneratedKeys();//获取主键集合
if (rs.next()) {
System.out.println(rs.getInt(1));//获取主键
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: