KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > util > GetGeneratedKeysHelper


1 //$Id: GetGeneratedKeysHelper.java,v 1.1 2004/06/03 16:31:30 steveebersole Exp $
2
package org.hibernate.util;
3
4 import java.lang.reflect.InvocationTargetException JavaDoc;
5 import java.lang.reflect.Method JavaDoc;
6 import java.sql.Connection JavaDoc;
7 import java.sql.PreparedStatement JavaDoc;
8 import java.sql.ResultSet JavaDoc;
9 import java.sql.SQLException JavaDoc;
10 import java.sql.Statement JavaDoc;
11
12 import org.hibernate.AssertionFailure;
13
14 /**
15  * @author Gavin King
16  */

17 public final class GetGeneratedKeysHelper {
18
19     private GetGeneratedKeysHelper() {}
20
21     private static final int RETURN_GENERATED_KEYS;
22     private static final Method JavaDoc PREPARE_STATEMENT_METHOD;
23     private static final Method JavaDoc GET_GENERATED_KEYS_METHOD;
24
25     static {
26         try {
27             RETURN_GENERATED_KEYS = Statement JavaDoc.class.getDeclaredField("RETURN_GENERATED_KEYS").getInt(PreparedStatement JavaDoc.class);
28             PREPARE_STATEMENT_METHOD = Connection JavaDoc.class.getMethod( "prepareStatement", new Class JavaDoc[] {String JavaDoc.class, Integer.TYPE} );
29             GET_GENERATED_KEYS_METHOD = Statement JavaDoc.class.getDeclaredMethod("getGeneratedKeys", null);
30         }
31         catch (Exception JavaDoc e) {
32             throw new AssertionFailure("could not initialize getGeneratedKeys() support", e);
33         }
34     }
35
36     public static PreparedStatement JavaDoc prepareStatement(Connection JavaDoc conn, String JavaDoc sql) throws SQLException JavaDoc {
37         Object JavaDoc[] args = new Object JavaDoc[] { sql, new Integer JavaDoc(RETURN_GENERATED_KEYS) } ;
38         try {
39             return (PreparedStatement JavaDoc) PREPARE_STATEMENT_METHOD.invoke(conn, args);
40         }
41         catch (InvocationTargetException JavaDoc ite) {
42             if ( ite.getTargetException() instanceof SQLException JavaDoc ) {
43                 throw (SQLException JavaDoc) ite.getTargetException();
44             }
45             else if ( ite.getTargetException() instanceof RuntimeException JavaDoc ) {
46                 throw (RuntimeException JavaDoc) ite.getTargetException();
47             }
48             else {
49                 throw new AssertionFailure("InvocationTargetException", ite);
50             }
51         }
52         catch (IllegalAccessException JavaDoc iae) {
53             throw new AssertionFailure("IllegalAccessException", iae);
54         }
55     }
56
57     public static ResultSet JavaDoc getGeneratedKey(PreparedStatement JavaDoc ps) throws SQLException JavaDoc {
58         try {
59             return (ResultSet JavaDoc) GET_GENERATED_KEYS_METHOD.invoke(ps, null);
60         }
61         catch (InvocationTargetException JavaDoc ite) {
62             if ( ite.getTargetException() instanceof SQLException JavaDoc ) {
63                 throw (SQLException JavaDoc) ite.getTargetException();
64             }
65             else if ( ite.getTargetException() instanceof RuntimeException JavaDoc ) {
66                 throw (RuntimeException JavaDoc) ite.getTargetException();
67             }
68             else {
69                 throw new AssertionFailure("InvocationTargetException", ite);
70             }
71         }
72         catch (IllegalAccessException JavaDoc iae) {
73             throw new AssertionFailure("IllegalAccessException", iae);
74         }
75     }
76
77 }
78
Popular Tags