[英]RETURN_GENERATED_KEYS doesn't work using JDBC ODBC
I'm trying to get insert ID while after inserting some data in my database. 在数据库中插入一些数据后,我试图获取插入ID。
String sql = "INSERT INTO ADI.DUMMY(dummy_data) VALUES('from database logger')";
PreparedStatement ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
int extUptReturn = ps.executeUpdate(sql);
But I got this exception: 但是我有一个例外:
Java exception: ''java.lang.UnsupportedOperationException'';
thrown from class name: ''sun.jdbc.odbc.JdbcOdbcConnection'', method name: ''prepareStatement'', file: ''JdbcOdbcConnection.java'', line: '1762'
The ODBC bridge driver doesn't support it. ODBC桥驱动程序不支持它。 Nothing to do against.
没关系。 Either replace the driver or live with it.
替换驱动程序或使用它。 I would just use a real JDBC driver instead of the poorly-developed, feature-lacking, bug-rich Sun ODBC bridge driver.
我将只使用真正的 JDBC驱动程序,而不是开发欠佳,功能不足,bug丰富的Sun ODBC桥驱动程序。 Almost all self-respected server based RDBMS vendors provides a fullworthy JDBC driver for download at their homepage.
几乎所有自重的基于服务器的RDBMS供应商都提供了一个值得下载的JDBC驱动程序,可在其主页上下载。 Just Google "[vendorname] jdbc driver download" to find it.
只需在Google“ [供应商名称] jdbc驱动程序下载”中找到它即可。 Here's an overview:
以下是概述:
/java
folder of the DB2 installation. /java
文件夹中。 MAy be the JDBC implementation could not be supporting the sepcific opration. 可能是JDBC实现不能支持特殊操作。 CHeck the JDBC driver used.
检查使用的JDBC驱动程序。
Try this example instead of Statement.RETURN_GENERATED_KEYS
: 请尝试以下示例,而不是
Statement.RETURN_GENERATED_KEYS
:
String[] returnId = { "BATCHID" };
String sql = "INSERT INTO BATCH (BATCHNAME) VALUES ('aaaaaaa')";
PreparedStatement statement = connection
.prepareStatement(sql, returnId);
int affectedRows = statement.executeUpdate();
if (affectedRows == 0) {
throw new SQLException("Creating user failed, no rows affected.");
}
try (ResultSet rs = statement.getGeneratedKeys()) {
if (rs.next()) {
System.out.println(rs.getInt(1));
}
rs.close();
}
Where BRANCHID is the auto generated id 其中BRANCHID是自动生成的ID
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.