0 Replies Latest reply on Oct 8, 2006 10:43 AM by Arklis Zeng

    Emergency for help!

    Arklis Zeng Newbie

      Deveoplement environment:
      JBoss 3.2.5 + jre 1.4.2 + Ejb 2.0 + MySQL 5.0

      Issue: I wrote a BMP entitybean for query products by product nam, but I got the null and 0 values from the BMP entitybean with not error message in JBoss container. My code as follow.

      1.package com.jatosoft.ejb.bmps;

      import java.rmi.RemoteException;

      import javax.ejb.EJBObject;

      /**
      * @author Administrator
      * write a remote interface for client sides using
      */
      public interface Product extends EJBObject {
      public int getProductId()throws RemoteException;
      public double getPrice()throws RemoteException;
      public String getProductName()throws RemoteException;
      public String getDescription()throws RemoteException;
      }

      2.package com.jatosoft.ejb.bmps;

      import java.rmi.RemoteException;
      import java.util.Enumeration;

      import javax.ejb.CreateException;
      import javax.ejb.EJBHome;
      import javax.ejb.FinderException;

      /**
      * @author Administrator write a remote home interface to create an remote
      * interface for client sides call
      */
      public interface ProductHome extends EJBHome {
      public Product create(int productId, String productName,
      String description, double price) throws RemoteException,
      CreateException;

      public Product findByPrimaryKey(ProductPK key) throws RemoteException,
      FinderException;

      public Enumeration findByName(String name) throws RemoteException,
      FinderException;
      }


      3.package com.jatosoft.ejb.bmps;

      import java.io.Serializable;

      public class ProductPK implements Serializable {
      public String productId;

      public ProductPK() {
      // TODO Auto-generated constructor stub
      }

      public ProductPK(String productId) {
      this.productId = productId;
      }

      public boolean equals(Object o){
      return ((ProductPK)o).productId.equals(productId);
      }

      public int hashCode(){
      return productId.hashCode();
      }

      public String toString(){
      return productId;
      }
      }

      4. package com.jatosoft.ejb.bmps;

      import java.rmi.RemoteException;
      import java.sql.Connection;
      import java.sql.DriverManager;
      import java.sql.PreparedStatement;
      import java.sql.ResultSet;
      import java.sql.SQLException;
      import java.util.Enumeration;
      import java.util.Vector;

      import javax.ejb.CreateException;
      import javax.ejb.EJBException;
      import javax.ejb.EntityBean;
      import javax.ejb.EntityContext;
      import javax.ejb.FinderException;
      import javax.ejb.ObjectNotFoundException;
      import javax.ejb.RemoveException;
      import javax.naming.Context;
      import javax.naming.InitialContext;
      import javax.naming.NamingException;

      public class ProductBean implements EntityBean {
      private EntityContext ctx;

      int productId;

      String productName;

      String description;

      double price;

      public ProductPK ejbCreate(int productId, String productName,
      String description, double price) throws RemoteException,
      CreateException {
      this.dbgPrint("ejbCreate()");
      //receive the parameter from client sides
      this.productId = productId;
      this.productName = productName;
      this.description = description;
      this.price = price;
      //connect with database
      Connection con = null;
      PreparedStatement ps = null;
      con = this.getConnection();
      //insert values into the database
      String sql = "insert into products(productId,productName,description,price) values"
      + "(?,?,?,?)";
      try {
      ps = con.prepareStatement(sql);
      ps.setInt(1,this.productId);
      ps.setString(2,this.productName);
      ps.setString(3,this.description);
      ps.setDouble(4,this.price);
      ps.executeUpdate();

      } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }finally{
      if(ps != null){
      try {
      ps.close();
      if (con != null)
      con.close();
      } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }
      }
      }

      return new ProductPK(Integer.toString(productId));
      }

      public void ejbPostCreate(int productId, String productName,
      String description, double price) throws RemoteException,
      CreateException {
      this.dbgPrint("ejbPostCreate()");
      }

      /**
      * @param primaryKey
      * @return
      * @throws RemoteException
      * @throws FinderException
      * implement the findByPrimaryKey() method in the remote home interface
      */
      public ProductPK ejbFindByPrimaryKey(ProductPK primaryKey)throws RemoteException,FinderException{
      this.dbgPrint("ejbFindByPrimaryKey()");
      Connection con = null;
      PreparedStatement ps = null;
      ResultSet rs = null;
      String sql = "select productName from products where productId=?";
      int productId = Integer.parseInt(primaryKey.productId);
      con = this.getConnection();
      try {
      ps = con.prepareStatement(sql);
      ps.setInt(1, productId);
      rs = ps.executeQuery();

      if (rs.next()){
      rs.close();
      ps.close();
      con.close();
      return primaryKey;
      }
      } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      this.dbgPrint(e.toString());
      }finally{
      try{
      if (rs != null)
      rs.close();
      if (ps != null)
      ps.close();
      if (con != null)
      con.close();

      }catch(Exception e){
      e.printStackTrace();
      }
      }
      throw new ObjectNotFoundException();
      }

      /**
      * @param name
      * @return
      * @throws RemoteException
      * @throws FinderException
      * implement findByName() method in the remote home interface
      */
      public Enumeration ejbFindByName(String name)throws RemoteException,FinderException{
      this.dbgPrint("ejbFindByName()");
      Vector products = new Vector();
      Connection con = null;
      PreparedStatement ps = null;
      ResultSet rs = null;
      try{
      String sql = "select productId from products where productName=?";
      con = this.getConnection();
      ps = con.prepareStatement(sql);
      ps.setString(1, name);
      rs = ps.executeQuery();
      while(rs.next()){
      int productId = rs.getInt(1);
      products.addElement(new ProductPK(Integer.toString(productId)));
      }
      }catch(Exception e){
      this.dbgPrint(e.toString());
      }finally{
      try{
      if (rs != null)
      rs.close();
      if (ps != null)
      ps.close();
      if (con != null)
      con.close();
      }catch(Exception e){
      this.dbgPrint(e.toString());
      }
      }
      return products.elements();
      }


      /**
      * @return
      * retrieve the connection with database
      */
      protected Connection getConnection(){
      String dbUrl = null;
      String userName = null;
      String password = null;
      Context initialContext = null;
      Context environment = null;
      Connection connection = null ;

      try {
      initialContext = new InitialContext();
      environment = (Context) initialContext.lookup("java:comp/env");
      dbUrl = (String) environment.lookup("dbUrl");
      userName = (String)environment.lookup("dbUserName");
      password = (String)environment.lookup("dbPassword");
      } catch (NamingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      this.dbgPrint(e.toString());
      }
      try {
      connection = DriverManager.getConnection(dbUrl,userName,password);
      } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      this.dbgPrint(e.toString());
      }
      return connection;
      }

      public void ejbActivate() throws EJBException, RemoteException {
      // TODO Auto-generated method stub
      dbgPrint("ejbActivate()");

      }

      public void ejbLoad() throws EJBException, RemoteException {
      // TODO Auto-generated method stub
      dbgPrint("ejbLoad()");
      Connection con = null;
      PreparedStatement ps = null;
      ResultSet rs = null;
      try{
      String sql = "select productName,description, price from products where productId=?";
      con = this.getConnection();
      ps = con.prepareStatement(sql);
      ps.setInt(1, this.productId);
      rs = ps.executeQuery();

      if (rs.next()){
      this.productName = rs.getString(1);
      this.description = rs.getString(2);
      this.price = rs.getDouble(3);
      }
      }catch(Exception e){
      this.dbgPrint(e.toString());
      }finally{
      try{
      if (rs != null)
      rs.close();
      if (ps != null)
      ps.close();
      if (con != null)
      con.close();
      }catch(Exception e){
      this.dbgPrint(e.toString());
      }
      }
      }

      public void ejbPassivate() throws EJBException, RemoteException {
      // TODO Auto-generated method stub
      dbgPrint("ejbPassivate()");
      }

      public void ejbRemove() throws RemoveException, EJBException,
      RemoteException {
      // TODO Auto-generated method stub
      dbgPrint("ejbRemove()");
      Connection con = null;
      PreparedStatement ps = null;
      try{
      String sql = "delete from products where productId=?";
      ProductPK key = (ProductPK)ctx.getPrimaryKey();
      con = this.getConnection();
      int productId = Integer.parseInt(key.productId);
      ps = con.prepareStatement(sql);
      ps.setInt(1, productId);
      ps.executeUpdate();
      }catch(Exception e){
      this.dbgPrint(e.toString());
      }finally{
      try{
      if (ps != null)
      ps.close();
      if (con != null)
      con.close();
      }catch(Exception e){
      this.dbgPrint(e.toString());
      }
      }
      }

      public void ejbStore() throws EJBException, RemoteException {
      // TODO Auto-generated method stub
      Connection con = null;
      PreparedStatement ps = null;
      try{
      String sql = "update products set productName=?, description=?, price=?"
      + " where productId=?";
      con = this.getConnection();
      ps = con.prepareStatement(sql);
      ps.setString(1, this.productName);
      ps.setString(2, this.description);
      ps.setDouble(3,this.price);
      ps.setInt(4, productId); //don not forget this parameter???

      ps.executeUpdate();
      }catch(Exception e){
      this.dbgPrint(e.toString());
      }finally{
      try{
      if (ps != null)
      ps.close();
      if (con != null)
      con.close();
      }catch(Exception e){
      this.dbgPrint(e.toString());
      }
      }
      }

      public void setEntityContext(EntityContext arg0) throws EJBException,
      RemoteException {
      // TODO Auto-generated method stub
      this.ctx = arg0;
      dbgPrint("setEntityContext()");

      }

      public void unsetEntityContext() throws EJBException, RemoteException {
      // TODO Auto-generated method stub
      this.ctx = null;
      dbgPrint("unsetEntityContext()");
      }

      public String getDescription() {
      dbgPrint("getDescription()");
      return description;
      }

      public double getPrice() {
      dbgPrint("getPrice()");
      return price;
      }

      public int getProductId() {
      dbgPrint("getProductId()");
      return productId;
      }

      public String getProductName() {
      dbgPrint("getProductName()");
      return productName;
      }

      /**
      * @param temp
      * write a debug method for later use
      */
      private void dbgPrint(String temp) {
      System.out.println(temp);
      }

      public void setDescription(String description) {
      this.description = description;
      }

      public void setPrice(double price) {
      this.price = price;
      }

      public void setProductId(int productId) {
      this.productId = productId;
      }

      public void setProductName(String productName) {
      this.productName = productName;
      }

      }

      5. Client test code is as following
      package com.jatosoft.ejb.bmp.tester;

      import java.util.Enumeration;
      import java.util.Properties;

      import javax.naming.Context;
      import javax.naming.InitialContext;
      import javax.rmi.PortableRemoteObject;

      import com.jatosoft.ejb.bmps.Product;
      import com.jatosoft.ejb.bmps.ProductHome;

      /**
      * @author Administrator
      * write a bmp entitybean tester
      */
      public class BMPTester {
      public static void main(String[] args){
      BMPTester tester = new BMPTester();
      tester.testBMP();
      }

      /**
      * write e method for tester ejbs
      */
      public void testBMP(){
      //initial the jndi
      Properties properties = new Properties();
      properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
      properties.put(Context.PROVIDER_URL, "localhost:1099");

      try{
      //1. get the initial context
      InitialContext jndiContext = new InitialContext(properties);
      //2. get a reference to the bean
      Object ref = jndiContext.lookup("BMPProduct");
      this.dbgPrint("Got reference");

      //3. get a refernce from this to the bean's home interface
      ProductHome home = (ProductHome)PortableRemoteObject.narrow(ref,ProductHome.class);
      //4. create an interest object from the home interface
      // home.create(16, "Franklin Spring Water", "400ml", 2.25);
      // home.create(17, "Franklin Spring Water", "600ml", 3.25);
      // home.create(18,"Choco Bar", "Chocoloate Bar 200g", 2.96);
      // home.create(19,"Timtim Biscuit","Biscuit w. mint flavor, 300g",9.25);

      Product product = home.create(20,"Supermine","Instant Noodle",1.05);
      product.remove();

      Enumeration enumeration = home.findByName("Franklin Spring Water");

      while(enumeration.hasMoreElements()){
      this.dbgPrint("The enumeration has more elements is " + enumeration.hasMoreElements());
      product = (Product) enumeration.nextElement();
      this.dbgPrint("The product is " + product);

      this.dbgPrint("Id: " + product.getProductId());
      this.dbgPrint("Product Name: " + product.getProductName());
      this.dbgPrint("Description: " + product.getDescription());
      this.dbgPrint("Price: " + product.getPrice());
      }

      }catch(Exception e){

      }
      }

      /**
      * @param temp
      * write a debug method for later use
      */
      private void dbgPrint(String temp){
      System.out.println(temp);
      }
      }


      What's wrong with my code. Please help me!
      Thank you!

      Arklis Zeng