JDBC插入Oracle数据库有主键的X表记录时,获取此记录的主键ID。

时间:2022-10-17 21:47:05

JDBC插入Oracle数据库有主键表的记录时,返回此记录的主键ID问题:

                                     无效的列类型: getInt not implemented for class oracle.jdbc.driver.T4CRowidAccessor

解决方法:

public int addAudit(Audit audit) throws Exception {

conn = DBHelper.getConn();
String sql = "insert into SAFE_AUDIT values (sequence_audit.nextval,?,?,?,?,?,?,?,?,?)";
ps = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);

ps.setString(1, audit.getAudit_number());
ps.setString(2, audit.getAudit_step());
ps.setString(3, audit.getAudit_agent());
ps.setString(4, audit.getAudit_dn());
ps.setString(5, audit.getAudit_content());
ps.setString(6, audit.getAudit_md5());
ps.setString(7, audit.getAudit_doing());
ps.setString(8, audit.getAudit_base());
ps.setString(9, audit.getAudit_date());
ps.executeUpdate();

ResultSet res = ps.getGeneratedKeys();
res.next();
String rowid= res.getString(1);//获得rowid
PreparedStatement ps1 = conn.prepareStatement("select t.audit_id from SAFE_AUDIT t where rowid = ?");
ps1.setString(1, rowid);
ResultSet rest2 = ps1.executeQuery();
rest2.next();
int audit_id=Integer.parseInt(rest2.getString(1));
DBHelper.closeResouce(ps, conn);
return audit_id;
}