Sei sulla pagina 1di 67

Page 1 of 67 //The following is an example program for CORBA. //Hello.

idl module HelloApp { interface Hello { string select(in string name); }; }; //HelloServer.java import HelloApp.*; import org.omg.CosNaming.*; import org.omg.CosNaming.NamingContextPackage.*; import org.omg.CORBA.*; import java.sql.*; public class HelloServer { public static void main(String args[]) { try { ORB orb=ORB.init(args,null); HelloServant helloRef=new HelloServant(); orb.connect(helloRef); org.omg.CORBA.Object objRef=orb.resolve_initial_references("NameService"); NamingContext ncRef=NamingContextHelper.narrow(objRef); NameComponent nc=new NameComponent("Hello",""); NameComponent path[]={nc}; ncRef.rebind(path,helloRef); java.lang.Object sync=new java.lang.Object(); OUCW

Page 2 of 67 synchronized(sync) { sync.wait(); } } catch(Exception e){e.printStackTrace();} } } class HelloServant extends _HelloImplBase { Connection con; Statement stmt; ResultSet rs; public String select(String tableName) { String values=""; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:StuCorba"); stmt=con.createStatement(); rs=stmt.executeQuery("Select * from "+tableName); int rno; String name; while(rs.next()) { rno=rs.getInt(1); name=rs.getString(2); OUCW

Page 3 of 67 System.out.println(rno+"\t"+name); values=values+rno+"\t"+name+"\n"; } rs.close(); stmt.close(); con.close(); } catch(SQLException se){} catch(ClassNotFoundException ce){} return values; } } //HelloClient.java import HelloApp.*; import org.omg.CosNaming.*; import org.omg.CORBA.*; public class HelloClient { public static void main(String args[]) { try { String values; ORB orb=ORB.init(args,null); org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService"); NamingContext ncRef=NamingContextHelper.narrow(objRef); OUCW

Page 4 of 67 NameComponent nc=new NameComponent("Hello",""); NameComponent path[]={nc}; Hello helloRef=HelloHelper.narrow(ncRef.resolve(path)); System.out.println("----------Retrieving values using CORBA/JDBC from database----------------\n"); values=helloRef.select("StudentTable"); System.out.println(values); } catch(Exception e){e.printStackTrace();} } } Input/Output: >a >cd HelloApp >javac *.java >javac HelloServer.java >javac HelloClient.java >start orbd -ORBInitialPort 1205 >start java HelloServer -ORBInitialPort 1205 >java HelloClient -ORBInitialPort 1205 ----------Retrieving values using CORBA/JDBC from database---------------1 2 5 3 4 6 Dept of CSE Dept of ECE Dept of EEE Dept of Civil Dept of MECH Dept of Bio OUCW

Page 5 of 67 In the server window: 1 2 5 3 4 6 Dept of CSE Dept of ECE Dept of EEE Dept of Civil Dept of MECH Dept of Bio

OUCW

Page 6 of 67 //The following is an example program for the RMI application //MyRemote.java public interface MyRemote extends java.rmi.Remote { String sayHai( ) throws java.rmi.RemoteException; //similary n-number of methods can be decalred } //MyImpl.java public class MyImpl extends java.rmi.server.UnicastRemoteObject implements MyRemote { public MyImpl( ) throws java.rmi.RemoteException { } public String sayHai() { System.out.println("In sayHai( )"); return "Hello to you from RMI"; } } //MyRegistry.java public class MyRegistry { public static void main(String [ ]s)throws Exception { MyImpl mi=new MyImpl( ); java.rmi.Naming.bind("viswam",mi); System.out.println("Object binded into RMI registry"); } OUCW

Page 7 of 67 } //Client.java public class Client { public static void main(String [ ]s)throws Exception { java.rmi.Remote r=java.rmi.Naming.lookup("viswam"); MyRemote mr=(MyRemote) r; System.out.println(mr.sayHai( )); } } Input/Output: >javac *.java >rmic MyImpl >start rmiregistry >start java MyRegistry >java Client Hello to you from RMI In MyRegistry: Object binded into RMI registry In sayHai( )

OUCW

Page 8 of 67 //The following is an example program for Stateless Session Bean. //Remote interface import javax.ejb.*; import java.rmi.*; public interface MyRemote extends EJBObject { String getMessage( ) throws RemoteException; } //Home interface import javax.ejb.*; import java.rmi.*; public interface MyHome extends EJBHome { MyRemote create( ) throws CreateException,RemoteException; } //Bean class import javax.ejb.*; public class MyBean implements SessionBean { public void setSessionContext(SessionContext sc) { System.out.println("In setSessionContext"); this.sc=sc; } SessionContext sc; public void ejbActivate( ) { } public void ejbPassivate( ) OUCW

