PreparedStatement与Statement.RETURN_GENERATED_KEYS

某些JDBC驱动程序返回Statement.RETURN_GENERATED_KEYS的唯一方法是执行以下操作:

 long key = -1L; Statement statement = connection.createStatement(); statement.executeUpdate(YOUR_SQL_HERE, Statement.RETURN_GENERATED_KEYS); ResultSet rs = statement.getGeneratedKeys(); if (rs != null && rs.next()) { key = rs.getLong(1); } 

有没有办法与PreparedStatement


编辑

我问我是否可以用PreparedStatement做同样的事情的原因考虑以下情况:

 private static final String SQL_CREATE = "INSERT INTO USER(FIRST_NAME, MIDDLE_NAME, LAST_NAME, EMAIL_ADDRESS, DOB) VALUES (?, ?, ?, ?, ?)"; 

USER表中有一个PRIMARY KEY (USER_ID) ,它是一个BIGINT AUTOINCREMENT (因此你为什么不在SQL_CREATEstring中看到它。

现在,我填充? 使用PreparedStatement.setXXXX(index, value) 。 我想返回ResultSet rs = PreparedStatement.getGeneratedKeys() 。 我怎样才能做到这一点?

您可以使用另外一个int参数的prepareStatement方法

 PreparedStatement ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS) 

对于某些JDBC驱动程序(例如Oracle),您必须明确列出生成的密钥的列名称或索引:

 PreparedStatement ps = con.prepareStatement(sql, new String[]{"USER_ID"}) 

你的意思是这样吗?

 long key = -1L; PreparedStatement preparedStatement = connection.prepareStatement(YOUR_SQL_HERE, PreparedStatement.RETURN_GENERATED_KEYS); preparedStatement.setXXX(index, VALUE); preparedStatement.executeUpdate(); ResultSet rs = preparedStatement.getGeneratedKeys(); if (rs.next()) { key = rs.getLong(1); } 

我现在没有编译器,我会回答一个问题:

你尝试过吗? 它工作吗?

 long key = -1L; PreparedStatement statement = connection.prepareStatement(); statement.executeUpdate(YOUR_SQL_HERE, PreparedStatement.RETURN_GENERATED_KEYS); ResultSet rs = statement.getGeneratedKeys(); if (rs != null && rs.next()) { key = rs.getLong(1); } 

免责声明:显然,我没有编译这个,但你明白了。

PreparedStatement是Statement的一个子接口,所以我没有看到为什么这种方法不起作用,除非一些JDBC驱动程序有问题。

 private void alarmEventInsert(DriveDetail driveDetail, String vehicleRegNo, int organizationId) { final String ALARM_EVENT_INS_SQL = "INSERT INTO alarm_event (event_code,param1,param2,org_id,created_time) VALUES (?,?,?,?,?)"; CachedConnection conn = JDatabaseManager.getConnection(); PreparedStatement ps = null; ResultSet generatedKeys = null; try { ps = conn.prepareStatement(ALARM_EVENT_INS_SQL, ps.RETURN_GENERATED_KEYS); ps.setInt(1, driveDetail.getEventCode()); ps.setString(2, vehicleRegNo); ps.setString(3, null); ps.setInt(4, organizationId); ps.setString(5, driveDetail.getCreateTime()); ps.execute(); generatedKeys = ps.getGeneratedKeys(); if (generatedKeys.next()) { driveDetail.setStopDuration(generatedKeys.getInt(1)); } } catch (SQLException e) { e.printStackTrace(); logger.error("Error inserting into alarm_event : {}", e .getMessage()); logger.info(ps.toString()); } finally { if (ps != null) { try { if (ps != null) ps.close(); } catch (SQLException e) { logger.error("Error closing prepared statements : {}", e .getMessage()); } } } JDatabaseManager.freeConnection(conn); } 
 String query = "INSERT INTO ...."; PreparedStatement preparedStatement = connection.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); preparedStatement.setXXX(1, VALUE); preparedStatement.setXXX(2, VALUE); .... preparedStatement.executeUpdate(); ResultSet rs = preparedStatement.getGeneratedKeys(); int key = rs.next() ? rs.getInt(1) : 0; if(key!=0){ System.out.println("Generated key="+key); }