1 45 package org.openejb.test.beans; 46 47 import java.sql.Connection ; 48 import java.sql.PreparedStatement ; 49 import java.sql.ResultSet ; 50 import java.sql.Statement ; 51 52 import javax.ejb.EJBException ; 53 import javax.ejb.SessionContext ; 54 import javax.naming.InitialContext ; 55 import javax.sql.DataSource ; 56 57 public class DatabaseBean implements javax.ejb.SessionBean { 58 59 public SessionContext context; 60 public InitialContext jndiContext; 61 62 public void ejbCreate( ) throws javax.ejb.CreateException { 63 try{ 64 jndiContext = new InitialContext (); 65 } catch (Exception e){ 66 throw new EJBException (e.getMessage()); 67 } 68 } 69 70 public void executeQuery(String statement) throws java.sql.SQLException { 71 try{ 72 73 DataSource ds = (DataSource )jndiContext.lookup("java:comp/env/database"); 74 Connection con = ds.getConnection(); 75 76 PreparedStatement stmt = con.prepareStatement(statement); 77 ResultSet rs = stmt.executeQuery(); 78 79 con.close(); 80 } catch (Exception e){ 81 throw new EJBException ("Cannot execute the statement: "+statement+ e.getMessage()); 82 } 83 } 84 85 public boolean execute(String statement) throws java.sql.SQLException { 86 boolean retval; 87 Connection con = null; 88 try{ 89 90 DataSource ds = (DataSource )jndiContext.lookup("java:comp/env/database"); 91 con = ds.getConnection(); 92 93 Statement stmt = con.createStatement(); 94 retval = stmt.execute(statement); 95 96 } catch (javax.naming.NamingException e){ 97 throw new EJBException ("Cannot lookup the Database bean."+e.getMessage()); 101 } finally { 102 if(con!=null) { 103 con.close(); 104 } 105 } 106 return retval; 107 } 108 109 public void ejbPassivate( ){ 110 } 112 public void ejbActivate(){ 113 } 115 public void ejbRemove(){ 116 } 117 118 public void setSessionContext(javax.ejb.SessionContext cntx){ 119 context = cntx; 120 } 121 } 122 | Popular Tags |