【发布时间】:2016-06-05 02:36:04
【问题描述】:
我想将最后插入的 ID 设为long。因为我的表的主键是long 数据类型。
这是我的代码:
Connection connection = null;
Statement statement = null;
try {
String sql = "INSERT INTO testTable(name) VALUES('Anonym')";
connection = DriverManager.getConnection(DB_URL, USER, PASS);
statement = connection.createStatement();
long lastInsertedID = statement.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);
System.out.println("LAST INSERTED ID = "+lastInsertedID);
} catch (Exception e) {
e.printStackTrace();
}
我已经测试过,当整数的最大值达到时,我得到最后一个作为最后插入的 ID。
顺便说一句,我已经通过了这个post。
谢谢。
【问题讨论】:
-
try (ResultSet generatedKeys = statement.getGeneratedKeys()) { generatedKeys.next(); SYstem.out.println(generatedKeys.getLong(1)); }- BalusC