Entity Bean import java.io.Serializable; import java.sql.*; import java.util.Collection; import java.util.Vector; import javax.ejb.*; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; public class PlayerEntityBean implements EntityBean { final static private boolean VERBOSE = true; private EntityContext ctx; private String playerID; // also the primary Key private String playerName; private String playerNumber; private String playerPosition; private String teamID; boolean isModified = false; GenericObject myPlayerObject = new GenericObject(); public void setEntityContext(EntityContext ctx) { log("setEntityContext called"); this.ctx = ctx; } public void unsetEntityContext() { log("unsetEntityContext (" + id() + ")"); this.ctx = null; } public void ejbActivate() { log("ejbActivate (" + id() + ")"); } public void ejbPassivate() { log("ejbPassivate (" + id() + ")"); } public void ejbLoad() { System.out.println("ejbLoad()"); Connection con = null; PreparedStatement ps = null; playerID = (String) ctx.getPrimaryKey(); try { con = getConnection(); ps = con.prepareStatement("select * from players where player_id = ?"); ps.setString(1, playerID); ps.executeQuery(); ResultSet rs = ps.getResultSet(); if (rs.next()) { myPlayerObject.setAttribute("PlayerName", rs.getString(1)); } else { String error = "ejbLoad: PlayerBean (" + playerID + ") not found"; log(error); throw new NoSuchEntityException(error); } } catch (SQLException sqe) { log("SQLException: " + sqe); throw new EJBException(sqe); } finally { cleanup(con, ps); } } public void ejbStore() { if (isModified) { System.out.println("ejbStore() id = " + id()); Connection con = null; PreparedStatement ps = null; try { System.out.println("ejbStore() playerID = " + playerID); con = getConnection(); ps = con.prepareStatement("UPDATE players set name = ? WHERE player_id = ?"); ps.setString(1, myPlayerObject.getAttribute("PlayerName"); ps.setString(2, myPlayerObject.getAttribute("PlayerID"); if (!(ps.executeUpdate() > 0)) { String error = "ejbStore: PlayerBean (" + playerID + ") not updated"; log(error); throw new NoSuchEntityException(error); } } catch (SQLException sqe) { log("SQLException: " + sqe); throw new EJBException(sqe); } finally { cleanup(con, ps); } isModified = false; } } public String ejbCreate(GenericObject inPlayerHolder) throws CreateException { System.out.println("ejbCreate()"); this.playerID = inPlayerHolder.getAttributeAsString("PlayerID"); Connection con = null; PreparedStatement ps = null; try { con = getConnection(); ps = con.prepareStatement("insert into players (team_id, player_id, player_name, player_number, player_position) values (?,?,?,?,?)"); ps.setString(1, inPlayerHolder.getAttributeAsString("team_id"); ps.setString(2, inPlayerHolder.getAttributeAsString("player_id"); ps.setString(3, inPlayerHolder.getAttributeAsString("player_name"); ps.setString(4, inPlayerHolder.getAttributeAsString("player_number"); ps.setString(5, inPlayerHolder.getAttributeAsString("player_position"); if (ps.executeUpdate() != 1) { String error = "JDBC did not create any row"; log(error); throw new CreateException(error); } return playerID; } catch (SQLException sqe) { throw new CreateException(sqe.getMessage()); // Check to see if this SQLException is due to a unique constraint // violation on our database table (ie. there is already a pk with the // value of accountId in the table). If so, throw a // DuplicateKeyException else throw a CreateException. /* try { ejbFindByPrimaryKey(playerID); } catch(ObjectNotFoundException onfe) { String error = "SQLException: " + sqe; log(error); throw new CreateException (error); } */ //String error = "An Account already exists in the database with Primary Key " + playerID; //log(error); //throw new DuplicateKeyException(error); } finally { cleanup(con, ps); } } public void ejbPostCreate(String playerID) { System.out.println("ejbPostCreate()"); } public void ejbRemove() { System.out.println("ejbRemove()"); Connection con = null; PreparedStatement ps = null; try { con = getConnection(); playerID = (String) ctx.getPrimaryKey(); ps = con.prepareStatement("delete from players where id = ?"); ps.setString(1, playerID); if (!(ps.executeUpdate() > 0)) { String error = "PlayerBean (" + playerID + " not found"; log(error); throw new NoSuchEntityException(error); } } catch (SQLException sqe) { log("SQLException: " + sqe); throw new EJBException(sqe); } finally { cleanup(con, ps); } } public String ejbFindByPrimaryKey(String pk) throws ObjectNotFoundException { System.out.println("ejbFindByPrimaryKey()"); Connection con = null; PreparedStatement ps = null; isModified = true; try { con = getConnection(); ps = con.prepareStatement("select * from players where id = ?"); ps.setString(1, pk); ps.executeQuery(); ResultSet rs = ps.getResultSet(); if (rs.next()) { //playerName = rs.getString(1); } else { String error = "ejbFindByPrimaryKey: AccountBean (" + pk + ") not found"; log(error); throw new ObjectNotFoundException(error); } } catch (SQLException sqe) { log("SQLException: " + sqe); throw new EJBException(sqe); } finally { cleanup(con, ps); } log("ejbFindByPrimaryKey (" + pk + ") found"); return pk; } private Connection getConnection() throws SQLException { System.out.println("getConnection()"); InitialContext initCtx = null; try { initCtx = new InitialContext(); DataSource ds = (javax.sql.DataSource) initCtx.lookup("java:comp/env/jdbc/fwrkPool"); return ds.getConnection(); } catch (NamingException ne) { log("Dan says, Failed to lookup JDBC Datasource. Please double check that"); log("the JNDI name defined in the resource-description of the "); log("EJB's weblogic-ejb-jar.xml file is the same as the JNDI name "); log("for the Datasource defined in your config.xml."); throw new EJBException(ne); } finally { try { if (initCtx != null) initCtx.close(); } catch (NamingException ne) { log("Error closing context: " + ne); throw new EJBException(ne); } } } private void log(String s) { if (VERBOSE) System.out.println(s); } // Return a String that contains this beans id private String id() { return "PK = " + (String) ctx.getPrimaryKey(); } private void cleanup(Connection con, PreparedStatement ps) { try { if (ps != null) ps.close(); } catch (Exception e) { log("Error closing PreparedStatement: " + e); throw new EJBException(e); } try { if (con != null) con.close(); } catch (Exception e) { log("Error closing Connection: " + e); throw new EJBException(e); } } public GenericObject getPlayer() { return myPlayerObject; } public GenericObject setPlayer(GenericObject inPlayerObject) { myPlayerObject = inPlayerObject; isModified = true; } } Generic Object utility class import java.util.HashMap; import java.util.Iterator; public class GenericObject implements java.io.Serializable { private HashMap myMap = new HashMap(); public Object getAttribute( Object pAttributeName ) throws ClassCastException { return myMap.get( pAttributeName ); } public String getAttributeAsString( Object pAttributeName ) throws ClassCastException { return (String)myMap.get( pAttributeName ); } public void setAttribute( Object pName, Object pValue ) { myMap.put( pName, pValue ); } } package com.struts; import org.apache.struts.action.*; import java.sql.*; import java.util.ArrayList; import java.util.Vector; import java.util.*; import FW_GenericObject; import Player; import CommonDelegate; import javax.servlet.http.*; import com.struts.Constants; import com.struts.Category; Struts Action class public class PlayerUpdateAction extends org.apache.struts.action.Action { public ActionForward perform(ActionMapping mapping, ActionForm form, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) { System.out.println("SEARCH_ACTION: entering perform()"); // The bean's already been set up by setServlet. Forward to the view return mapping.findForward( "success" ); } private Collection getPlayers(javax.servlet.http.HttpServletRequest pRequest) { Collection players = null; FW_GenericObject genericObject = new FW_GenericObject(); FW_GenericObject genericCreditCard = new FW_GenericObject(); Enumeration enum = pRequest.getParameterNames(); while (enum.hasMoreElements()) { System.out.println("Request param: " + enum.nextElement().toString()); } try { CommonDelegate delegate = new CommonDelegate(); ArrayList playerList = delegate.getPlayer(pRequest.getParameter("gopherName")); Vector cv = new Vector(); for (int i = 0; i < playerList.size(); i++) { genericObject = new FW_GenericObject(); Player player = (Player)playerList.get(i); System.out.println("SearchAction - Player Name: " + player.getName()); //cv.addElement( new CategoryImpl(player.getName(), player.getPosition(), player.getTeam())); //categoryArray = cv.toArray(); } } catch ( Exception ex ) { ex.printStackTrace(); } return players; } }