a_h*_*ame 23
无需为此使用特定于DBMS的SQL.
这就是getGeneratedKeys()的用途.
在准备语句时,您传递自动生成列的名称,然后可以使用getGeneratedKeys()检索这些列的名称
PreparedStatement pstmt = connection.prepareStatement(
"insert into some_table (col1, col2, ..) values (....)",
new String[] { "ID_COLUMN"} );
pstmt.executeUpdate();
ResultSet rs = pstmt.getGeneratedKeys(); // will return the ID in ID_COLUMN
Run Code Online (Sandbox Code Playgroud)
请注意,在这种情况下,列名称区分大小写(在Derby和许多其他DBMS中).
new String[] { "ID_COLUMN"} 是不同的东西new String[] { "id_column"}
或者你也可以使用:
connection.prepareStatement("INSERT ...", PreparedStatement.RETURN_GENERATED_KEYS);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10980 次 |
| 最近记录: |