mysql獲取插入的id主鍵


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,否則會獲取不到想要的結果


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM