我从http://docs.oracle.com/javase/1.4.2/docs/guide/jdbc/getstart/preparedstatement.html获得了以下代码示例
我有三个问题:
- 考虑到只有三列 - LAST、FIRST、HOME,'keyColumn' 指的是什么。
- 为什么在迭代生成的密钥时使用循环?是否为该一个插入语句返回了多行?
- 哪些数据库支持每个表生成多个键?
String sql = "INSERT INTO AUTHORS (LAST, FIRST, HOME) VALUES " +
"(?, ?, ?, keyColumn)";
PreparedStatement addAuthor = con.prepareStatement(sql,
Statement.RETURN_GENERATED_KEYS);
addAuthor.setString(1, "Wordsworth");
addAuthor.setString(2, "William");
addAuthor.setString(3, "England");
int rows = addAuthor.executeUpdate();
ResultSet rs = stmt.getGeneratedKeys();
if (rs.next()) {
ResultSetMetaData rsmd = rs.getMetaData();
int colCount = rsmd.getColumnCount();
do {
for (int i = 1; i <= colCount; i++) {
String key = rs.getString(i);
System.out.println("key " + i + "is " + key);
}
}
while (rs.next();)
}
else {
System.out.println("There are no generated keys.");
}