我有一个查询如下:
@H_403_2@String sql = "insert into table (id,name) values (sequence.nextval,?)";
然后我像这样制作一个PreparedStatement:
@H_403_2@//initiate connection,statement etc pStatement = connection.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS); pStatement.setString(1,'blabla'); pStatement.executeUpdate(); ResultSet rs = pStatement.getGeneratedKeys(); while (rs.next()){ //debugging here to see what rs has }在该调试点执行和调试时,我看到我的ResultSet只有一个键,一个字符串 – 不像我期望的id.检查数据库时一切正常,id插入和一切. getGeneratedKeys()有一些东西;
那令我困惑.
我究竟做错了什么?
提前致谢
解决方法
我希望你回来的看起来像字符串的“密钥”是ROWID–这是数据库直接生成的唯一密钥.您应该能够更改它以获取您的id列(这可能需要适度的最新版本的JDBC驱动程序).
@H_403_2@//initiate connection,statement etc
String generatedColumns[] = {"ID"};
pStatement = connection.prepareStatement(sql,generatedColumns);
pStatement.setString(1,'blabla');
pStatement.executeUpdate();
ResultSet rs = pStatement.getGeneratedKeys();
while (rs.next()){
//debugging here to see what rs has
}
@H_403_2@String sql = "insert into table (id,name) " +
" values (sequence.nextval,?) " +
" returning id into ?";