MySQL连接器问题 [英] MySQL connector issues

查看:169
本文介绍了MySQL连接器问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的问题是,如果更改MySQL连接器Jar版本,则会出现某些错误. 我的代码在JBoss版本4.0.4.GA上运行良好. 然后,我使用连接器jar版本mysql-connector-java-5.1.36-bin将JBoss升级到Wildfly,并且未对运行良好的代码进行任何更改. 现在,每当我添加一些新行时,我都会收到一条错误消息,指出不要求生成密钥.

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().

我之前认为这可能是由于服务器(JBoss)升级问题引起的.然后,我开始在每个代码中将Statement.RETURN_GENERATED_KEYS添加到connection.prepareStatement().但随后,我在阅读此线程后意识到迁移到Mysql Connector Jar 5.1.27 不是JBoss升级问题,而是导致此错误的mysql连接器版本. 谁能指出我应使用哪个版本的mysql连接器停止这些错误.因为很难在文件数量中添加Statement.RETURN_GENERATED_KEYS.

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.

谢谢

推荐答案

注意:有几种方法可以告诉驱动程序返回生成的密钥

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

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

来自Connection.prepareStatement(String sql, int autoGeneratedKeys)

创建一个默认的PreparedStatement对象,该对象具有检索自动生成的键的功能.给定的常数告诉驱动程序是否应该使自动生成的密钥可用于检索.如果SQL语句不是INSERT语句,也不是能够返回自动生成的键的SQL语句(此类语句的列表是特定于供应商的),则忽略此参数.

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).

很遗憾, javadoc 不清楚您调用Connection.prepareStatement(String sql)时会发生什么.

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

据我所知,传递Statement.RETURN_GENERATED_KEYS(或列名/索引)可以保证Statement.getGeneratedKeys()将返回一个值.如果您未明确要求,则不能保证getGeneratedKeys()将返回值.

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).

简而言之,为了符合JDBC规范,如果要调用Statement.getGeneratedKeys()

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()

这篇关于MySQL连接器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