Page 9 of 67 { } public void ejbRemove( ) { System.out.println("In ejbRemove"); } public void ejbCreate( ) { System.out.println("In ejbCreate"); } public String getMessage( ) { System.out.println("In getMessage"); return "Enter into SLSB. welcome"; } } //Client import javax.naming.*; import java.util.*; public class Client { public static void main(String s[])throws Exception { Hashtable ht=new Hashtable( ); ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitial ContextFactory"); InitialContext ic=new InitialContext(ht); Object o=ic.lookup("nizam"); MyHome mh=(MyHome) o;

OUCW

Page 10 of 67 MyRemote mr=mh.create( ); System.out.println(mr.getMessage( )); } } //ejb-jar.xml <!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> <enterprise-beans> <session> <ejb-name>slsb</ejb-name> <home>MyHome</home> <remote>MyRemote</remote> <ejb-class>MyBean</ejb-class> <session-type>Stateless</session-type> <transaction-type>Bean</transaction-type> </session> </enterprise-beans> </ejb-jar> //weblogic-ejb-jar.xml <!DOCTYPE weblogic-ejb-jar PUBLIC "-//BEA Systems, Inc.//DTD WebLogic 8.1.0 EJB//EN" "http://www.bea.com/servers/wls810/dtd/weblogic-ejb-jar.dtd"> <weblogic-ejb-jar> <weblogic-enterprise-bean> <ejb-name>slsb</ejb-name> <jndi-name>nizam</jndi-name> </weblogic-enterprise-bean> </weblogic-ejb-jar>

OUCW

Page 11 of 67

Input/Output: In the Client window: >javac *.java >jar cvf slsb.jar *.class META-INF >java weblogic.ejbc slsb.jar ejbc successful. Deploy the jar file: To deploy the jar file, paste the jar file in the following path: :\bea\weblogic81\user_projects\domains\mydoamin\applications (paste in this folder). >java Client Enter into SLSB. welcome In the Weblogic server: In setSessionContext In ejbCreate In getMessage

OUCW

Page 12 of 67

//The following is an example program for Stateful Session Bean. //MyRemote import javax.ejb.*; import java.rmi.*; public interface MyRemote extends EJBObject { String getName( ) throws RemoteException; void setName(String name)throws RemoteException; } //MyHome.java import javax.ejb.*; import java.rmi.*; public interface MyHome extends EJBHome { MyRemote create(int empno) throws CreateException,RemoteException; } //MyBean.java import javax.ejb.*; import java.sql.*; public class MyBean implements SessionBean { public void setSessionContext(SessionContext sc) { System.out.println("In setSessionContext"); this.sc=sc; } public void ejbCreate(int eno)throws CreateException { OUCW

Page 13 of 67 System.out.println("In ejbCreate"); Connection con=null; Statement st=null; ResultSet rs=null; try { con=getConnection( ); st=con.createStatement( ); rs=st.executeQuery("select * from emp where empno="+eno); while(rs.next( )) { this.eno=eno; name=rs.getString(2); return; } }//try catch(Exception e) { e.printStackTrace( ); } finally { try { rs.close( ); st.close( ); con.close( ); }//try catch(Exception e) OUCW

Page 14 of 67 { e.printStackTrace( ); } }//finally throw new CreateException("Invalid empno given:"+eno); }//ejbCreate String name; int eno; public String getName( ) { System.out.println("In getName"); return name; } public void setName(String name) { System.out.println("In setName"); Connection con=null; Statement st=null; try { con=getConnection( ); st=con.createStatement( ); int i=st.executeUpdate("update emp set ename="+name+" where empno="+eno); if(i==1) return; }//try catch(Exception e) { e.printStackTrace( ); OUCW

Page 15 of 67 }//catch finally { try { st.close( ); con.close( ); } catch(Exception e) { e.printStackTrace( ); } } throw new EJBException("unable to update"); }//setName public void ejbActivate( ) { System.out.println("In ejbActivate"); } public void ejbPassivate( ) { System.out.println("In ejbPassivate"); } public void ejbRemove( ) { System.out.println("In ejbRemove"); } private Connection getConnection( )throws Exception { OUCW

Page 16 of 67 Class.forName("oracle.jdbc.driver.OracleDriver"); return DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "System","viswam08"); } SessionContext sc; }//MyBean //Client.java import javax.naming.*; import java.util.*; public class Client { public static void main(String args[])throws Exception { Hashtable ht=new Hashtable( ); ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitial ContextFactory"); InitialContext ic=new InitialContext(ht); Object o=ic.lookup("sfsb"); MyHome mh=(MyHome) o; MyRemote mr=mh.create(Integer.parseInt(args[0])); System.out.println(mr.getName( )); } } //ejb-jar.xml <!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> <enterprise-beans> <session> <ejb-name>sfsb</ejb-name> OUCW

