dain        2004/04/14 00:51:39

  Modified:    modules/itests/src/java/org/openejb/test/beans
                        DatabaseBean.java
  Log:

  Cleaned up jdbc code
  Changed default database to Axion
  Changed itests configuration to be a child of the database configuration
  
  Revision  Changes    Path
  1.2       +72 -52    
openejb/modules/itests/src/java/org/openejb/test/beans/DatabaseBean.java
  
  Index: DatabaseBean.java
  ===================================================================
  RCS file: 
/home/projects/openejb/scm/openejb/modules/itests/src/java/org/openejb/test/beans/DatabaseBean.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- DatabaseBean.java 1 Mar 2004 05:22:13 -0000       1.1
  +++ DatabaseBean.java 14 Apr 2004 04:51:39 -0000      1.2
  @@ -46,77 +46,97 @@
   
   import java.sql.Connection;
   import java.sql.PreparedStatement;
  -import java.sql.ResultSet;
  +import java.sql.SQLException;
   import java.sql.Statement;
  -
  +import javax.ejb.CreateException;
   import javax.ejb.EJBException;
  -import javax.ejb.SessionContext;
  +import javax.ejb.SessionBean;
   import javax.naming.InitialContext;
  +import javax.naming.NamingException;
   import javax.sql.DataSource;
   
  -public class DatabaseBean implements javax.ejb.SessionBean {
  -    
  -    public SessionContext context;
  -    public InitialContext jndiContext;
  -    
  -    public void ejbCreate( ) throws javax.ejb.CreateException{
  -        try{        
  +public class DatabaseBean implements SessionBean {
  +    private InitialContext jndiContext;
  +
  +    public void ejbCreate() throws CreateException {
  +        try {
               jndiContext = new InitialContext();
  -        } catch (Exception e){
  +        } catch (Exception e) {
               throw new EJBException(e.getMessage());
           }
       }
  -    
  -    public void executeQuery(String statement) throws java.sql.SQLException{
  -        try{        
  -
  -        DataSource ds = (DataSource)jndiContext.lookup("java:comp/env/database");
  -        Connection con = ds.getConnection();
  -
  -        PreparedStatement stmt = con.prepareStatement(statement);
  -        ResultSet rs = stmt.executeQuery();
  -        
  -        con.close();
  -        } catch (Exception e){
  -            throw new EJBException("Cannot execute the statement: "+statement+ 
e.getMessage());
  +
  +    public void executeQuery(String statement) throws SQLException {
  +        Connection con = null;
  +        PreparedStatement stmt = null;
  +        try {
  +            DataSource ds = (DataSource) 
jndiContext.lookup("java:comp/env/database");
  +            con = ds.getConnection();
  +
  +            stmt = con.prepareStatement(statement);
  +            stmt.executeQuery();
  +        } catch (NamingException e) {
  +            throw new EJBException("Cannot lookup the Database 
bean."+e.getMessage());
  +        } catch (Exception e) {
  +            throw new EJBException("Cannot execute the statement: " + statement + 
e.getMessage());
  +        } finally {
  +            if (stmt != null) {
  +                try {
  +                    stmt.close();
  +                } catch (SQLException e) {
  +                    e.printStackTrace();
  +                }
  +            }
  +            if (con != null) {
  +                try {
  +                    con.close();
  +                } catch (SQLException e) {
  +                    e.printStackTrace();
  +                }
  +            }
           }
       }
  -    
  -    public boolean execute(String statement) throws java.sql.SQLException{
  -        boolean retval;
  -        Connection con = null;
  -        try{        
  -
  -        DataSource ds = (DataSource)jndiContext.lookup("java:comp/env/database");
  -        con = ds.getConnection();
   
  -        Statement stmt = con.createStatement();
  -        retval = stmt.execute(statement);
  -        
  -        } catch (javax.naming.NamingException e){
  -//        } catch (Exception e){
  -//            e.printStackTrace();
  -            //throw new RemoteException("Cannot execute the statement: "+statement, 
e);
  +    public boolean execute(String statement) throws SQLException {
  +        Connection con = null;
  +        Statement stmt = null;
  +        try {
  +            DataSource ds = (DataSource) 
jndiContext.lookup("java:comp/env/database");
  +            con = ds.getConnection();
  +
  +            stmt = con.createStatement();
  +            return stmt.execute(statement);
  +        } catch (NamingException e) {
               throw new EJBException("Cannot lookup the Database 
bean."+e.getMessage());
           } finally {
  -            if(con!=null) {
  -                con.close();
  +            if (stmt != null) {
  +                try {
  +                    stmt.close();
  +                } catch (SQLException e) {
  +                    e.printStackTrace();
  +                }
  +            }
  +            if (con != null) {
  +                try {
  +                    con.close();
  +                } catch (SQLException e) {
  +                    e.printStackTrace();
  +                }
               }
           }
  -        return retval;
       }
  -    
  -    public void ejbPassivate( ){
  +
  +    public void ejbPassivate() {
           // never called
       }
  -    public void ejbActivate(){
  +
  +    public void ejbActivate() {
           // never called
       }
  -    public void ejbRemove(){
  +
  +    public void ejbRemove() {
       }
  -    
  -    public void setSessionContext(javax.ejb.SessionContext cntx){
  -        context = cntx;
  +
  +    public void setSessionContext(javax.ejb.SessionContext cntx) {
       }
  -} 
  -   
  \ No newline at end of file
  +}
  
  
  

Reply via email to