mysql取最后一个值,mysql – 获取数据库中的最后一个自动增量值

我试图检索mySQL中最后插入的数据的自动增量值.这是我的代码:

public int getAutoIncrementProductID() {

ResultSet rs = null;

DBController db = new DBController();

db.getConnection();

int autoIncKeyFromFunc = -1;

rs = db.readRequest("SELECT LAST_INSERT_ID()");

try {

if (rs.next()) {

autoIncKeyFromFunc = rs.getInt(1);

System.out.println("AUTO ID IS " + autoIncKeyFromFunc);

rs.close();

}

} catch (Exception e) {

e.printStackTrace();

}

db.terminate();

return autoIncKeyFromFunc;

}

但是,尽管数据库中的自动增量列不断增加,但这些代码仍然返回0值.它只是不会得到最后插入数据的自动增量值.有人可以帮忙吗?

解决方法:

插入内容后应使用LAST_INSERT_ID().

For LAST_INSERT_ID(), the most recently generated ID is maintained in

the server on a per-connection basis. It is not changed by another

client. It is not even changed if you update another AUTO_INCREMENT

column with a nonmagic value (that is, a value that is not NULL and

not 0).

你也可以试试

SELECT max(id) FROM tableName

但它不会假设已删除的行.

标签:mysql

来源: https://codeday.me/bug/20190716/1478678.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值