Page 17 of 67 <home>MyHome</home> <remote>MyRemote</remote> <ejb-class>MyBean</ejb-class> <session-type>Stateful</session-type> <transaction-type>Container</transaction-type> </session> </enterprise-beans> <assembly-descriptor> <container-transaction> <method> <ejb-name>sfsb</ejb-name> <method-name>*</method-name> </method> <trans-attribute>Required</trans-attribute> </container-transaction> </assembly-descriptor> </ejb-jar> //weblogic-ejb-jar.xml <!DOCTYPE weblogic-ejb-jar PUBLIC '-//BEA Systems, Inc.//DTD WebLogic 8.1.0 EJB//EN' 'http://www.bea.com/servers/wls810/dtd/weblogic-ejb-jar.dtd'> <weblogic-ejb-jar> <weblogic-enterprise-bean> <ejb-name>sfsb</ejb-name> <stateful-session-descriptor> <stateful-session-cache> </stateful-session-cache> <stateful-session-clustering> </stateful-session-clustering>

OUCW

Page 18 of 67

</stateful-session-descriptor> <transaction-descriptor> </transaction-descriptor> <jndi-name>sfsb</jndi-name> </weblogic-enterprise-bean> </weblogic-ejb-jar> Input/Output: >java *.java Creation of deployment descriptors and deployment using in built tool. i.e., weblogic Builder. To start the weblogic Builder click on the following path: :\bea\weblogic81\server\bin\startWLBuilder(click this). Then click on File>open>

Click on open button.

OUCW

Page 19 of 67

Click on YES.

Click on save button. Now to create a jar file in the Builder File>Click on Archive> save. Now to validate the jar file Tools>Validate Descriptors Start the weblogic server. Connect to the weblogic server via. weblogic Builder. Tools>Connect to Server(Click it).

OUCW

Page 20 of 67

Now to deploy the module Tools>Deploy Module (click it). In the Client window: >java Client 1001 Viswam In server window: In setSessionContext In ejbCreate In getName In setSessionContext In ejbCreate

OUCW

Page 21 of 67 //The following is the example program for the bean managed persistence by using a primary key. //MyRemote.java import javax.ejb.*; import java.rmi.*; public interface MyRemote extends EJBObject { String getEmpNo( )throws RemoteException; void setSal(double d)throws RemoteException; double getSal( )throws RemoteException; String getDeptNo( )throws RemoteException; } //MyHome.java import javax.ejb.*; import java.rmi.*; public interface MyHome extends EJBHome { MyRemote findByPrimaryKey(String s)throws FinderException, RemoteException; MyRemote create(String s,double d,String s1)throws CreateException, RemoteException; } //Bean.java import javax.ejb.*; import javax.sql.*; import java.sql.*; import javax.naming.*; public class MyBean implements EntityBean { public void setEntityContext(EntityContext ec)

OUCW

Page 22 of 67 { System.out.println("In setEntityContext"); this.ec=ec; } EntityContext ec; public String ejbFindByPrimaryKey(String s)throws FinderException { System.out.println("In ejbFindByPrimaryKey"); Connection con=null; Statement st=null; ResultSet rs=null; try { con=getConnection( ); st=con.createStatement( ); rs=st.executeQuery("select * from myempdata where empno="+s); while(rs.next( )) { return s; }//while }//try catch(Exception e) { e.printStackTrace( ); } finally { try { OUCW

Page 23 of 67 con.close( ); } catch(Exception e) { e.printStackTrace( ); } }//finally throw new FinderException("Invalid empno:"+s); }//ejbFindByPrimaryKey public String ejbCreate(String s,double d,String s1)throws CreateException { System.out.println("In ejbCreate"); Connection con=null; Statement st=null; try { con=getConnection( ); st=con.createStatement( ); int i=st.executeUpdate("insert into myempdata values( "+s+","+d+","+s1+")" ); if(i==1) empno=s; sal=d; deptno=s1; return empno; } catch(Exception e) { e.printStackTrace( ); } OUCW

Page 24 of 67 finally { try { con.close( ); } catch(Exception e) { e.printStackTrace( ); } } throw new CreateException("Problem in inserting"); }//ejbCreate String empno; double sal; String deptno; public void ejbPostCreate(String s,double d,String s1) { System.out.println("In ejbPostCreate"); } public void ejbLoad( ) { System.out.println("In ejbLoad"); Connection con=null; Statement st=null; ResultSet rs=null; try { con=getConnection( ); OUCW

