package  ren.welcome.EntityBean.sportBean.bmp;import javax.ejb.*;
import javax.naming.*;
import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.sql.DataSource;
import java.util.*;
import ren.welcome.EntityBean.sportBean.BaseClass;public class SportTeamEJB extends BaseClass implements EntityBean
{
  public String sport;
  public String nickName;
  public String ownerName;
  public String franchisePlayer;  EntityContext ctx;  public SportTeamPK ejbCreate(String sport, String nickName) throws CreateException {
    this.sport = sport;
    this.nickName = nickName;
    ownerName = null;
    franchisePlayer = null;    Connection con = null;
    try
    {
      con = getConnection(); // this method is detailed later
      PreparedStatement statement =
        con.prepareStatement ("INSERT INTO SPORTSTEAMS (SPORT, NICKNAME) " +
                              "VALUES (?, ?)");
      statement.setString(1, sport);
      statement.setString(2, nickName);
      if (statement.executeUpdate() != 1)
      {
        throw new CreateException("Failed to create sports team.");
      }
    }
    catch (SQLException sqle)
    {
      throw new EJBException(sqle);
    }
    finally
    {
      try
      {
        if (con != null)
        {
          con.close();
        }
      }
      catch (SQLException sqle)
      {}
    }
    return new SportTeamPK(sport, nickName);
  }  public void ejbPostCreate(String key, String relatedData) {}  public SportTeamPK ejbCreate(String sport, String nickName, String ownerName, String franchisePlayer) throws javax.ejb.CreateException {
    this.sport = sport;
    this.nickName = nickName;
    this.ownerName = ownerName;
    this.franchisePlayer = franchisePlayer;    Connection con = null;
    try {
      con = getConnection();
      PreparedStatement statement =
        con.prepareStatement("INSERT INTO SPORTSTEAMS (SPORT, NICKNAME, " +
                             "OWNERNAME, FRANCHISEPLAYER ) VALUES " +
                             "(?, ?, ?, ?)");
      statement.setString(1, sport);
      statement.setString(2, nickName);
      statement.setString(3, ownerName);
      statement.setString(4, franchisePlayer);
      if (statement.executeUpdate() != 1) {
        throw new CreateException("Failed to create sports team.");
      }
    } catch (SQLException sqle) {
      throw new EJBException(sqle);
    }
    finally {
      try {
        if (con != null) {
          con.close();
        }
      } catch (SQLException sqle) {}
    }    return new SportTeamPK(sport, nickName);
  }public void ejbPostCreate(String sport, String nickName, String ownerName, String franchisePlayer) {}
public void setEntityContext(EntityContext ctx)
{
    this.ctx = ctx;
}

解决方案 »

  1.   

    public void unsetEntityContext ()
    {
      ctx = null;
    }public void ejbLoad()
    {
       SportTeamPK primaryKey = (SportTeamPK) ctx.getPrimaryKey();
       Connection con = null;
       try
       {
          con = getConnection();
          PreparedStatement statement =
                con.prepareStatement("SELECT OWNERNAME, FRANCHISEPLAYER " +
                                     "FROM SPORTSTEAMS WHERE SPORT = ? " +
                                     "AND NICKNAME = ? ");
          statement.setString(1, primaryKey.getSport());
          statement.setString(2, primaryKey.getNickName());      ResultSet resultSet = statement.executeQuery();
          if (!resultSet.next())
          {
            throw new EJBException("Object not found.");
          }
          sport = primaryKey.getSport();
          nickName = primaryKey.getNickName();
          ownerName = resultSet.getString(1);
          franchisePlayer = resultSet.getString(2);
          resultSet.close();
          statement.close();
        }
        catch (SQLException sqle)
        {
          throw new EJBException(sqle);
        }
        finally
        {
          try
          {
            if (con != null)
            {
              con.close();
            }
          }
          catch (SQLException sqle)
          {}
        }
      }
      public void ejbStore()
      {
        Connection con = null;
        try {
          con = getConnection();
          PreparedStatement statement =
                  con.prepareStatement("UPDATE SPORTSTEAMS SET OWNERNAME=?, " +
                                       "FRANCHISEPLAYER=? WHERE SPORT = ? " +
                                       "AND NICKNAME = ? ");
          statement.setString(1, ownerName);
          statement.setString(2, franchisePlayer);
          statement.setString(3, sport);
          statement.setString(4, nickName);      if (statement.executeUpdate() != 1) {
            throw new EJBException("Failed to save object state.");
          }
          statement.close();
        } catch (SQLException sqle) {
          throw new EJBException(sqle);
        } finally {
          try {
            if (con != null) {
              con.close();
            }
          } catch (SQLException sqle) {}
        }
      }  public void ejbRemove() throws javax.ejb.RemoveException {
        Connection con = null;
        try {
          con = getConnection();
          PreparedStatement statement =
                  con.prepareStatement("DELETE FROM SPORTSTEAMS " +
                                       "WHERE SPORT = ? AND NICKNAME = ? ");
          statement.setString(1, sport);
          statement.setString(2, nickName);      if (statement.executeUpdate() != 1) {
            throw new EJBException("Failed to remove object.");      }
          statement.close();
        } catch (SQLException sqle) {
          throw new EJBException(sqle);
        } finally {
          try {
            if (con != null) {
              con.close();
            }
          } catch (SQLException sqle) {}
        }
      }
      public SportTeamPK ejbFindByPrimaryKey(SportTeamPK pk) throws FinderException {
        Connection con = null;
        try
        {
          con = getConnection();
          PreparedStatement statement =
                con.prepareStatement("SELECT SPORT " +
                                     "FROM SPORTSTEAMS WHERE SPORT = ? " +
                                     "AND NICKNAME = ? ");
          statement.setString(1, pk.getSport());
          statement.setString(2, pk.getNickName());      ResultSet resultSet = statement.executeQuery();
          if (!resultSet.next()) {
            throw new ObjectNotFoundException();
          }
          resultSet.close();
          statement.close();
          return pk;
        }
        catch (SQLException sqle)
        {
          throw new EJBException(sqle);
        }
        finally
        {
          try
          {
            if (con != null) {
              con.close();
            }
          }
          catch(SQLException sqle)
          {}
        }
      }
      public Collection ejbFindByOwnerName(String ownerName) throws FinderException {
        Connection con = null;
        try
        {
          con = getConnection();
          PreparedStatement statement =                   // Primary key info
                  con.prepareStatement("SELECT SPORT, NICKNAME " +
                                       "FROM SPORTSTEAMS WHERE OWNERNAME = ? ");
          statement.setString(1, ownerName);
          ResultSet resultSet = statement.executeQuery();      LinkedList queryMatches = new LinkedList();
          while (resultSet.next()) {
            SportTeamPK pk = new SportTeamPK(resultSet.getString(1),
                                             resultSet.getString(2));
            queryMatches.add(pk);
          }      resultSet.close();
          statement.close();
          return queryMatches;
        }
        catch (SQLException sqle)
        {
          throw new EJBException(sqle);
        }
        finally
        {
          try
          {
            if (con != null)
            {
              con.close();
            }
          }
          catch (SQLException sqle)
          {}
        }
      }  public void ejbActivate() {}  public void ejbPassivate() {}
      public void setOwnerName(String ownerName)
              throws RemoteException
      {
            this.ownerName = ownerName;
      }  public String getOwnerName()
      {
            return ownerName;
      }  public void setFranchisePlayer(String franchisePlayer)
      {
        this.franchisePlayer =franchisePlayer;
      }  public String getFranchisePlayer()
      {
            return franchisePlayer;
      }  private Connection getConnection()
      {
        try
        {
          Context initial = new InitialContext();
          DataSource dataSource =
            (DataSource) initial.lookup("LocalOracleDS");
          return dataSource.getConnection();
        }
        catch (javax.naming.NamingException ne)
        {
          ne.printStackTrace();
          throw new EJBException(ne);
        }
        catch (java.sql.SQLException sqle)
        {
          sqle.printStackTrace();
          throw new EJBException(sqle);
        }
      }
    }
      

  2.   

    已经在JBulider7+WebLocgic6.1实现,可以了吧?
      

  3.   

    能否把关键的几个xml文件贴上,不胜感激!