通过getGeneratedKeys获取记录的主键

    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));//获取主键
            }

 

posted @ 2013-08-24 22:43  苦逼IT男  阅读(342)  评论(0编辑  收藏  举报