Page 25 of 67 st=con.createStatement( ); rs=st.executeQuery("select * from myempdata where empno="+empno); while(rs.next( )) { sal=rs.getDouble(2); deptno=rs.getString(3); return; } //while }//try catch(Exception e) { e.printStackTrace( ); } finally { try { con.close( ); } catch(Exception e) { e.printStackTrace( ); } } throw new EJBException("problem in ejbLoad"); } public String getEmpNo( ) { OUCW

Page 26 of 67 System.out.println("In getEmpNo"); return empno; } public void setSal(double d) { sal=sal+d; } public double getSal( ) { return sal; } public String getDeptNo( ) { return deptno; } public void ejbStore( ) { System.out.println("In ejbStore"); Connection con=null; Statement st=null; try { con=getConnection( ); st=con.createStatement( ); int i = st.executeUpdate("update myempdata set sal="+sal+"where empno="+empno); if(i==1) return; }//try

OUCW

Page 27 of 67 catch(Exception e) { e.printStackTrace( ); } finally { try { con.close( ); } catch(Exception e) { e.printStackTrace( ); } }//finally throw new EJBException("problem in ejbStore"); }//ejbStore public void ejbActivate( ) { System.out.println("In ejbActivate"); Object o=ec.getPrimaryKey( ); empno=(String)o; }//ejbActivate public void ejbPassivate( ) { System.out.println("In ejbPassivate"); } public void ejbRemove( ) { OUCW

Page 28 of 67 System.out.println("In ejbRemove"); Connection con=null; Statement st=null; try { con=getConnection( ); st=con.createStatement( ); int i=st.executeUpdate("delete from myempdata where empno="+empno); if(i==1) return; } catch(Exception e) { e.printStackTrace( ); } finally { try { con.close( ); } catch(Exception e) { e.printStackTrace( ); } } throw new EJBException("problem in ejbRemove"); }//ejbRemove public void unsetEntityContext( ) OUCW

Page 29 of 67 { System.out.println("In unsetEntityContext"); } private Connection getConnection( )throws Exception { InitialContext ic=new InitialContext( ); Object o=ic.lookup("uuu"); DataSource ds=(DataSource)o; return ds.getConnection( ); } }//Bean //Client.java import javax.naming.*; import java.util.*; public class Client { public static void main(String args[ ])throws Exception { Hashtable ht=new Hashtable( ); ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitial ContextFactory"); InitialContext ic=new InitialContext(ht); Object o=ic.lookup("cuc"); MyHome mh=(MyHome)o; MyRemote mr=null; try { mr=mh.findByPrimaryKey(args[0]); }

OUCW

Page 30 of 67 catch(Exception e) { System.out.println("Record not found creating new for you"); mr=mh.create(args[0],4000,"10"); } System.out.println(mr.getSal( )); } } Input/Output: In the client window: >javac *.java Create the deployment descriptors using weblogic Builder. Configuration of Connection Pool in the weblogic server. Start the weblogic server. Click on Administration Console. User name and password: weblogic and weblogic: click on sign in.

OUCW

Page 31 of 67 Click on Services>JDBC> Connection Pools (click this).

Click on Configure a new JDBC Connection Pool.

Click on Continue.

OUCW

Page 32 of 67 The following are Connection Properties.

Click on Continue.

OUCW

Page 33 of 67 Click on Test Driver Configuration

Click on Create and Deploy. Click on Services>JDBC>Data Sources(click this).

OUCW

Page 34 of 67 Click on Configure a new Data Source.

Click on Continue.

Click on Continue.

OUCW

Page 35 of 67

Click on Create.

OUCW

Page 36 of 67 In the Client window: >java Client 2 Record not found creating new for you 4000.0 In the Server window: In setEntityContext In ejbFindByPrimaryKey In setEntityContext In ejbCreate In ejbPostCreate In ejbStore In ejbLoad In ejbStore In the client window: >java Client 2 4000.0 In the server window: In setEntityContext In ejbFindByPrimaryKey In ejbLoad In ejbStore

OUCW

Page 37 of 67 //The following is an example for container managed persistence by using a primary key. //MyRemote.java import javax.ejb.*; import java.rmi.*; public interface MyRemote extends EJBObject { void setEmpNo(String s) throws RemoteException; String getEmpNo( ) throws RemoteException; void setSal(double d) throws RemoteException; double getSal( )throws RemoteException; void setDeptNo(String s) throws RemoteException; String getDeptNo( ) throws RemoteException; } //MyHome.java import javax.ejb.*; import java.rmi.*; public interface MyHome extends EJBHome { MyRemote findByPrimaryKey(String s) throws FinderException,RemoteException; MyRemote create(String s,double d,String s1)throws CreateException,RemoteException; } //MyBean.java import javax.ejb.*; abstract public class MyBean implements EntityBean { public abstract void setEmpNo(String s); public abstract String getEmpNo( ); public abstract void setSal(double d);

