Skip to content

Instantly share code, notes, and snippets.

@ipolevoy
Created October 9, 2012 19:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ipolevoy/3861026 to your computer and use it in GitHub Desktop.
Save ipolevoy/3861026 to your computer and use it in GitHub Desktop.
PreparedStatement ps = connection.prepareStatement("INSERT INTO users (first_name, email, last_name) VALUES (?, ?, ?)", new String[]{"id"});
ps.setObject(1, "John");
ps.setObject(2, "Doe");
ps.setObject(3, "john@doe.com");
ps.executeUpdate();
ResultSet rs = ps.getGeneratedKeys();
rs.next();
Object id = rs.getObject(1);
System.out.println("Generated Id value: " + id);
System.out.println("Generated Id class: " + id.getClass());
Statement s = connection.createStatement();
ResultSet rs1 = s.executeQuery("select * from users");
rs1.next();
Object realId = rs1.getObject("id");
System.out.println("realId Id value: " + realId);
System.out.println("realId Id class: " + realId.getClass());
@ipolevoy
Copy link
Author

ipolevoy commented Oct 9, 2012

Output:
Generated Id value: 1
Generated Id class: class java.lang.Long
realId Id value: 1
realId Id class: class java.lang.Integer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment