日期:2014-05-16  浏览次数:20832 次

mysql返回新插入的id值

今天项目中用到:数据表id自增,当新插入一条记录后马上返回这个插入的id值,核心代码conn=DBUtilsC3P0.getInstance().getConnection();
??? ??? ??? Statement stmt =conn.createStatement();
??? ??? ??? stmt.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);
??? ??? ??? ResultSet newid = stmt.getGeneratedKeys();
??? ??? ??? if(newid.next()){
??? ??? ??? ??? uid=newid.getInt(1);
??? ??? ??? }