OUCW

Page 38 of 67 public abstract double getSal( ); public abstract void setDeptNo(String s); public abstract String getDeptNo( ); public void setEntityContext(EntityContext ec) { } public void unsetEntityContext( ) { } public void ejbActivate( ) { } public void ejbLoad( ) { } public void ejbStore( ) { } public void ejbPassivate( ) { } public void ejbRemove( ) { } public String ejbCreate(String s,double d,String s1) { setEmpNo(s); setSal(d); setDeptNo(s1); OUCW

Page 39 of 67 return null; } public void ejbPostCreate(String s,double d,String s1) { } }//MyBean //Client.java import javax.naming.*; import java.util.*; public class Client { public static void main(String s[ ])throws Exception { Hashtable ht=new Hashtable( ); ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitial ContextFactory"); InitialContext ic=new InitialContext(ht); Object o=ic.lookup("mic"); MyHome mh=(MyHome)o; MyRemote mr=null; try { mr=(MyRemote)mh.findByPrimaryKey(s[0]); } catch(Exception e) { System.out.println("Record not found create new"); mr=mh.create(s[0],5000,"20"); }

OUCW

Page 40 of 67 System.out.println(mr.getSal( )); } } //ejb-jar.xml <!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> <enterprise-beans> <entity> <ejb-name>cmp</ejb-name> <home>MyHome</home> <remote>MyRemote</remote> <ejb-class>MyBean</ejb-class> <persistence-type>Container</persistence-type> <prim-key-class>java.lang.String</prim-key-class> <reentrant>False</reentrant> <abstract-schema-name>MyBean2</abstract-schema-name> <cmp-field> <field-name>empNo</field-name> </cmp-field> <cmp-field> <field-name>sal</field-name> </cmp-field> <cmp-field> <field-name>deptNo</field-name> </cmp-field> <primkey-field>empNo</primkey-field> </entity> </enterprise-beans>

OUCW

Page 41 of 67

<assembly-descriptor> <container-transaction> <method> <ejb-name>cmp</ejb-name> <method-name>*</method-name> </method> <trans-attribute>Required</trans-attribute> </container-transaction> </assembly-descriptor> </ejb-jar> //weblogic-cmp-rdbms-jar.xml <!DOCTYPE weblogic-rdbms-jar PUBLIC '-//BEA Systems, Inc.//DTD WebLogic 8.1.0 EJB RDBMS Persistence//EN' 'http://www.bea.com/servers/wls810/dtd/weblogic-rdbms20-persistence810.dtd'> <weblogic-rdbms-jar> <weblogic-rdbms-bean> <ejb-name>cmp</ejb-name> <data-source-name>cim</data-source-name> <table-map> <table-name>myempdata</table-name> <field-map> <cmp-field>empNo</cmp-field> <dbms-column>empNo</dbms-column> </field-map> <field-map> <cmp-field>sal</cmp-field> <dbms-column>sal</dbms-column> </field-map> OUCW

Page 42 of 67 <field-map> <cmp-field>deptNo</cmp-field> <dbms-column>deptNo</dbms-column> </field-map> </table-map> </weblogic-rdbms-bean> <create-default-dbms-tables>CreateOnly</create-default-dbms-tables> </weblogic-rdbms-jar> //weblogic-ejb-jar.xml <!DOCTYPE weblogic-ejb-jar PUBLIC '-//BEA Systems, Inc.//DTD WebLogic 8.1.0 EJB//EN' 'http://www.bea.com/servers/wls810/dtd/weblogic-ejb-jar.dtd'> <weblogic-ejb-jar> <weblogic-enterprise-bean> <ejb-name>cmp</ejb-name> <entity-descriptor> <pool> </pool> <entity-cache> </entity-cache> <persistence> <persistence-use> <type-identifier>WebLogic_CMP_RDBMS</type-identifier> <type-version>7.0</type-version> <type-storage>META-INF/weblogic-cmp-rdbms-jar.xml</type-storage> </persistence-use> </persistence> <entity-clustering> </entity-clustering>

OUCW

Page 43 of 67

</entity-descriptor> <transaction-descriptor> </transaction-descriptor> <jndi-name>mic</jndi-name> </weblogic-enterprise-bean> </weblogic-ejb-jar> Input/Output: >javac *.java Crate the deployment descriptors using weblogic Builder. Deploy the Archive using Console deployment. In the Client window: >java Client 2 4000.0

OUCW

