0 Replies Latest reply on Jan 6, 2006 2:33 AM by guptamala

    Entity Beans with BMP : Unable to delete row in database

    guptamala

      Hi

      I am not able to delete a row in the database, when I execute the remove() method. All the other methods and code and working fine as expected. The code of the Employee Entity EJB and ejb-jar file are as follows:

      /* THE BEAN CLASS */

      package labs.banking;

      import javax.ejb.*;
      import java.rmi.*;
      import java.sql.*;
      import javax.naming.*;
      import javax.sql.DataSource;
      import javax.rmi.PortableRemoteObject;

      public class CustomerBean implements EntityBean
      {
      /*------------------------------------------------------------*/
      //variables
      private EntityContext entityContext;
      private String first, last;
      private double balance;
      private int accountNumber;
      private Connection connection;
      private PreparedStatement insert, delete, findByKey, findByName, update;

      /*------------------------------------------------------------*/
      //methods
      public String getFirstName() throws RemoteException {
      return first;
      }
      /*------------------------------------------------------------*/
      public String getLastName() throws RemoteException {
      return last;
      }
      /*------------------------------------------------------------*/
      public void deposit(double amount) throws RemoteException {
      balance +=amount;
      }
      /*------------------------------------------------------------*/
      public void withdraw(double amount) throws RemoteException {
      balance -=amount;
      }
      /*------------------------------------------------------------*/
      public double getBalance() throws RemoteException {
      return balance;
      }
      /*------------------------------------------------------------*/
      public CustomerPK ejbCreate(int n, String f, String l, double b) throws CreateException {
      try {
      insert.setInt(1,n);
      insert.setDouble(2,b);
      insert.setString(3,f);
      insert.setString(4,l);
      insert.executeUpdate();
      }
      catch(SQLException e) {
      e.printStackTrace();
      throw new CreateException(e.getMessage());
      }

      first = f;
      last = l;
      balance = b;
      accountNumber = n;

      CustomerPK key = new CustomerPK();
      key.accountNumber = n;
      return key;
      }
      /*------------------------------------------------------------*/
      public void ejbPostCreate(int n, String f, String l, double b) {}
      /*------------------------------------------------------------*/
      public CustomerPK ejbFindByPrimaryKey(CustomerPK key) throws FinderException {
      try {
      findByKey.setInt(1, key.accountNumber);
      ResultSet result = findByKey.executeQuery();
      result.next();
      return key;
      }
      catch (SQLException e) {
      e.printStackTrace();
      throw new FinderException(e.getMessage());
      }
      }
      /*------------------------------------------------------------*/
      public CustomerPK ejbFindByName(String first, String last) throws FinderException {
      try {
      findByName.setString(1, first);
      findByName.setString(2, last);
      ResultSet result = findByName.executeQuery();
      result.next();

      CustomerPK key = new CustomerPK();
      key.accountNumber = result.getInt("accountNumber");
      return key;
      }
      catch (SQLException e) {
      e.printStackTrace();
      throw new FinderException(e.getMessage());
      }
      }
      /*------------------------------------------------------------*/
      public void setEntityContext(EntityContext ec) {
      entityContext = ec;
      try {
      System.out.println("Initialising the context");
      Context ctx = new InitialContext();
      System.out.println("Lookup the data source");
      DataSource source = (DataSource)PortableRemoteObject.narrow(ctx.lookup("java:/OracleDS"),DataSource.class);
      System.out.println("Found data source");
      connection = source.getConnection();


      insert = connection.prepareStatement("INSERT into BankCustomers VALUES (?,?,?,?)");
      System.out.println(insert.toString());
      delete = connection.prepareStatement("DELETE FROM BankCustomers WHERE accountNumber = ?");
      findByKey = connection.prepareStatement("SELECT * FROM BankCustomers WHERE accountNumber = ?");
      findByName = connection.prepareStatement("SELECT * FROM BankCustomers WHERE first = ? AND last = ?");
      update = connection.prepareStatement("UPDATE BankCustomers SET balance = ?, first = ?, last = ? WHERE accountNumber = ?");
      System.out.println(update.toString());
      }
      catch(NamingException c) {
      System.out.println("Could not locate db");
      c.printStackTrace();
      throw new EJBException(c.getMessage());
      }
      catch(SQLException e) {
      System.out.println("Problem with db connection");
      e.printStackTrace();
      throw new EJBException(e.getMessage());
      }
      catch(Exception e) {
      System.out.println("Something Failed");
      e.printStackTrace();
      throw new EJBException(e.getMessage());
      }
      }
      /*------------------------------------------------------------*/
      public void unsetEntityContext() {
      try {
      connection.close();
      }
      catch(SQLException e) {
      e.printStackTrace();
      }
      }
      /*------------------------------------------------------------*/
      public void ejbRemove() throws RemoveException {
      System.out.println("Removing " + first + " " + last);
      try {
      System.out.println("accountNumber .." + accountNumber);
      delete.setInt(1, accountNumber);
      System.out.println ("deleted ::::: " + delete.executeUpdate());
      }
      catch(SQLException e) {
      e.printStackTrace();
      throw new RemoveException(e.getMessage());
      }
      }
      /*------------------------------------------------------------*/
      public void ejbPassivate() {
      System.out.println("Passivating ");
      accountNumber = -1;
      }
      /*------------------------------------------------------------*/
      public void ejbActivate() {
      System.out.println("Activating ");
      CustomerPK key = (CustomerPK) entityContext.getPrimaryKey();
      accountNumber = key.accountNumber;
      }
      /*------------------------------------------------------------*/
      public void ejbLoad() throws EJBException {
      try {
      findByKey.setInt(1, accountNumber);
      ResultSet result = findByKey.executeQuery();
      result.next();
      balance = result.getDouble("balance");
      first = result.getString("first");
      last = result.getString("last");
      }
      catch(SQLException e) {
      e.printStackTrace();
      throw new EJBException("Load Failed");
      }
      }
      /*------------------------------------------------------------*/
      public void ejbStore() {
      System.out.println("EJBStore..........");
      try {

      update.setDouble(1, balance);
      update.setString(2, first);
      update.setString(3, last);
      update.setInt(4, accountNumber);
      update.executeUpdate();
      }
      catch(SQLException e) {
      e.printStackTrace();
      throw new EJBException("Store Failed");
      }
      }
      /*------------------------------------------------------------*/

      }


      ejb-jar.xml
      ======

      <?xml version="1.0" encoding="UTF-8"?>

      <!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN' 'http://java.sun.com/dtd/ejb-jar_2_0.dtd'>

      <ejb-jar>
      <display-name>bankingJAR</display-name>
      <enterprise-beans>

      <display-name>CustomerBean</display-name>
      <ejb-name>banking.CustomerHome</ejb-name>
      labs.banking.CustomerHome
      labs.banking.Customer
      <ejb-class>labs.banking.CustomerBean</ejb-class>
      <persistence-type>Bean</persistence-type>
      <prim-key-class>labs.banking.CustomerPK</prim-key-class>
      False
      <security-identity>

      <use-caller-identity></use-caller-identity>
      </security-identity>

      </enterprise-beans>
      <assembly-descriptor>
      <container-transaction>

      <ejb-name>banking.CustomerHome</ejb-name>
      <method-intf>Remote</method-intf>
      <method-name>getLastName</method-name>
      <method-params />

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      <container-transaction>

      <ejb-name>banking.CustomerHome</ejb-name>
      <method-intf>Remote</method-intf>
      <method-name>getBalance</method-name>
      <method-params />

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      <container-transaction>

      <ejb-name>banking.CustomerHome</ejb-name>
      <method-intf>Remote</method-intf>
      <method-name>remove</method-name>
      <method-params />

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      <container-transaction>

      <ejb-name>banking.CustomerHome</ejb-name>
      <method-intf>Remote</method-intf>
      <method-name>deposit</method-name>
      <method-params>
      <method-param>double</method-param>
      </method-params>

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      <container-transaction>

      <ejb-name>banking.CustomerHome</ejb-name>
      <method-intf>Remote</method-intf>
      <method-name>getFirstName</method-name>
      <method-params />

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      <container-transaction>

      <ejb-name>banking.CustomerHome</ejb-name>
      <method-intf>Remote</method-intf>
      <method-name>withdraw</method-name>
      <method-params>
      <method-param>double</method-param>
      </method-params>

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      </assembly-descriptor>
      </ejb-jar>


      Can anyone please help?

      regards
      Mala