1 Reply Latest reply on Apr 6, 2004 8:52 AM by gunzip

    Data Access Object and session bean

    Arup Sarkar Newbie

      Hi,

      I created a session bean, and trying to access data access object, the dao is created via xdoclet tags, when I am running the client the output just prints out the messages in the bean and does not enter the dao at all.

      JBoss 3.2.1/MySql 4.x/Eclipse 3.0 integrated with lomboz.

      I would appreciate if someone could help me. What am I missing.

      Regards
      Arup


      
      
      package zen.com.store.session;
      
      import javax.ejb.SessionBean;
      import javax.ejb.SessionContext;
      
      
      
      
      /**
       * @ejb.bean name="StoreAccess"
       * jndi-name="StoreAccessBean"
       * type="Stateless"
       *
       * @ejb.dao
       * class="zen.com.store.session.StoreAccessDAO"
       * impl-class="zen.com.store.util.dao.StoreAccessDAOImpl"
       *
       * @ejb.resource-ref res-ref-name="jdbc/MySqlDS"
       * res-type="javax.sql.Datasource"
       * res-auth="Container"
       *
       * @jboss.resource-ref res-ref-name="jdbc/MySqlDS"
       * jndi-name="java:/MySqlDS"
       *
       **/
      
      public abstract class StoreAccessBean implements SessionBean {
      
      
       protected SessionContext ctx;
      
      
      /**
       * @ejb.interface-method
       * view-type="remote"
       * @dao.call name="loginUser"
       *
       **/
       public String loginUser(String username, String password){
       System.out.println("Entering StoreAccessBean");
      
       //String userID = StoreAccessDAOImpl.loginUser("ANDY", "PASSWD");
       //System.out.println(userID);
       System.out.println("Leaving StoreAccessBean");
      
       // -start
       // -end
       return null;
       } //loginUser
      
       /**
       * Sets the param context
       * @param javax.ejb.SessionContext the new ctx value
       **/
       public void setSessionContext(SessionContext ctx){
       this.ctx = ctx;
       } //setSessionContext
       /**
       * Unsets the SessionContext
       *
       */
      
       public void unsetSessionContext(){
       this.ctx = null;
       }// unsetSessionContext
      
      
      
      }
      
      


      /***************** dao code **********/
      
      package zen.com.store.util.dao;
      
      import javax.naming.InitialContext;
      import javax.sql.DataSource;
      import java.sql.Connection;
      import java.sql.PreparedStatement;
      import java.sql.ResultSet;
      import java.sql.SQLException;
      
      /**
       * @author asarkar
       *
       * To change the template for this generated type comment go to
       * Window - Preferences - Java - Code Generation - Code and Comments
       */
      public class StoreAccessDAOImpl {
      
       private DataSource jdbcFactory;
      
       public void init(){
       System.out.println("Entering StoreAccessDAOImpl.init()");
       InitialContext c = null;
       if(jdbcFactory == null){
       try{
       c = new InitialContext();
       //jdbcFactory = (DataSource)c.lookup("java:comp/env/jdbc/MySqlDS");
       jdbcFactory = (DataSource)c.lookup("java:/jdbc/MySqlDS");
      
       }catch(Exception e){
       System.out.println("Error in StoreAccessDAOImpl.init()" + e.toString());
       }
       }
       System.out.println("Leaving StoreAccessDAOImpl.init()");
      
       }
      
      
       /**
       *
       */
       public String loginUser(String username, String password){
      
       System.out.println("Entering StoreAccessDAOImpl.loginUser()");
       Connection conn=null;
       PreparedStatement ps=null;
       ResultSet rs=null;
       String userID = null;
       try{
       conn = jdbcFactory.getConnection();
       String queryString = "select userid from storeaccess where username = ? and password = ?";
       ps = conn.prepareStatement(queryString);
       ps.setString(1, username);
       ps.setString(2, password);
       rs = ps.executeQuery();
       boolean result = rs.next();
       if(result){
       userID = rs.getString(1);
       System.out.println("User Id : " + userID);
       }
      
       }catch(SQLException e){
       System.out.println("Error in StoreAccessBean.loginUser()" + e.toString());
       }finally{
       try{
       rs.close();
       ps.close();
       conn.close();
      
       }catch(Exception e){
       System.out.println("Final Error in StoreAccessBean.loginUser()" + e.toString());
       }
       }
       System.out.println("Leaving StoreAccessDAOImpl.loginUser()");
       return userID;
       } //loginUser
      
      }