RETURN_GENERATED_KEYS doesn't work using JDBC ODBC

匿名 (未验证) 提交于 2019-12-03 01:03:01

问题:

I'm trying to get insert ID while after inserting some data in my database.

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'    

回答1:

The ODBC bridge driver doesn't support it. 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. Almost all self-respected server based RDBMS vendors provides a fullworthy JDBC driver for download at their homepage. Just Google "[vendorname] jdbc driver download" to find it. Here's an overview:



回答2:

MAy be the JDBC implementation could not be supporting the sepcific opration. CHeck the JDBC driver used.



回答3:

Try this example instead of 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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!