1
package org.hibernate.util;
3
import org.hibernate.AssertionFailure;
5
import java.lang.reflect.Method;
6
import java.lang.reflect.InvocationTargetException;
7
import java.sql.Connection;
8
import java.sql.Statement;
9
import java.sql.PreparedStatement;
10
import java.sql.SQLException;
11
import java.sql.ResultSet;
14
* @author Steve Ebersole
16
public class NamedGeneratedKeysHelper {
17
private NamedGeneratedKeysHelper() {
20
private static final Method PREPARE_STATEMENT_METHOD;
21
private static final Method GET_GENERATED_KEYS_METHOD;
25
PREPARE_STATEMENT_METHOD = Connection.class.getMethod(
27
new Class[] { String.class, String[].class }
29
GET_GENERATED_KEYS_METHOD = Statement.class.getDeclaredMethod(
34
catch ( Exception e ) {
35
throw new AssertionFailure( "could not initialize getGeneratedKeys() support", e );
39
public static PreparedStatement prepareStatement(Connection conn, String sql, String[] columnNames) throws SQLException {
40
Object[] args = new Object[] { sql, columnNames } ;
42
return ( PreparedStatement ) PREPARE_STATEMENT_METHOD.invoke( conn, args );
44
catch ( InvocationTargetException ite ) {
45
if ( ite.getTargetException() instanceof SQLException ) {
46
throw ( SQLException ) ite.getTargetException();
48
else if ( ite.getTargetException() instanceof RuntimeException ) {
49
throw ( RuntimeException ) ite.getTargetException();
52
throw new AssertionFailure( "InvocationTargetException preparing statement capable of returning generated keys (JDBC3)", ite );
55
catch ( IllegalAccessException iae ) {
56
throw new AssertionFailure( "IllegalAccessException preparing statement capable of returning generated keys (JDBC3)", iae );
60
public static ResultSet getGeneratedKey(PreparedStatement ps) throws SQLException {
62
return ( ResultSet ) GET_GENERATED_KEYS_METHOD.invoke( ps, null );
64
catch ( InvocationTargetException ite ) {
65
if ( ite.getTargetException() instanceof SQLException ) {
66
throw ( SQLException ) ite.getTargetException();
68
else if ( ite.getTargetException() instanceof RuntimeException ) {
69
throw ( RuntimeException ) ite.getTargetException();
72
throw new AssertionFailure( "InvocationTargetException extracting generated keys (JDBC3)", ite );
75
catch ( IllegalAccessException iae ) {
76
throw new AssertionFailure( "IllegalAccessException extracting generated keys (JDBC3)", iae );