KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > cmp > jdbc > keygen > JDBCDB2IdentityValLocalCreateCommand


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.ejb.plugins.cmp.jdbc.keygen;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.PreparedStatement JavaDoc;
26 import java.sql.ResultSet JavaDoc;
27 import java.sql.SQLException JavaDoc;
28 import javax.ejb.EJBException JavaDoc;
29 import org.jboss.ejb.EntityEnterpriseContext;
30 import org.jboss.ejb.plugins.cmp.jdbc.JDBCIdentityColumnCreateCommand;
31 import org.jboss.ejb.plugins.cmp.jdbc.JDBCUtil;
32
33 /**
34  * Create method that uses the identity_val_local() function in DB2 to get
35  * get the ID of the last inserted row, and populate it into the EJB
36  * object being created.
37  *
38  * @author <a HREF="mailto:dwintschel@esports.com">Daniel Wintschel</a>
39  * @version $Revision: 37459 $
40  */

41 public class JDBCDB2IdentityValLocalCreateCommand extends JDBCIdentityColumnCreateCommand {
42     private static final String JavaDoc SQL = "values (identity_val_local())";
43
44     protected int executeInsert(int paramIndex, PreparedStatement JavaDoc ps, EntityEnterpriseContext ctx ) throws SQLException JavaDoc {
45         int rows = ps.executeUpdate();
46         ResultSet JavaDoc results = null;
47         try {
48             Connection JavaDoc conn = ps.getConnection();
49             results = conn.prepareStatement( SQL ).executeQuery();
50             if( !results.next() ) {
51                 throw new EJBException JavaDoc( "identity_val_local() returned an empty ResultSet" );
52             }
53             pkField.loadInstanceResults( results, 1, ctx );
54         } catch( RuntimeException JavaDoc e ) {
55             throw e;
56         } catch( Exception JavaDoc e ) {
57             // throw EJBException to force a rollback as the row has been inserted
58
throw new EJBException JavaDoc( "Error extracting identity_val_local()", e );
59         } finally {
60             JDBCUtil.safeClose( results );
61         }
62         return rows;
63     }
64 }
65
Popular Tags