MySQL connector issues

给你一囗甜甜゛ 提交于 2019-12-11 21:17:12

问题


I am facing a problem where I get certain error if change MySQL connector Jar version. My code was finely running on JBoss version 4.0.4.GA. Then I upgraded JBoss to Wildfly with connector jar version mysql-connector-java-5.1.36-bin, and did not made any changes to the code which was running well. Now whenever I add some new row I get an error saying that Generated keys not requested.

You need to specify Statement.RETURN_GENERATED_KEYS to Statement.executeUpdate() or Connection.prepareStatement().

Earlier I thought this may be because of server(JBoss) upgrade issue. And I started adding Statement.RETURN_GENERATED_KEYS to connection.prepareStatement(), in every code. But then I realize after reading this thread Migration to Mysql Connector Jar 5.1.27 its not a JBoss upgrade problem rather it is mysql connector version that causes this error. Can any one point me out which version of mysql connector should I use to stop these errors. Because it is very difficult to add Statement.RETURN_GENERATED_KEYS in number of files.

Thanks


回答1:


Note: there's a few ways to tell the driver to return generated keys

  1. Connection.prepareStatement(String sql, int autoGeneratedKeys)
  2. Connection.prepareStatement(String sql, int[] columnIndexes)
  3. Connection.prepareStatement(String sql, String[] columnNames)
  4. Statement.executeUpdate(String sql, int autoGeneratedKeys)
  5. Statement.executeUpdate(String sql, int[] columnIndexes)
  6. Statement.executeUpdate(String sql, String[] columnNames)

From the javadoc for Connection.prepareStatement(String sql, int autoGeneratedKeys)

Creates a default PreparedStatement object that has the capability to retrieve auto-generated keys. The given constant tells the driver whether it should make auto-generated keys available for retrieval. This parameter is ignored if the SQL statement is not an INSERT statement, or an SQL statement able to return auto-generated keys (the list of such statements is vendor-specific).

Unfortunately the javadoc is not clear about what happens when you call Connection.prepareStatement(String sql).

As far as I can tell, passing Statement.RETURN_GENERATED_KEYS (or the column names/indexes) guarantees that Statement.getGeneratedKeys() will return a value. If you don't explicitly request it there's no guarantee that getGeneratedKeys() will return the values(s).

In short, to be compliant with the JDBC specification, you should always use one of the methods above if you intend on calling Statement.getGeneratedKeys()



来源:https://stackoverflow.com/questions/32581253/mysql-connector-issues

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