[jboss-user] [Beginners Corner] - Re: ERROR [NamingService] Could not start on port 1099

vkokodyn do-not-reply at jboss.com
Mon Dec 4 11:21:42 EST 2006


I have ejbCreate method in my bean

==============

package com.beans.ejb;

import java.sql.*; 
import java.util.Enumeration; 
import java.util.Vector; 
import java.rmi.RemoteException; 
import javax.ejb.EntityBean; 
import javax.ejb.EntityContext; 
import javax.ejb.CreateException; 
import javax.ejb.FinderException; 
import javax.ejb.ObjectNotFoundException; 
import javax.ejb.*;
import com.beans.interfaces.ProductPK;
import javax.naming.InitialContext; 
import javax.naming.Context; 
import javax.naming.NamingException; 
import javax.sql.DataSource;

/**
 * XDoclet-based BMP entity bean.
 * 
 * To generate EJB related classes using XDoclet:
 *
 *		- Add Standard EJB module to XDoclet project properties
 *		- Customize XDoclet configuration
 *		- Run XDoclet
 * 
 * Below are the xdoclet-related tags needed for this EJB.
 *
 * @ejb.bean name="Product"
 *           display-name="Name for Product"
 *           description="Description for Product"
 *           jndi-name="ejb/Product"
 *           type="BMP"
 *           view-type="both"
 */
public class ProductBean implements EntityBean {

	/** The entity context */
	private EntityContext context;

	int UID; 
	String pName; 
	int pQuantity ; 
	String pPrice; 
	
	public int getUID() {
		System.out.println("getUID"); 
		return UID; 
	} 
	
	public String getpName() {
		System.out.println("getpName"); 
		return pName; 
	} 

	public int getpQuantity() {
		System.out.println("getpQuantity"); 
		return pQuantity; 
	}   
	
	public String getpPrice() {
	    System.out.println("getpPrice"); 
	    return pPrice; 
	} 

	public ProductBean() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * There are zero or more ejbCreate(...) methods, whose signatures match
	 * the signatures of the create(...) methods of the entity bean?s home interface.
	 * The container invokes an ejbCreate(...) method on an entity bean instance
	 * when a client invokes a matching create(...) method on the entity bean?s
	 * home interface.
	 * 
	 * The entity bean provider?s responsibility is to initialize the instance in the ejbCreate<
	 * METHOD>(...) methods from the input arguments, using the get and set accessor
	 * methods, such that when the ejbCreate(...) method returns, the persistent
	 * representation of the instance can be created. 
	 * 
	 * The entity bean provider must not attempt to modify the values of cmr-fields in an ejbCreate<
	 * METHOD(...) method; this should be done in the ejbPostCreate<METHOD(...) method instead.
	 * 
	 * The entity object created by the ejbCreate method must have a unique primary
	 * key. This means that the primary key must be different from the primary keys of all the existing
	 * entity objects within the same home. However, it is legal to reuse the primary key of a previously
	 * removed entity object. The implementation of the bean provider?s ejbCreate<
	 * METHOD>(...) methods should be coded to return a null.
	 * 
	 * An ejbCreate(...) method executes in the transaction context determined by
	 * the transaction attribute of the matching create(...) method. 
	 * The database insert operations are performed by the container within the same
	 * transaction context after the Bean Provider?s ejbCreate(...) method completes.
	 *
	 * @throws CreateException Thrown if method fails due to system-level error.
	 * 
	 * @throws CreateException
	 *
	 * @ejb.create-method
	 */
	public ProductPK ejbCreate( int UID, String pName, int pQuantity, 
			String pPrice)	throws CreateException {
		System.out.println("ejbCreate"); 
	    this.UID = UID; 
	    this.pName = pName; 
	    this.pQuantity = pQuantity; 
	    this.pPrice = pPrice; 
	    Connection connect = null; 
	    PreparedStatement ps = null; 
	    try {
	      String sql = "INSERT INTO ProductInfo" + 
	        " (UID, pName, pQuantity, pPrice)" + 
	        " VALUES" + 
	        " (?, ?, ?, ?)"; 
	      connect = getConnection(); 
	      ps = connect.prepareStatement(sql); 
	      ps.setInt(1, UID); 
	      ps.setString(2, pName); 
	      ps.setInt(3, pQuantity); 
	      ps.setString(4, pPrice); 
	      ps.executeUpdate(); 
	    } 
	    catch (SQLException e) {
	      System.out.println(e.toString()); 
	    } 
	    finally {
	      try {
	        if (ps!=null) 
	          ps.close(); 
	        if (connect!=null) 
	          connect.close(); 
	      } 
	      catch (SQLException e) {
	      } 
	    } 
	    return new ProductPK(UID); 
		
	}

