<JSTORM> Writing Enterprise Applications with Java 2 Platform, Enterprise Edition - part7 JSTORM http://wwwjstormpekr
Revision: <10> <2001/04/29> Document Information Document title: Writing Enterprise Applications with Java 2 Platform, Enterprise Edition - part7 Document file name: Revision number: <10> Issued by: Issue Date: <2001/04/29> Status: Writing_Enterprise_Applications_7_JunoYoon < > junoyoon@orgionet final Content Information Audience Abstract Reference Benchmark information BMP Writing Enterprise Applications with JavaTM Platform, Enterprise Edition By Monica Pawlan (http://developerjavasuncom/developer/online Training/J2EE/Intro/) JSTORM <2/2>
Revision: <10> <2001/04/29> Table of Contents Lesson 7 - JDBC technology BMP(Bean Managed Persistence)4 Bean 4 BonusBean 6 CalcBean JBonusBean 13 14 19 20 21 JSTORM <3/3>
Revision: <10> <2001/04/29> SQL Cloudscape, Bean CMP(Container Managed Persistence), CMP (override) BMP(Bean Managed Pers istence) BMP CMP SQL SQL Bean BMP Bean 1 BMP J2EE Bean Bean BonusBean Container Managed Bean BonusBean getbonus, getsocsec, bonus socsec Bean ejbcreate BMP JDBC SQL Bean CalcBean calcbean getrecord 2 JSTORM <4/4>
Revision: <10> <2001/04/29> BonusBean getbonus getsocsec 2 CalcBean BonusBean B ean setentitycontext:, EntityContext Bean EntityContext Bean, Context Bean setentitycontext setse ssioncontext ejbcreate: Bean Create Create (signature) ejbcreate ejbpostcreate: ejbcreate ejbcreate ejbpostcreate ejbpostcreate ejbpostcreate Bean Bean ejbremove: Bean J2EE unsetentitycontext: ejbremove Bean, unsetentitycontext Bean Bean unsetsessioncontext ejbfindbyprimarykey: Bean findbyprimaryk ey, Bean (Signature) ejbfind <type> ejbload ejbstore: set Bean (set) (get), ejbstore, ejbload finder ejbload Bean ejbactivate ejbpassivate:, Bean A ctivation( ) Passvate( ) Swap Bean Swapping JSTORM <5/5>
Revision: <10> <2001/04/29> ejbpassiv ate Bean Connection Close ejbactivate Open BonusBean Bean Management Persistence BonusBean Persistence Import InitialContext, DataSource Connection PreparedStatement SQL Request ResultSet query FinderException SQLException, package Beans; import javarmiremoteexception; import javaxejbcreateexception; import javaxejbentitybean; import javaxejbentitycontext; import javaxnaminginitialcontext; import javaxsqldatasource; import javasqlconnection; import javasqlpreparedstatement; import javasqlresultset; import javaxejbfinderexception; import javasqlsqlexception; java:comp/env/jdbc/bonusdb (resourse reference), Deploy Bean J2EE Cloudscape (CloudscapeDB) (alias) CloudscapeDB BONUS, jdbc/bonusd B jdbc/cloudscapedb public class BonusBean implements EntityBean { private EntityContext context; JSTORM <6/6>
Revision: <10> <2001/04/29> private Connection con; private String dbname = "java:comp/env/jdbc/bonusdb"; private InitialContext ic = null; private PreparedStatement ps = null; private double bonus; private String socsec; Systemoutprintln Systemoutprintln public double getbonus() { Systemoutprintln("getBonus"); return thisbonus; public String getsocsec() { Systemoutprintln("getSocSec"); return thissocsec; Systemoutprintln, ejbcreate ejbcreate Signature CreateException Remote- Exception SQLException ejbcreate SQL (SQL ) SQLException, RemoteException Primary key String Bonus Primary Key Bonus public String ejbcreate(double bonus, String socsec) throws RemoteException, CreateException, SQLException { thissocsec=socsec; JSTORM <7/7>
Revision: <10> <2001/04/29> thisbonus=bonus; Systemoutprintln("Create Method"); try { //Establish database connection ic = new InitialContext(); DataSource ds = (DataSource) iclookup(dbname); con = dsgetconnection(); //Use PreparedStatement to form SQL INSERT statement //to insert into BONUS table ps = conpreparestatement( "INSERT INTO BONUS VALUES (?,? ) "); //Set 1st PreparedStatement value marked by?, with //socsec and the 2nd value marked by? ) with bonus pssetstring(1, socsec); pssetdouble(2, bonus); psexecuteupdate(); catch (javaxnamingnamingexception ex) { exprintstacktrace(); finally { //Close database connection psclose(); conclose(); //Return primary key return socsec; ejbpostcreate ejbcreate Signature public void ejbpostcreate(double bonus, String socsec) throws RemoteException, CreateException, SQLException { Systemoutprintln("Post Create"); ejbfindbyprimarykey BonusBean CMP ejbfindbyprimarykey CMP Primary Key, Pri mary Key BonusBean BMP, JSTORM <8/8>
Revision: <10> <2001/04/29> SQLException CMP RemoteException FinderExcepti on Find Operation ejbfindbyprimarykey Primary Key Primary Key Primary Key ejbload bonus socsec BonusBean Primary Key Type String Bonus, Primart Key Bonus public String ejbfindbyprimarykey(string primarykey) throws RemoteException, FinderException, SQLException { Systemoutprintln("Find by primary key"); try { //Establish database connection ic = new InitialContext(); DataSource ds = (DataSource) iclookup(dbname); con = dsgetconnection(); //Use PreparedStatement to form SQL SELECT statement //to select from BONUS table ps = conpreparestatement( "SELECT socsec FROM BONUS WHERE socsec =? "); pssetstring(1, primarykey); //Use ResultSet to capture SELECT statement results ResultSet rs = psexecutequery(); //If ResultSet has a value, the find was successful, //and so initialize and return key if(rsnext()) { key = primarykey; else { Systemoutprintln("Find Error"); catch (javaxnamingnamingexception ex) { exprintstacktrace(); finally { //Close database connection psclose(); conclose(); //Return primary key return key; ejbload JSTORM <9/9>
Revision: <10> <2001/04/29> ejbfindbyprimarykey Bean public void ejbload() { Systemoutprintln("Load method"); try { //Establish database connection ic = new InitialContext(); DataSource ds = (DataSource) iclookup(dbname); con = dsgetconnection(); //Use PreparedStatement to form SQL SELECT statement //to select from BONUS table ps = conpreparestatement( "SELECT * FROM BONUS WHERE SOCSEC =? "); pssetstring(1, thissocsec); //Use ResultSet to capture SELECT statement results ResultSet rs = psexecutequery(); //If ResultSet has a value, the find was successful if(rsnext()){ thisbonus = rsgetdouble(2); else { Systemoutprintln("Load Error"); catch (javasqlsqlexception ex) { exprintstacktrace(); catch (javaxnamingnamingexception ex) { exprintstacktrace(); finally { try { //Close database connection psclose(); conclose(); catch (javasqlsqlexception ex) { exprintstacktrace(); ejbstore Bean (set) (get) Bean public void ejbstore() { systemoutprintln("store method"); try { JSTORM <10/10>
Revision: <10> <2001/04/29> //Establish database connection DataSource ds = (DataSource) iclookup(dbname); con = dsgetconnection(); //Use PreparedStatement to form SQL UPDATE statement //to update BONUS table ps = conpreparestatement( "UPDATE BONUS SET BONUS =? WHERE SOCSEC =? "); //Set 1st PreparedStatement value marked by? with //bonus and the 2nd value marked by? ) with socsec pssetdouble(1, bonus); pssetstring(2, socsec); int rowcount = psexecuteupdate(); catch (javaxnamingnamingexception ex) { exprintstacktrace(); catch (javasqlsqlexception ex) { exprintstacktrace(); finally { try { //Close database connection psclose(); conclose(); catch (javasqlsqlexception ex) { exprintstacktrace(); ejbremove Bean JavaBean BonusBean ejbremove ejbremoveejbremove socsec Primary Key (socsec), Bean public void ejbremove() throws RemoteException { Systemoutprintln("Remove method"); try { DataSource ds = (DataSource) iclookup(dbname); con = dsgetconnection(); ps = conpreparestatement( "DELETE FROM BONUS WHERE SOCSEC =? "); pssetstring(1, socsec); psexecuteupdate(); catch (javasqlsqlexception ex) { JSTORM <11/11>
Revision: <10> <2001/04/29> exprintstacktrace(); catch (Exception ex) { exprintstacktrace(); try { psclose(); conclose(); catch (javasqlsqlexception ex) { exprintstacktrace(); ejbactivate Bean, Bean Bean Temporary Bean, Bean EntityContext getprimarykey Bean Primary Key, Primary Key Bean public void ejbactivate() { Systemoutprintln("Activate method"); socsec = (String) contextgetprimarykey(); ejbpassivate Bean, Bean Bean Temporary Bean, Bean Bean Primary Key null public void ejbpassivate() { Systemoutprintln("Passivate method"); socsec = null; setentitycontext Bean Context ejbactivate Context getprimarykey Bean public void setentitycontext( javaxejbentitycontext ctx){ Systemoutprintln("setEntityContext me thod"); JSTORM <12/12>
Revision: <10> <2001/04/29> thiscontext = ctx; unsetentitycontext Bean ejbremove Context null unsetentitycontext Bean public void unsetentitycontext(){ Systemoutprintln("unsetEntityContext method"); ctx = null; CalcBean JBonusBean BonusBean SQL BonusBean CalcBeancalcbonus javasqlsqlexception public class CalcBean implements SessionBean { BonusHome homebonus; public Bonus calcbonus(int multiplier, double bonus, String socsec) throws RemoteException, SQLException, CreateException { Bonus thebonus = null; double calc = (multiplier*bonus); try { InitialContext ctx = new InitialContext(); Object objref = ctxlookup("bonus"); homebonus = (BonusHome) PortableRemoteObjectnarrow( objref, BonusHomeclass); catch (Exception NamingException) { NamingExceptionprintStackTrace(); //Store data in entity Bean thebonus=homebonuscreate(calc, socsec); return thebonus; JSTORM <13/13>
Revision: <10> <2001/04/29> JBonusBean CalcBean SQLException DuplicateKeyExcpetion CreateException catch (javaxejbcreateexception e) public double getbonusamt() { if(strmult! = null){ Integer integermult = new Integer(strMult); int multiplier = integermultintvalue(); try { double bonus = 10000; thecalculation = homecalccreate(); Bonus thebonus = thecalculationcalcbonus( multiplier, bonus, socsec); Bonus record = thecalculationgetrecord( socsec); bonusamt = recordgetbonus(); socsec = recordgetsocsec(); catch (javasqlsqlexception e) { thisbonusamt = 00; thissocsec = "000"; thismessage = egetmessage(); catch (javaxejbcreateexception e) { thisbonusamt = 00; thissocsec = "000"; thismessage = egetmessage(); catch (javarmiremoteexception e) { thisbonusamt = 00; thissocsec = "000"; thismessage = egetmessage(); genxml(); return thisbonusamt; else { thisbonusamt = 0; thismessage = "None "; return thisbonusamt; BMP, CloudscapeDB BONUS CMP, createtablesql cloudtablesh (Unix) JSTORM <14/14>
Revision: <10> <2001/04/29> cloudtablebat (Windows/NT) 2, createtablesql ~/J2EE/Beans cloudtablesh (Unix) cloudtablebat (Windows/NT) ~/J2EE, Beans, Unix: /cloudtablesh Windows/NT: \cloudtablebat createtablesql drop table bonus; create table bonus (socsec varchar(9) constraint pk_bonus primary key, bonus decimal(10,2)); exit; cloudtablebat rem cloudtablebat rem Creates BONUS table in CloudscapeDB rem rem Place this script in ~\J2EE rem To run: cd ~\J2EE\cloudTablesh rem rem Change this next line to point to *your* rem j2sdkee1 21 installation rem set J2EE_HOME=\home \monicap\j2ee\j2sdkee1 21 rem rem Everything below goes on one line java -DijconnectionCloudscapeDB= jdbc:rmi://localhost:1099/jdbc:cloudscape: CloudscapeDB\;create=true -Dcloudscapesystemhome= JSTORM <15/15>
Revision: <10> <2001/04/29> %J2EE_HOME%\cloudscape -classpath %J2EE_HOME%_ib\cloudscape\clientjar; %J2EE_HOME%_ib\cloudscape\ toolsjar; %J2EE_HOME%_ib\cloudscape\cloudscapejar; %J2EE_HOME%_ib\cloudscape\RmiJdbcjar; %J2EE_HOME%_ib\cloudscape_icensejar; %CLASSPATH% -ms16m -mx32m COMcloudscapetoolsij createtablesql cloudtablesh #! /bin/sh # # cloudtablesh # Creates BONUS table in CloudscapeDB # # Place this script in ~\J2EE # To run: cd ~\J2EE\cloudTablesh # # Change this next line to point to *your* # j2s dkee1 21 installation # J2EE_HOME=/home/monicap/J2EE/j2sdkee1 2 # # Everything below goes on one line java -DijconnectionCloudscapeDB=jdbc:rmi: //localhost:1099/jdbc:cloudscape:cloudscapedb\; create=true -Dcloudscapesystemhome= $J2EE_HOME/cloudscape -classpath $J2EE_HOME/lib/cloudscape/clientjar: $J2EE_HOME/lib/cloudscape/toolsjar: $J2EE_HOME/lib/cloudscape/cloudscapejar: $J2EE_HOME/lib/cloudscape/RmiJdbcjar: $J2EE_HOME/lib/cloudscape/licensejar: ${CLASSPATH -ms16m -mx32m COMcloudscapetoolsij createtablesql JAR Bean Bean JAR Bean 1 JAR, 2BeansJar JAR CalcBean Bean JAR ( ) BonusBean, JSTORM <16/16>
Revision: <10> <2001/04/29> 2 JAR Bean, BonusBean J AR JAR BonusBean JAR [EJB JAR]: [Add] ([Contents] ) Beans [Bonusclass] [Add] [BonusBeanclass] [Add] [BonusHomeclass] [Add] [Enterprise Bean JAR classes]: Beans/Bonusclass,Beans/BonusHomeclass Beans/BonusBeanclass [OK] [EJB JAR]: [Next] [General]: BeansBonusBean,BeansBonusHome, Be ansbonus BonusBean [Entity] [Next] [Entity Settings]: [Bean-managed persistence] Primary Key javalangstring Primary Key Private Primary Key [Next] JSTORM <17/17>
Revision: <10> <2001/04/29> [Environment Entries]: [Next] Bean ( ) [Enterprise Bean References]: [Next] [Resource References]: [Add] [Coded Name] 1 jdbc/bonusdb [Type] ja vaxsqldatasource [Authentication] Container [Next] [Security]: [Next] Bean Security Role [Transaction Management]: [Container-managed transactions] ( ) create,findbyprimarykey, getbonus getsocsec requ ired Commit, Enterprise JavaBeans Developer's Guid e Chapter 6 [Next] [Review Settings]: [Finish] [Inspecting] : 2BeansApp [JNDI names] CalcBean calcs BonusBean bonus, jdbc/bonusdb jdbc/cloud scape JSTORM <18/18>
Revision: <10> <2001/04/29> (Verifier) Verifier Bean compiler - Verify Save, Verify : 2BeansApp,[Tools] [Verifier] [OK] Verifier - 12 1,tests appweburi, Deploy WAR WAR war (extension), J2EE : [Tools] [Deploy Application] [Deploy BonusApp] J2EE [Return Client Jar], Bean JAR [Next] CalcBean JNDI calcs,bonusbean JNDI bonus, BonusDB JNDI jdbc/cloudscape JNDI Ret urn [Next] Context Root JSPRoot Return [Next] [Finish] JSTORM <19/19>
Revision: <10> <2001/04/29> [OK] Web 8000 bonusjsp http://localhost:8000/jsproot/bonusjsp Deploy JSP [Submit] Bonusjsp HTML, J2EE Cloudscape, Cannot find principal mapping information for data source with JNDI name jdbc/cloudscape J2EE ( ) setentitycontext method Create Method Post Create setentitycontext method Find by primary key Load method getbonus Store method Load method getsocsec Store method Find by primary key Load method getsocsec Store method Load method getbonus Store method <? xml version="1 0"? > <report> JSTORM <20/20>
Revision: <10> <2001/04/29> <bonuscalc ssnum="777777777" bonusamt="300 0" /> </report> Bean BMP http://javasuncom/j2ee/j2sdkee/techdocs/guides/ejb/html/entityfmhtml, http://javasuncom/j2ee/j2sdkee/techdocs/guides/ejb/html/databasefmhtml JSTORM <21/21>