Global ENTITY EJB Application
1, the role of global EJB
(1), can be combined with the data of the database, and the global EJB provides a variety of data operation methods. (2) The overall EJB data can be shared by multiple clients, the global EJB introduces the application of the primary key, and the primary key can identify each global EJB. 2, the global EJB classification global EJB is divided into bean persistent and container persistence. Their differences are as follows: (1), bean continuity writes the SQL statement into code (2), and the container persistence EJB code does not use the SQL processing statement, and the XML file is a sense, and adapt to different databases. 3, the global EJB lifecycle structure diagram buffer status EJB does not link data on the data table, there is no primary key; the buffer status EJB obtains the primary key to establish the data table, and the buffer is established by the EJBCREATE method and the EJBPOSTCREATE method or the EJBFIND method When the state EJB acquires the main key, it is ready to use, if the remote method within the EJB is used, the EJBLOAD method is first activated, run the remote method, and then the EJBStore is activated. A global EJB can have multiple buffer status and preparation status.
EJB main file: package hello;
Import java.rmi. *; import java.sql. *; import javax.ejb. *;
Public Class Hellobean Implements EntityBean {EntityContext EntityContext; String Name
Public string ejbcreate () throws createException {system.out.println ("ejbcreate"); this.name = name; return "";
/ / Perform public void ejbpostcreate () throws createException {system.out.println ("ejbpostcreate");} after ejbcreate;
Public void ejbremove () THROWS RemoveException {system.out.println ("ejbremove");}
Public string getname () {system.out.println ("getname"); return name;}
public String ejbFindByPrimaryKey (String name) throws FinderException, SQLException {System.out.println ( "ejbFindByPrimaryKey"); Connection conn = this.getConnection (); Statement stmt = conn.createStatement (); ResultSet rs = stmt.executeQuery ( "SELECT User, password from usertable where user = '" name "' "); while (rs.next ()) {string s2 = rs.getstring (" user "); string s3 = rs.getstring (" password ") System.out.println ("/ Tuser:" S2 "/ TPASSWORD:" S3);} return "hufei";
// Activate public void ejbload () {system.out.println ("ejbcreate") before performing a remote method;
// Activate public void ejbstore () {system.out.println ("EJBStore") after executing the remote method;
Public void ejbactivate () {system.out.println ("ejbactivate");}
Public void ejbpassivate () {system.out.println ("ejbpassivate");}
// When the first reference is created, the method is activated public void setentityContext ("---------------------------------------------------------------------------------- ------------------------------- "); system.out.println (" setentityContext "); this.entityContext = EntityContext;}
Public void unsentityContext () {system.out.println ("unsentityContext"); this.entityContext = null;}
Public connection getConnection () {system.out.println ("getConnection"); try {class.forname ("org.gjt.mm.mysql.driver); // mysql string url =" JDBC: mysql: //172.16 .87.10 / test useUnicode = true & characterEncoding = SJIS? "; // database name hellodb, change to your characterEncoding Connection con = (Connection) DriverManager.getConnection (url," root "," "); //this.conn = con; return con;} catch (Exception ex) {ex.printStackTrace ();} return null;} public boolean addUser (String user, String password) {try {Connection conn = this.getConnection (); Statement stmt = conn.createStatement ( ); String sql = "INSERT INTO UserTable VALUES"; " Password ") "; System.out.Println (" SQL: " SQL); Boolean Success = Stmt.execute SQL); Return Success;} catch (exception ex) {ex.printstacktrace ();} returnaf
}
EJB Remote Interface: Package Hello;
Import javax.ejb.ejbobject; import java.rmi.RemoteException;
Public interface helloremote extends ejbobject {
Public string getname () throws remoteException;
Public Boolean AddUser (String User, String Password) Throws RemoteException;} EJB Create an interface: package hello;
Import javax.ejb.ejbhome; import javax.ejb.createException; import java.rmi.RemoteException; import javax.ejb.findeRexception; import java.sql.sqlexception;
Public interface helloremotehome extends ejbhome {
Public Helloremote Create () THROWS CREATEXCEPTION, REMOTEEXCEPTION;
Public HelloreMote FindByPrimaryKey (String Name) Throws FinderException, Sqlexception, RemoteException;} Client: / ** *
Title: p> *
Description: p> *
Copyright: CopyRight (C ) 2004 p> *
Company: p> * @Author Hu Fei * @version 1.0 * / import java.util. *; Import javax.naming. *; Import javax.rmi. *;
Import hello. *;
Public class helloclient {
Public static void main (String [] args) throws exception {// Declare link JBoss server property class Properties Properties; // Declare the Context Class CTXT CTXT CTXT CTXT CTXT CTX; // Setting Access to JBoss Server Properties = New Properties ); Properties.Put ("java.naming.factory.initial," org.jnp.interfaces.namingContextFactory "); Properties.Put (" java.naming.provider.URL "," localhost: 1099 "); // create JNDI objects searcher ctx = new InitialContext (properties); // use the lookup method to find UserTableRemote objects object UserTableJNDI = ctx.lookup ( "Hello"); // get the EJB Home Interface HelloRemoteHome home = (HelloRemoteHome) PortableRemoteObject.narrow (UserTableJNDI, HelloRemoteHome.class); // declare an EJB Remote interface HelloRemote jackUser; try {// obtain the EJB Remote interface jackUser = (HelloRemote) PortableRemoteObject.narrow (home findByPrimaryKey ( "wfz"), HelloRemote.class.) System.out.println (Jackuser.GetName ()); Jackuser.Adduser ("HX", "SAN1");} catch (exception ex) {ex.printstacktrace (); }
}
} Note: Two exceptions cannot be thrown within a method, will cause a Nesting error.