码迷,mamicode.com
首页 > 数据库 > 详细

mysql获取插入的id主键

时间:2017-12-28 21:34:33      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:null   run   long   注意   on()   integer   select   insert   eof   

long _retId = -1;
        Connection con = null;
        try {
            con = queryRunner.getDataSource().getConnection();
            queryRunner.update(con, sql.toString(), val);
            Object _ret = queryRunner.query(con, "SELECT LAST_INSERT_ID() FROM " + getTableName(), new ScalarHandler());
            if (_ret instanceof BigInteger) {
                _retId = ((BigInteger) _ret).longValue();
            } else if (_ret instanceof Long) {
                _retId = (Long) _ret;
            } else {
                _retId = ((Integer) _ret).longValue();
            }
        } catch (SQLException e) {
            throw e;
        } finally {
            ConnectionUtils.closeConnection(con);
        }

注意:必须使用同一个Connection,否则会获取不到想要的结果

mysql获取插入的id主键

标签:null   run   long   注意   on()   integer   select   insert   eof   

原文地址:https://www.cnblogs.com/nizuimeiabc1/p/8137510.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!