Page 44 of 67 //The following is an example program for bean managed persistence by using other than primary key. //MyRemote.java import javax.ejb.*; import java.rmi.*; public interface MyRemote extends EJBObject { String getEmpNo( )throws RemoteException; void setSal(double d)throws RemoteException; double getSal( )throws RemoteException; String getDeptNo( )throws RemoteException; } //MyHome.java import javax.ejb.*; import java.rmi.*; import java.util.*; public interface MyHome extends EJBHome { MyRemote findByPrimaryKey(String s)throws FinderException,RemoteException; Collection findByDeptNo(String s)throws FinderException,RemoteException; MyRemote create(String s,double d,String s1)throws CreateException,RemoteException; } //Bean.java import javax.ejb.*; import javax.sql.*; import java.sql.*; import javax.naming.*; import java.util.*; public class MyBean implements EntityBean

OUCW

Page 45 of 67 { public void setEntityContext(EntityContext ec) { System.out.println("In setEntityContext"); this.ec=ec; } EntityContext ec; public String ejbFindByPrimaryKey(String s)throws FinderException { System.out.println("In ejbFindByPrimaryKey"); Connection con=null; Statement st=null; ResultSet rs=null; try { con=getConnection( ); st=con.createStatement( ); rs=st.executeQuery("select * from myempdata where empno="+s); while(rs.next( )) { return s; }//while }//try catch(Exception e) { e.printStackTrace( ); } finally { OUCW

Page 46 of 67 try { con.close( ); } catch(Exception e) { e.printStackTrace( ); } }//finally throw new FinderException("Invalid empno:"+s); }//ejbFindByPrimaryKey public Collection ejbFindByDeptNo(String deptno)throws FinderException { System.out.println("In ejbFindByDeptNo"); Connection con=null; Statement st=null; ResultSet rs=null; try { con=getConnection( ); st=con.createStatement( ); rs=st.executeQuery("select * from myempdata where deptno="+deptno); Vector v=new Vector( ); while(rs.next( )) { empno=rs.getString(1); v.add(empno); }//while return v; OUCW

Page 47 of 67 }//try catch(Exception e) { e.printStackTrace( ); } finally { try { con.close( ); } catch(Exception e) { e.printStackTrace( ); } }//finally throw new FinderException("Invalid deptno:"+deptno); }//ejbFindByDeptNo public String ejbCreate(String s,double d,String s1)throws CreateException { System.out.println("In ejbCreate"); Connection con=null; Statement st=null; try { con=getConnection( ); st=con.createStatement( ); int i=st.executeUpdate("insert into myempdata values("+s+","+d+","+s1+")"); if(i==1) OUCW

Page 48 of 67 empno=s; sal=d; deptno=s1; return empno; } catch(Exception e) { e.printStackTrace( ); } finally { try { con.close( ); } catch(Exception e) { e.printStackTrace( ); } } throw new CreateException("Problem in inserting"); }//ejbCreate String empno; double sal; String deptno; public void ejbPostCreate(String s,double d,String s1) { System.out.println("In ejbPostCreate"); } OUCW

Page 49 of 67 public void ejbLoad( ) { System.out.println("In ejbLoad"); Connection con=null; Statement st=null; ResultSet rs=null; try { con=getConnection( ); st=con.createStatement( ); rs=st.executeQuery("select * from myempdata where empno="+empno); while(rs.next( )) { sal=rs.getDouble(2); deptno=rs.getString(3); return; } //while }//try catch(Exception e) { e.printStackTrace( ); } finally { try { con.close( ); } OUCW

Page 50 of 67 catch(Exception e) { e.printStackTrace( ); } } throw new EJBException("problem in ejbLoad"); } public String getEmpNo( ) { System.out.println("In getEmpNo"); return empno; } public void setSal(double d) { sal=sal+d; } public double getSal( ) { return sal; } public String getDeptNo( ) { return deptno; } public void ejbStore( ) { System.out.println("In ejbStore"); Connection con=null; Statement st=null; OUCW

Page 51 of 67 try { con=getConnection( ); st=con.createStatement( ); int i=st.executeUpdate("update empno="+empno); if(i==1) return; }//try catch(Exception e) { e.printStackTrace( ); } finally { try { con.close( ); } catch(Exception e) { e.printStackTrace( ); } }//finally throw new EJBException("problem in ejbStore"); }//ejbStore public void ejbActivate( ) { System.out.println("In ejbActivate"); myempdata set sal="+sal+"where

OUCW

Page 52 of 67 Object o=ec.getPrimaryKey( ); empno=(String)o; }//ejbActivate public void ejbPassivate( ) { System.out.println("In ejbPassivate"); } public void ejbRemove( ) { System.out.println("In ejbRemove"); Connection con=null; Statement st=null; try { con=getConnection( ); st=con.createStatement( ); int i=st.executeUpdate("delete from myempdata where empno="+empno); if(i==1) return; } catch(Exception e) { e.printStackTrace( ); } finally { try { con.close( ); OUCW

