001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.test.beans;
017:
018: import java.sql.Connection;
019: import java.sql.PreparedStatement;
020: import java.sql.Statement;
021:
022: import javax.ejb.EJBException;
023: import javax.ejb.SessionContext;
024: import javax.naming.InitialContext;
025: import javax.sql.DataSource;
026:
027: /**
028: * @version $Rev: 556384 $ $Date: 2007-07-15 03:00:10 -0700 $
029: */
030: public class DatabaseBean implements javax.ejb.SessionBean {
031:
032: private static final long serialVersionUID = 1L;
033:
034: public SessionContext context;
035: public InitialContext jndiContext;
036:
037: public DatabaseBean() {
038: this .getClass();
039: }
040:
041: public void ejbCreate() throws javax.ejb.CreateException {
042: try {
043: jndiContext = new InitialContext();
044: } catch (Exception e) {
045: throw new EJBException(e.getMessage());
046: }
047: }
048:
049: public void executeQuery(String statement)
050: throws java.sql.SQLException {
051: try {
052:
053: DataSource ds = (DataSource) jndiContext
054: .lookup("java:comp/env/database");
055: Connection con = ds.getConnection();
056:
057: try {
058: PreparedStatement stmt = con
059: .prepareStatement(statement);
060: try {
061: stmt.executeQuery();
062: } finally {
063: stmt.close();
064: }
065: } finally {
066: con.close();
067: }
068: } catch (Exception e) {
069: throw new EJBException("Cannot execute the statement: "
070: + statement + e.getMessage());
071: }
072: }
073:
074: public boolean execute(String statement)
075: throws java.sql.SQLException {
076: boolean retval;
077: Connection con = null;
078: try {
079:
080: DataSource ds = (DataSource) jndiContext
081: .lookup("java:comp/env/database");
082: con = ds.getConnection();
083:
084: Statement stmt = con.createStatement();
085: try {
086: retval = stmt.execute(statement);
087: } finally {
088: stmt.close();
089: }
090:
091: } catch (javax.naming.NamingException e) {
092: // } catch (Exception e){
093: // e.printStackTrace();
094: //throw new RemoteException("Cannot execute the statement: "+statement, e);
095: throw new EJBException("Cannot lookup the Database bean."
096: + e.getMessage());
097: } finally {
098: if (con != null) {
099: con.close();
100: }
101: }
102: return retval;
103: }
104:
105: public void ejbPassivate() {
106: // never called
107: }
108:
109: public void ejbActivate() {
110: // never called
111: }
112:
113: public void ejbRemove() {
114: }
115:
116: public void setSessionContext(javax.ejb.SessionContext cntx) {
117: context = cntx;
118: }
119: }
|