java.sql.Statement.getGeneratedKeys()
843854May 29 2002 — edited Mar 21 2007Hi!
I'm using the latest MySQL driver (org.gjt.mm.mysql) and JDK1.4. After an INSERT I want the inserted ID. I found the method Statement.getGeneratedKeys() but if I try the following code I'll get an exception. Is this function not implemented in the MySQL drivers? Or what is wrong?
query = "INSERT ........";
PreparedStatement ps = connection.prepareStatement(query,Statement.RETURN_GENERATED_KEYS); //
try {
result = ps.execute();
ResultSet rs = ps.getGeneratedKeys();
int columns = rs.getMetaData().getColumnCount();
while (rs.next()) {
for (int i=1;i<=columns;i++) {
System.out.println(rs.getMetaData().getColumnName(i)+":"+rs.getString(i));
}
}
rs.close();
rs = null;
} catch (NullPointerException npe) {
error("NullPointerException durch die Query '"+query+"'",npe);
}
ps.close();
ps = null;
Exception in thread "main" java.lang.AbstractMethodError:
org.gjt.mm.mysql.jdbc2 Connection.prepareStatement(Ljava/lang/String;I)Ljava/sql/PreparedStatement;
at de.gateway4m.mp3db.common.database.DataBaseConnection.execute(DataBaseConnection.java:201)
at de.gateway4m.mp3db.importer.MP3File.addToDB(MP3File.java:156)
at de.gateway4m.mp3db.importer.Importer.load(Importer.java:79)
at de.gateway4m.mp3db.importer.Importer.begin(Importer.java:52)
at de.gateway4m.mp3db.importer.Start.main(Start.java:113)
If I use connection.prepareStatement(query) without the auto_incremented_id constant and ask for getGeneratedKeys() I'll get the same exception (but with undefined getGeneratedKeys() method). If I don't call it, anything is OK, but a following SELECT id FROM... is very slow (XX minutes - because the database is very big) and I want avoid this...