我正在玩JDBC/MySQL 5.1.我创建了一个insert查询,将一些数据插入到表中,并希望从新创建的行返回生成的键.但是,当我通过"id"引用列时,这是我的PK和自动增量列.
PreparedStatement ps = St0rm.getInstance().getDatabase("main")
.prepare("INSERT INTO quests (name,minlevel,start_npc,end_npc) VALUES(?,?,?,?)", true); // creates a prepared statement with flag RETURN_GENERATED_KEYS
// ...
int affected = ps.executeUpdate();
ResultSet keys = ps.getGeneratedKeys();
if (affected > 0 && keys.next()) {
St0rm.getInstance().getLogger().warning(String.format("ID Column Name: %s", keys.getMetaData().getColumnName(1))); // says the column name is: GENERATED_KEY
q = new Quest(keys.getInt(1)); // column index from the generated key, no error thrown.
q = new Quest(keys.getInt("id")); // actual column name, line throws a SQLException
// ...
}
Run Code Online (Sandbox Code Playgroud)
所以,我的问题:为什么ResultSet.getGeneratedKeys使用GENERATED_KEY列名?
| 归档时间: |
|
| 查看次数: |
1698 次 |
| 最近记录: |