	/**
	 * For each ejbCreate(...) method, there is a matching ejbPostCreate<
	 * METHOD>(...) method that has the same input parameters but whose return type is
	 * void. The container invokes the matching ejbPostCreate(...) method on
	 * an instance after it invokes the ejbCreate(...) method with the same arguments.
	 * The instance can discover the primary key by calling getPrimaryKey() on its
	 * entity context object. 
	 * 
	 * The entity object identity is available during the ejbPostCreate(...)
	 * method. The instance may, for example, obtain the component interface of the associated entity
	 * object and pass it to another enterprise bean as a method argument.
	 * 
	 * The entity Bean Provider may use the ejbPostCreate(...) to set the values
	 * of cmr-fields to complete the initialization of the entity bean instance.
	 * An ejbPostCreate(...) method executes in the same transaction context as
	 * the previous ejbCreate(...) method.
	 * 
	 * @throws CreateException Thrown if method fails due to system-level error.
	 */
	public void ejbPostCreate(int UID, String pName, int pQuantity
			, String pPrice) 
	throws CreateException {
		System.out.println("ejbPostCreate");
	}

	 public ProductPK ejbFindByPrimaryKey(ProductPK primaryKey) 
	    throws RemoteException, FinderException {
	    System.out.println("ejbFindByPrimaryKey"); 
	    Connection connect = null; 
	    PreparedStatement ps = null; 
	    ResultSet rs = null; 
	    try {
	      String sql = "SELECT pName" + 
	        " FROM ProductInfo" + 
	        " WHERE UID=?"; 
	      int UID = primaryKey.UID; 
	      connect = getConnection(); 
	      ps = connect.prepareStatement(sql); 
	      ps.setInt(1, UID); 
	      rs = ps.executeQuery(); 
	      if (rs.next()) {
	        rs.close(); 
	        ps.close(); 
	        connect.close(); 
	        return primaryKey; 
	      } 
	    } 
	    catch (SQLException e) {
	      System.out.println(e.toString()); 
	    } 
	    finally {
	      try {
	        if (rs!=null) 
	          rs.close(); 
	        if (ps!=null) 
	          ps.close(); 
	        if (connect!=null) 
	          connect.close(); 
	      } 
	      catch (SQLException e) {
	      } 
	    } 
	    throw new ObjectNotFoundException(); 

	  } 

	 public Enumeration ejbFindBypName(String pName) 
	    throws RemoteException, FinderException {
	    System.out.println("ejbFindBypName"); 
	    Vector products = new Vector(); 
	    Connection connect = null; 
	    PreparedStatement ps = null; 
	    ResultSet rs = null;   
	    try {
	      String sql = "SELECT UID " + 
	        " FROM ProductInfo" + 
	        " WHERE pName=?"; 
	      connect = getConnection(); 
	      ps = connect.prepareStatement(sql); 
	      ps.setString(1, pName); 
	      rs = ps.executeQuery(); 
	      while (rs.next()) {
	        int UID = rs.getInt(1); 
	        products.addElement(new ProductPK(UID)); 
	      } 
	    } 
	    catch (SQLException e) {
	      System.out.println(e.toString()); 
	    } 
	    finally {
	      try {
	        if (rs!=null) 
	          rs.close(); 
	        if (ps!=null) 
	          ps.close(); 
	        if (connect!=null) 
	          connect.close(); 
	      } 
	      catch (SQLException e) {
	      } 
	    } 
	    return products.elements(); 
	  } 
 
	public void ejbActivate() throws EJBException, RemoteException {
		// TODO Auto-generated method stub
		System.out.println("ejbActivate");
	}

