Strange Exception on getGeneratedKeys() with JDBC for MySQL 5.1

Posted by sweeney on Stack Overflow See other posts from Stack Overflow or by sweeney
Published on 2009-05-13T00:05:57Z Indexed on 2010/04/19 11:03 UTC
Read the original article Hit count: 346

Filed under:
|
|
|

Hello,

I'm using JDBC to insert a row into a MYSQL database. 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();:

!Statement.Generated Keys Not Requested!

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.

Thanks, brian

© Stack Overflow or respective owner

Related posts about java

Related posts about jdbc