我正在通过java连接mysql。我正在尝试插入一条记录,并且使用 statement.getGenerateKeys()
和 if ( generatedKeys.next())
查找状态。现在我可以获得插入的 coulmn 值(即,我有名为 pass 的列,它是一个 auto_increament 列,它是我的主键,现在我想获取插入到该列中的值。可能吗?
最佳答案
标准 MySQL Connector J 驱动程序确实支持获取生成的 key ,是的。这是一些示例代码:
final Connection conn ; // setup connection
final String SQL ; // define SQL template string
final PreparedStatement stmt = connection.prepareStatement(
SQL,
Statement.RETURN_GENERATED_KEYS);
int affected = stmt.executeUpdate();
if (affected > 0) {
final ResultSet keySet = stmt.getGeneratedKeys();
while (keySet.next()) {
// these are your autogenerated keys, do something with them
System.out.println(keySet.getInt(1));
}
}
关于java - 在MySQL中使用statement.getGenerateKeys()时是否可以获取自动生成的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15615070/