Page 53 of 67 } catch(Exception e) { e.printStackTrace( ); } } throw new EJBException("problem in ejbRemove"); }//ejbRemove public void unsetEntityContext( ) { System.out.println("In unsetEntityContext"); } private Connection getConnection( )throws Exception { InitialContext ic=new InitialContext( ); Object o=ic.lookup("uuu"); DataSource ds=(DataSource)o; return ds.getConnection( ); } }//MyBean //Client.java import javax.naming.*; import java.util.*; public class Client { public static void main(String args[ ])throws Exception { Hashtable ht=new Hashtable( );

OUCW

Page 54 of 67 ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitial ContextFactory"); InitialContext ic=new InitialContext(ht); Object o=ic.lookup("vvv"); MyHome mh=(MyHome)o; MyRemote mr=null; Collection c=mh.findByDeptNo(args[0]); Iterator it=c.iterator( ); while(it.hasNext()) { mr=(MyRemote)it.next( ); System.out.println(mr.getEmpNo( )); } } } //ejb-jar.xml <!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> <enterprise-beans> <entity> <ejb-name>bmp</ejb-name> <home>MyHome</home> <remote>MyRemote</remote> <ejb-class>MyBean</ejb-class> <persistence-type>Bean</persistence-type> <prim-key-class>java.lang.String</prim-key-class> <reentrant>False</reentrant> </entity> </enterprise-beans> OUCW

Page 55 of 67

<assembly-descriptor> <container-transaction> <method> <ejb-name>bmp</ejb-name> <method-name>*</method-name> </method> <trans-attribute>Required</trans-attribute> </container-transaction> </assembly-descriptor> </ejb-jar> //weblogic-ejb-jar.xml <!DOCTYPE weblogic-ejb-jar PUBLIC '-//BEA Systems, Inc.//DTD WebLogic 8.1.0 EJB//EN' 'http://www.bea.com/servers/wls810/dtd/weblogic-ejb-jar.dtd'> <weblogic-ejb-jar> <weblogic-enterprise-bean> <ejb-name>bmp</ejb-name> <entity-descriptor> <pool> </pool> <entity-cache> </entity-cache> <persistence> <persistence-use> <type-identifier>WebLogic_CMP_RDBMS</type-identifier> <type-version>7.0</type-version> <type-storage>META-INF/weblogic-cmp-rdbms-jar.xml</type-storage> </persistence-use>

OUCW

Page 56 of 67

</persistence> <entity-clustering> </entity-clustering> </entity-descriptor> <transaction-descriptor> </transaction-descriptor> <jndi-name>vvv</jndi-name> </weblogic-enterprise-bean> </weblogic-ejb-jar> Input/Output: >javac *.java Crate the deployment descriptors using weblogic Builder. Deploy the Archive using Console deployment. In the Client window: >java Client 10 2 In the Server window: In setEntityContext In ejbFindByDeptNo In ejbActivate In ejbLoad In getEmpNo In ejbStore

OUCW

Page 57 of 67 //The following is an example program for container managed persistence by using other than primary key. //MyRemote.java import javax.ejb.*; import java.rmi.*; public interface MyRemote extends EJBObject { void setEmpNo(String s)throws RemoteException; String getEmpNo( )throws RemoteException; void setSal(double d)throws RemoteException; double getSal( )throws RemoteException; void setDeptNo(String s) throws RemoteException; String getDeptNo( )throws RemoteException; } //MyHome.java import javax.ejb.*; import java.rmi.*; public interface MyHome extends EJBHome { MyRemote findByPrimaryKey(String s)throws FinderException,RemoteException; MyRemote create(String s,double d,String s1)throws CreateException,RemoteException; java.util.Collection findByDeptNo(String s)throws FinderException,RemoteException; } //MyBean.java import javax.ejb.*; public abstract class MyBean implements EntityBean { public abstract void setEmpNo(String s); public abstract String getEmpNo( );

OUCW

Page 58 of 67 public abstract void setSal(double d); public abstract double getSal( ); public abstract void setDeptNo(String s); public abstract String getDeptNo( ); public void setEntityContext(EntityContext ec) { } public void unsetEntityContext( ) { } public void ejbActivate( ) { } public void ejbPassivate( ) { } public void ejbRemove( ) { } public void ejbLoad( ) { } public void ejbStore( ) { } public String ejbCreate(String s,double d,String s1) { setEmpNo(s); setSal(d); OUCW

Page 59 of 67 setDeptNo(s1); return null; } public void ejbPostCreate(String s,double d,String s1) { } } //Client.java import javax.naming.*; import java.util.*; public class Client { public static void main(String args[ ])throws Exception { Hashtable ht=new Hashtable( ); ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitial ContextFactory"); InitialContext ic=new InitialContext(ht); Object o=ic.lookup("abcd"); MyHome mh=(MyHome)o; MyRemote mr=null; Collection c=mh.findByDeptNo(args[0]); Iterator it=c.iterator( ); while(it.hasNext()) { mr=(MyRemote)it.next( ); System.out.println(mr.getEmpNo( )); } }

