简体   繁体   English

带有用于MySQL 5.1的JDBC的getGeneratedKeys()的奇怪异常

[英]Strange Exception on getGeneratedKeys() with JDBC for MySQL 5.1

I'm using JDBC to insert a row into a MYSQL database. 我正在使用JDBC在MYSQL数据库中插入一行。 I build a parameterized command, execute it and attempt to retrieve the auto generated keys as follows: 我构建了一个参数化命令,执行该命令并尝试按以下方式检索自动生成的键:

String sql = "INSERT IGNORE INTO `users` (`email`, `pass-hash`) VALUES (?, ?)";
Connection conn = SQLAccess.getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, login);
ps.setString(2, passHash);

int count = ps.executeUpdate();

if (count == 1) {
    ResultSet rs = ps.getGeneratedKeys();
    rs.next();
         //some more stuff...
}

For some reason, I get the following SQLException on the line containing ResultSet rs = ps.getGeneratedKeys(); 由于某种原因,我在包含ResultSet rs = ps.getGeneratedKeys();的行上获得了以下SQLException ResultSet rs = ps.getGeneratedKeys(); :

!Statement.Generated Keys Not Requested! !Statement.Generated密钥未请求!

Any thoughts? 有什么想法吗? When I run the same query, as generated by running the app through the debugger, in a MySQL browser it executes without incident. 当我运行相同的查询时(如通过调试器运行应用程序所生成的查询),在MySQL浏览器中它会毫无意外地执行。

Thanks, brian 谢谢,布莱恩

我认为您想致电prepareStatement(sql, RETURN_GENERATED_KEYS)

The method getGeneratedKeys() is in JDBC 3.0 API and this is not useable on all platform. 方法getGeneratedKeys()在JDBC 3.0 API中,并且并非在所有平台上都可用。

And PreparedStatement does not have getGenerateKeys() 's implement and it is only in Statement and is a abstract method. 而且PreparedStatement没有getGenerateKeys()的实现,它仅在Statement中,是一种抽象方法。

Here is a topic about this method's issue : http://forums.sun.com/thread.jspa?threadID=260818 这是有关此方法的问题的主题: http : //forums.sun.com/thread.jspa?threadID=260818

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM