package cylikon.YahooEnt; import javax.ejb.EntityBean; import javax.ejb.EntityContext; import javax.ejb.CreateException; import javax.ejb.FinderException; import javax.ejb.RemoveException; import java.rmi.RemoteException; import java.sql.*; import java.lang.ClassNotFoundException; import sun.jdbc.rowset.CachedRowSet; import javax.naming.InitialContext; //well be used for serializing to the JNDI namespace import javax.naming.Context; import javax.naming.NamingException; import java.util.Properties; import java.util.Vector; public class CategoryBean implements EntityBean { //instance variables private EntityContext ec; public String queryString=""; public String RowSet_Name; public String MD_Name; public String meta[]=null; public Vector MData=new Vector(); public String dbURL="jdbc:mysql://:3306/siteindex2"; public String dbURL2="jdbc:mysql://:3306/preferences"; public String db_driver="org.gjt.mm.mysql.Driver"; //these variables are the Container-managed fields...must be public // using all strings/varchars even where int/Integer would be best, because for some // reason Ejboss was setting all ints to 0, ignoring client input, might be a bug public String cat_no; //our primary key public String name; public String col_count; public String row_count; public String crs_ready; public String db_url="jdbc:mysql://:3306/siteindex2"; public String crs_error; public CachedRowSet cr=null; //saved as a blob in the DB // --- methods mandatory in the EJB spec...all will be void return types (rather than primary key // ----values) because this is container-managed persistence, not bean-managed persistence public void ejbCreate(String cat__no) throws RemoteException,CreateException,SQLException,ClassNotFoundException { cat_no=cat__no; System.out.println("Cat_no is "+cat_no); name=""; col_count="0"; row_count="0"; crs_ready="0"; //initial value of zero until we get a resultset crs_error=""; db_url=""; cr=new CachedRowSet(); PopulateRowSet(); // defined later } public void ejbCreate(String cat__no,String name_) throws RemoteException,CreateException,SQLException,ClassNotFoundException { name=name_; cat_no=cat__no; System.out.println("Cat_no is "+cat_no); col_count="0"; row_count="0"; crs_ready="0"; //initial value of zero until we get a resultset crs_error=""; db_url=""; cr=new CachedRowSet(); PopulateRowSet(); //defined later } //method correspond to the create methods above, not really doing anything useful here, just // get a primary key from the EntityContext.getPrimaryKey() method and a reference to the // remote interface using EntityContext.getEJBObject() public void ejbPostCreate(String cat__no) throws RemoteException, CreateException { } public void ejbPostCreate(String cat__no,String name_) throws RemoteException,CreateException{} public void ejbFindByPrimaryKey(CategoryPK pk) throws RemoteException,FinderException{ } public void ejbActivate() throws RemoteException { } public void ejbPassivate() throws RemoteException { } public void ejbRemove() throws RemoteException,RemoveException {} // we dont need to do anything with the load and store methods in a container-managed bean, // since all of that will be handled by the container... public void ejbLoad() throws RemoteException { } public void ejbStore() throws RemoteException { } // methods for (un)setting the EntityContext public void setEntityContext(EntityContext enc) throws RemoteException { ec=enc; } public void unsetEntityContext() throws RemoteException { ec=null; } // ****** now, on to the actual business methods.... ***************************** public CachedRowSet crs() { return cr; } public String getCatName() { return name; } public int getCatNo() { return Integer.valueOf(cat_no).intValue(); } public int getColCount() { return Integer.valueOf(col_count).intValue(); } public int getRowCount() { return Integer.valueOf(row_count).intValue(); } public String crsError() { return crs_error; } public String getDBUrl() { return db_url; } public int crsReady() { return Integer.valueOf(crs_ready).intValue(); } // this method will be called during ejbCreate - so the Entity should start off initially // with a populated CachedRowSet. This method should *not* need to be called again, unless // we have emptied and closed the CachedRowSet (using the crsRelease() ) method, in that // case, we will be basically be creating a completely new CachedRowSet with new data. // For simply refreshing the contents of this serialized ResultSet, the crsRefresh() // method has been provided to simply update the CachedRowSet row-by-row public void PopulateRowSet() { Connection conn=null; PreparedStatement stmt=null; ResultSet rs=null; ResultSetMetaData md=null; String match=cat_no; //queryString="SELECT * FROM thesites WHERE itsgroup='"+cat_no+"'"; if (cat_no ==null) { crs_error="itsgroup was null"; } if (cat_no !=null && Integer.valueOf(cat_no).intValue() <=0) {crs_error="itsgroup equaled "+cat_no; } /* Keeping all the values above at null (to decrease overhead) until all the "try" statements have been passed successfully, so these resources will only be created in-full on an as-needed basis */ this.crs_ready=""+1; // starting with an initial value of "ready"(1) - it will be changed to 0 if something fails try { Class.forName(db_driver); } catch (ClassNotFoundException cn) { crs_ready=""+0; crs_error="Error loading JDBC Driver: "+cn+cn.getMessage(); } try { conn = DriverManager.getConnection(dbURL,"Someuser","Somepass"); conn.setAutoCommit(true); // only value allowed for MySQL } catch (Throwable t) { crs_error="Error establishing a database connecton: "+t+t.getMessage(); crs_ready=""+0; org.ejboss.services.Tracer.trace("ERROR: "+t.getMessage()); } try { stmt=conn.prepareStatement("SELECT * FROM thesites WHERE itsgroup=?"); stmt.setString(1,cat_no); rs=stmt.executeQuery(); // org.ejboss.services.Tracer.trace("Query was "+queryString); //not using trace now cr=new CachedRowSet(); cr.populate(rs); //put the ResultSet into the serializable CachedRowSet } catch (Throwable t) { crs_ready=""+0; crs_error="Error getting data from the database: "+t+t.getMessage(); org.ejboss.services.Tracer.trace("ERROR: "+t.getMessage()); } // now set the metadata information for the rest of the class... if (cr!=null) { try { md=cr.getMetaData(); this.db_url=dbURL; this.col_count=""+md.getColumnCount(); this.row_count=""+cr.size(); } catch (Throwable t) { crs_ready=""+1; /* setting crs_ready to 1, because if the CachedRowSet is not null, then it is technically available, we just don't have any MetaData to go along with it */ crs_error="Error getting CachedRowSet MetaData: "+t+t.getMessage(); } //end try-catch } //end if if (rs !=null) { try { rs.close(); rs=null; } catch (Throwable t) { crs_error=crs_error+"\nError closing original ResultSet: "+t; } } if (stmt !=null) { try { stmt.close(); stmt=null; } catch (Throwable t) { crs_error=crs_error+"\nError closing Statement: "+t; } } if (conn !=null) { try { conn.close(); conn=null; } catch (Throwable t) { crs_error=crs_error+"\nError closing Database Connection"; } } md=null; } //end PopulateRowSet method public void crsRefresh() { try { if (cr != null) { while(cr.next()) { cr.refreshRow(); } this.cr=cr; crs_ready=""+1; } //end if } catch (Throwable t) { crs_ready=""+0; crs_error="Error refreshing the CachedRowSet: "+t+t.getMessage(); } } // end crsRefresh method public void crsRelease() { // this method is for clearing the CachedRowSet and releasing its contents/resources try { cr.release(); cr.close(); this.cr=null; crs_ready=""+0; //CachedRowSet is now no longer available col_count=""+0; row_count=""+0; } catch (Throwable t) { crs_ready=""+0; crs_error="Error emptying the CachedRowSet: "+t+t.getMessage(); } } //end crsRelease method public Vector MetaData() { // first make sure the Vector is clear... MData.removeAllElements(); MData.addElement(name); MData.addElement(cat_no); MData.addElement(crs_ready); MData.addElement(crs_error); MData.addElement(row_count); MData.addElement(col_count); MData.addElement(db_url); return MData; } public String MetaDataOrder() { return "Field Order of MetaData Array: name,cat_no,crs_ready,crs_error,row_count,col_count,db_url"; } } //end class