steve wrote:
DISCLAIMER: I have had intermittent success getting Oracle drivers to properly support getGeneratedKeys(). It seemed like it would work in one release, break in the next, and then be back to working in the very next. So ymmv
No "intermittent success" in my experience. But it's mainly linked to the version of the jdbc drivers, and not the database: I have tested with many different versions of Oracle above the 9.2.0.3, and it always worked when the drivers were at least 10.2.0.1. The problem is that sometimes the developers don't know exactly which version of the driver they're using, for a few reasons:
- they include the jar in their project, but the server has already a driver in its classpath and it uses that one instead.
- Oracle wasn't always very explicit about the version of their driver (no version number, or even wrong version number, in the manifest).
- Some application servers use their own version of the jdbc drivers (Weblogic, to name just one)
This small utility method will tell which driver you're using for a valid connection in parameter and will also tell if your db supports "getGeneratedKeys()":
Code:
import java.sql.*;
public class TestJDBCVersion {
public static void version(Connection pconnection) {
try {
DatabaseMetaData dmd = pconnection.getMetaData();
System.out.println("DriverVersion: [" + dmd.getDriverVersion() + "]");
System.out.println("DriverMajorVersion: [" + dmd.getDriverMajorVersion() + "]");
System.out.println("DriverMinorVersion: [" + dmd.getDriverMinorVersion() + "]");
System.out.println("DriverName: [" + dmd.getDriverName() + "]");
System.out.println("URL: [" + dmd.getURL() + "]");
System.out.println("UserName: [" + dmd.getUserName() + "]");
System.out.println(dmd.getDatabaseProductName());
System.out.println(dmd.getDatabaseProductVersion());
System.out.print("Supports getGeneratedKeys(): ");
if (dmd.supportsGetGeneratedKeys() )
System.out.println("true");
else
System.out.println("false");
String ver;
ver = System.getProperty("java.version");
System.out.println("The JDK version is " + ver);
} catch (SQLException e) {
e.printStackTrace();
}
}
}