	public void ejbLoad() throws EJBException, RemoteException {
		// TODO Auto-generated method stub
		System.out.println("ejbLoad"); 
	    Connection con = null; 
	    PreparedStatement ps = null; 
	    ResultSet rs = null; 
	    try {
	      String sql = "SELECT pName, pPrice" + 
	        " FROM ProductInfo" + 
	        " WHERE UID=?"; 
	      con = getConnection(); 
	      ps = con.prepareStatement(sql); 
	      ps.setInt(1, this.UID); 
	      rs = ps.executeQuery(); 
	      if (rs.next()) {
	        this.pName = rs.getString(1); 
	        this.pQuantity = rs.getInt(2); 
	        this.pPrice = rs.getString(3); 
	      } 
	    } 
	    catch (SQLException e) {
	      System.out.println(e.toString()); 
	    } 
	    finally {
	      try {
	        if (rs!=null) 
	          rs.close(); 
	        if (ps!=null) 
	          ps.close(); 
	        if (con!=null) 
	        con.close(); 
	      } 
	      catch (SQLException e) {  
	      } 
	    } 
	  
	}

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

	public void ejbRemove()	throws RemoveException,
		EJBException, RemoteException {
		// TODO Auto-generated method stub
		System.out.println("ejbRemove"); 
	    Connection connect = null; 
	    PreparedStatement ps = null; 
	    try {
	      String sql = "DELETE FROM ProductInfo" + 
	        " WHERE UID=?"; 
	      ProductPK key = (ProductPK) context.getPrimaryKey(); 
	      int UID = key.UID; 
	      connect = getConnection(); 
	      ps = connect.prepareStatement(sql); 
	      ps.setInt(1, UID); 
	      ps.executeUpdate(); 
	    } 
	    catch (SQLException e) {
	      System.out.println(e.toString()); 
	    } 
	    finally {
	      try {
	        if (ps!=null) 
	          ps.close(); 
	        if (connect!=null) 
	          connect.close(); 
	      } 
	      catch (SQLException e) {
	      } 
	    } 

	}

	public void ejbStore() throws EJBException, RemoteException {
		// TODO Auto-generated method stub
		System.out.println("ejbStore"); 
	    Connection connect = null; 
	    PreparedStatement ps = null; 
	    try {
	      String sql = "UPDATE ProductInfo" + 
	        " SET pName=?, pQuantity=?, pPrice=?" + 
	        " WHERE UID=?"; 
	      ProductPK key = (ProductPK) context.getPrimaryKey(); 
	      int UID = key.UID; 
	      connect = getConnection(); 
	      ps = connect.prepareStatement(sql); 
	      ps.setString(1, this.pName); 
	      ps.setInt(2, this.pQuantity); 
	      ps.setString(3, this.pPrice); 
	      ps.setInt(4, UID); 
	      ps.executeUpdate(); 
	    } 
	    catch (SQLException e) {
	      System.out.println(e.toString()); 
	    } 
	    finally {
	      try {
	        if (ps!=null) 
	          ps.close(); 
	        if (connect!=null) 
	          connect.close(); 
	      } 
	      catch (SQLException e) {
	      } 
	    } 

	}

	/**
	 * Set the associated entity context. The container calls this method 
	 * after the instance creation. The entity bean must not attempt to 
	 * access its persistent state and relationships using the accessor 
	 * methods during this method. 
	 *
	 * The enterprise bean instance should store the reference to the context 
	 * object in an instance variable. 
	 * 
	 * This method is called with no transaction context. 
	 * 
	 * @throws EJBException Thrown if method fails due to system-level error.
	 */
	public void setEntityContext(EntityContext newContext) throws EJBException {
		System.out.println("setEntityContext");
		context = newContext;
	}

	/**
	 * Unset the associated entity context. A container invokes this method 
	 * before terminating the life of the instance. The entity bean must not 
	 * attempt to access its persistent state and relationships using the 
	 * accessor methods during this method. 
	 * 
	 * This method is called with no transaction context. 
	 * 
	 * @throws EJBException Thrown if method fails due to system-level error.
	 */
	public void unsetEntityContext() throws EJBException {
		context = null;
	}
	
	public Connection getConnection() {
		   String DBDriver = null;
		   String DBUrl = null;
		   String DBUserName = null;
		   String DBPassword = null;
		   Context initialContext;
		   Context environment;
		   Connection connect = null;
		   
		   DBUrl      = "jdbc:odbc:EJB";
		   DBUserName = "";
		   DBPassword = "";
		   try {
				DBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
				Class.forName(DBDriver).newInstance();
				connect = DriverManager.getConnection(DBUrl, DBUserName, DBPassword);
				System.out.println("Connect");
		   } 
		   catch(Exception e) {
				System.out.println(e);			
				//return;
		   }
		   return connect;
	}

}


View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3991036#3991036

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3991036



More information about the jboss-user mailing list