0 Replies Latest reply on Feb 24, 2005 6:43 AM by Abhishek Asthana

    Accessing table through session bean

    Abhishek Asthana Newbie

      Hi,
      I want to print a table using a JSP page. I am using a DAO from a session bean to access the table and send the ResultSet back to the bean.


      This is the code of the DAO interface:
      public interface DataEJBDAO
      {
      public void init();

      public java.sql.ResultSet giveResults() ;

      }


      This is the code of my stateless Bean:
      public abstract class DataEJBBean implements SessionBean {
      protected SessionContext ctx;
      /**
      * @ejb.interface-method
      * view-type="remote"
      * @dao.call name="giveResults"
      **/
      public java.sql.ResultSet giveResults(){
      System.out.println("Entering DataEJBBean.giveResults");
      System.out.println("Exiting DataEJBBean.giveResults");
      return null;
      }
      public void setSessionContext(javax.ejb.SessionContext ctx)
      {
      this.ctx=ctx;
      }

      public void unsetSessionContext()
      {
      this.ctx=null;
      }

      }

      And this is the Implementation of the DAO Interface:

      public class DataEJBDAOImpl implements DataEJBDAO{

      private DataSource jdbcFactory;

      public void init(){
      System.out.println("Entering DataEJBDAOImpl.init()");
      InitialContext c=null;

      if(this.jdbcFactory==null){

      try{
      c=new InitialContext();
      this.jdbcFactory=(DataSource)c.lookup("java:comp/env/jdbc/OracleDS");
      }catch(Exception e){
      System.out.println("Error in DataEJBDAOImpl.init()");
      System.out.println(e);
      }
      }
      System.out.println("Leaving DataEJBDAOImpl.init()");
      }

      public java.sql.ResultSet giveResults()
      {
      Connection con=null;
      PreparedStatement ps=null;
      ResultSet rs=null;

      try{
      con=jdbcFactory.getConnection();
      String query="select employeeName from employeePersonelDetails";
      ps=con.prepareStatement(query);

      rs=ps.executeQuery();
      }catch(SQLException e){
      e.printStackTrace();
      System.out.println("Inside DataEJBDAOImpl.giveResults()"+e);
      }
      finally{
      try{
      rs.close();
      ps.close();
      con.close();
      }catch(Exception e){
      }
      System.out.println("Leaving DataEJBDAOImpl.giveResults()");
      return(rs);
      }
      }
      }

      And this is the Client code:
      /*
      * Created on Feb 24, 2005
      *
      * To change the template for this generated file go to
      * Window>Preferences>Java>Code Generation>Code and Comments
      */
      package myDAO.client;

      import java.rmi.RemoteException;
      import java.util.Hashtable;
      import java.sql.*;
      import javax.ejb.CreateException;
      import javax.naming.InitialContext;
      import javax.naming.NamingException;

      /**
      * @author ram
      *
      * To change the template for this generated type comment go to
      * Window>Preferences>Java>Code Generation>Code and Comments
      */
      public class DAOClient {

      private ResultSet result=null;
      private myDAO.bean.DataEJBHome getHome() throws NamingException {
      return (myDAO.bean.DataEJBHome) getContext().lookup(
      myDAO.bean.DataEJBHome.JNDI_NAME);
      }
      private InitialContext getContext() throws NamingException {
      Hashtable props = new Hashtable();

      props.put(
      InitialContext.INITIAL_CONTEXT_FACTORY,
      "org.jnp.interfaces.NamingContextFactory");
      props.put(InitialContext.PROVIDER_URL, "jnp://127.0.0.1:1099");

      InitialContext initialContext = new InitialContext(props);
      return initialContext;
      }
      public void testBean() {

      try {
      myDAO.bean.DataEJB myBean = getHome().create();

      System.out.println("Getting results");
      //--------------------------------------
      //This is the place you make your calls.
      result=myBean.giveResults();
      try{
      result.next();
      String name=result.getString("employeeName");
      System.out.println(name);
      }catch(SQLException e){
      System.out.println("Sql Exception in result");
      }
      } catch (RemoteException e) {
      e.printStackTrace();
      } catch (CreateException e) {
      e.printStackTrace();
      } catch (NamingException e) {
      e.printStackTrace();
      }
      }

      public static void main(String[] args) {
      DAOClient test = new DAOClient();
      test.testBean();

      }
      }

      But I am getting this error:

      Getting Result:
      java.lang.reflect.UndeclaredThrowableException
      at $Proxy1.giveResults(Unknown Source)
      at myDAO.client.DAOClient.testBean(DAOClient.java:48)
      at myDAO.client.DAOClient.main(DAOClient.java:67)
      Caused by: java.io.NotSerializableException: oracle.jdbc.driver.OracleResultSetImpl
      at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1054)
      at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:278)
      at java.rmi.MarshalledObject.(MarshalledObject.java:92)
      at org.jboss.invocation.jrmp.server.JRMPInvoker.invoke(JRMPInvoker.java:363)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:324)
      at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:261)
      at sun.rmi.transport.Transport$1.run(Transport.java:148)
      at java.security.AccessController.doPrivileged(Native Method)
      at sun.rmi.transport.Transport.serviceCall(Transport.java:144)
      at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:460)
      at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:701)
      at java.lang.Thread.run(Thread.java:534)
      at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:247)
      at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:223)
      at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:133)
      at org.jboss.invocation.jrmp.server.JRMPInvoker_Stub.invoke(Unknown Source)
      at org.jboss.invocation.jrmp.interfaces.JRMPInvokerProxy.invoke(JRMPInvokerProxy.java:135)
      at org.jboss.invocation.InvokerInterceptor.invoke(InvokerInterceptor.java:87)
      at org.jboss.proxy.TransactionInterceptor.invoke(TransactionInterceptor.java:46)
      at org.jboss.proxy.SecurityInterceptor.invoke(SecurityInterceptor.java:45)
      at org.jboss.proxy.ejb.StatelessSessionInterceptor.invoke(StatelessSessionInterceptor.java:100)
      at org.jboss.proxy.ClientContainer.invoke(ClientContainer.java:85)
      ... 3 more
      Exception in thread "main"

      I am using JBoss 3.2.1

      Can anyone please help me.
      Thanks,
      IJ