1 Reply Latest reply on Aug 23, 2001 10:11 AM by manu

    Business Method in CMP

    keerthikm

      Hello Gurus

      I have a CMP to be deployed on JBOSS. I want to have all the CMP features and also have a direct SQL query Execution.

      I want to have a method which would run a SQL query on the databse and return me a collection.

      the code snippet of the CMP bean is below,

       import java.rmi.RemoteException;
       import javax.ejb.*;
       import java.util.*;
       import java.sql.*;
       import javax.sql.*;
       import javax.naming.*;
      
       public class ProjectBean implements EntityBean {
       public String Name;
       public String Id;
       public float Netamount;
       public String Client;
       private EntityContext context;
       public String getName() throws RemoteException{
       return Name;
       }
       public void setName(String Name) throws RemoteException{
       this.Name = Name;
       }
       public String getId() throws RemoteException{
       return Id;
       }
       public void setId(String Id) throws RemoteException{
       this.Id = Id;
       }
       public float getNetamount() throws RemoteException{
       return Netamount;
       }
       public void setNetamount(float Netamount) throws RemoteException{
       this.Netamount = Netamount;
       }
       public String getClient() throws RemoteException{
       return Client;
       }
       public void setClient(String Client) throws RemoteException{
       this.Client = Client;
       }
       public Collection executeSQL(String SQL) throws Exception {
       String dbName = "java:comp/env/jdbc/DB";
       InitialContext ic = new InitialContext();
       DataSource ds = (DataSource) ic.lookup(dbName);
       Connection con = ds.getConnection();
       Statement stmt = con.createStatement ();
       ResultSet rs = stmt.executeQuery("select * from projectbean");
       ArrayList a = new ArrayList();
      
       while (rs.next()) {
       String id = rs.getString(1);
       a.add(id);
       }
      
       stmt.close();
       return a;
      
       }
       public String ejbCreate(String Name,String Id,float Netamount,String Client) throws RemoteException, CreateException{
       this.Name = Name;
       this.Id = Id;
       this.Netamount = Netamount;
       this.Client = Client;
       return null;
       }
       public void setEntityContext(EntityContext context) {
       this.context = context;
      }
       public void ejbActivate() { }
       public void ejbPassivate() { }
       public void ejbRemove() { }
       public void ejbLoad() { }
       public void ejbStore() { }
       public void unsetEntityContext() { }
       public void ejbPostCreate(String Name,String Id,float Netamount,String Client) { }
       }


      1. Will this work ?
      2. Can I optimise it in anyway ?
      3. How doi keep this generic to be deployed in any j2ee server?

      Please help me

      Thanx in advance
      Keerthi