OUCW

Page 60 of 67 } //ejb-jar.xml <!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> <enterprise-beans> <entity> <ejb-name>cmp</ejb-name> <home>MyHome</home> <remote>MyRemote</remote> <ejb-class>MyBean</ejb-class> <persistence-type>Container</persistence-type> <prim-key-class>java.lang.String</prim-key-class> <reentrant>False</reentrant> <abstract-schema-name>MyBean2</abstract-schema-name> <cmp-field> <field-name>empNo</field-name> </cmp-field> <cmp-field> <field-name>sal</field-name> </cmp-field> <cmp-field> <field-name>deptNo</field-name> </cmp-field> <primkey-field>empNo</primkey-field> <query> <query-method> <method-name>findByDeptNo</method-name> <method-params>

OUCW

Page 61 of 67 <method-param>java.lang.String</method-param> </method-params> </query-method> <ejb-ql><![CDATA[SELECT OBJECT(o) FROM MyBean2 AS o where o.deptNo=?1]]></ejb-ql> </query> </entity> </enterprise-beans> <assembly-descriptor> <container-transaction> <method> <ejb-name>cmp</ejb-name> <method-name>*</method-name> </method> <trans-attribute>Required</trans-attribute> </container-transaction> </assembly-descriptor> </ejb-jar> //weblogic-ejb-jar.xml <!DOCTYPE weblogic-ejb-jar PUBLIC '-//BEA Systems, Inc.//DTD WebLogic 8.1.0 EJB//EN' 'http://www.bea.com/servers/wls810/dtd/weblogic-ejb-jar.dtd'> <weblogic-ejb-jar> <weblogic-enterprise-bean> <ejb-name>cmp</ejb-name> <entity-descriptor> <pool> </pool> <entity-cache> </entity-cache> OUCW

Page 62 of 67

<persistence> <persistence-use> <type-identifier>WebLogic_CMP_RDBMS</type-identifier> <type-version>7.0</type-version> <type-storage>META-INF/weblogic-cmp-rdbms-jar.xml</type-storage> </persistence-use> </persistence> <entity-clustering> </entity-clustering> </entity-descriptor> <transaction-descriptor> </transaction-descriptor> <jndi-name>abcd</jndi-name> </weblogic-enterprise-bean> </weblogic-ejb-jar> //weblogic-cmp-rdbms-jar.xml <!DOCTYPE weblogic-rdbms-jar PUBLIC '-//BEA Systems, Inc.//DTD WebLogic 8.1.0 EJB RDBMS Persistence//EN' 'http://www.bea.com/servers/wls810/dtd/weblogic-rdbms20-persistence810.dtd'> <weblogic-rdbms-jar> <weblogic-rdbms-bean> <ejb-name>cmp</ejb-name> <data-source-name>xyz</data-source-name> <table-map> <table-name>myempdata</table-name> <field-map> <cmp-field>empNo</cmp-field> <dbms-column>empNo</dbms-column> OUCW

Page 63 of 67 </field-map> <field-map> <cmp-field>sal</cmp-field> <dbms-column>sal</dbms-column> </field-map> <field-map> <cmp-field>deptNo</cmp-field> <dbms-column>deptNo</dbms-column> </field-map> </table-map> <weblogic-query> <query-method> <method-name>findByDeptNo</method-name> <method-params> <method-param>java.lang.String</method-param> </method-params> </query-method> </weblogic-query> </weblogic-rdbms-bean> <create-default-dbms-tables>CreateOnly</create-default-dbms-tables> </weblogic-rdbms-jar> Input/Output: >javac *.java Crate the deployment descriptors using weblogic Builder. Deploy the Archive using Console deployment. In the Client window: >java Client 10 2

OUCW

Page 64 of 67 //The following is an example program for conversion of currency. //In the source view using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace currency { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnconvert_Click(object sender, EventArgs e) { int a = Convert.ToInt32(txtdoller.Text); txtrupee.Text = Convert.ToString(a * 48); } } } In the design view:

OUCW

Page 65 of 67 Input/Output:

OUCW

Page 66 of 67 //The following is an example program for the extraction of data from the text box. In the source view: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace viswam { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { MessageBox.Show(textBox1.Text); } } } In the design view:

OUCW

Page 67 of 67 Input/Output:

OUCW

Potrebbero piacerti anche