私はJava経由でmysqlを接続しています。レコードを挿入しようとしていますが、を使用してステータスを検索していstatement.getGeneratedKeys()
ますif (generatedKeys.next())
。これで、挿入されたcoulmnの値を取得できますか(つまり、passという名前の列があり、auto_increament列であり、これが主キーであり、この列に挿入された値を取得したいと思います。出来ますか??
1 に答える
2
標準のMySQLConnectorJドライバーは、生成されたキーの取得をサポートしています。サンプルコードは次のとおりです。
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));
}
}
于 2013-03-25T12:50:54.123 に答える