全局(Entity)EJB的应用
1、全局EJB的作用
(1)、可以和数据库的数据结合操作,全局EJB提供各种数据操作的方法。 (2)、全局EJB的数据可以被多个客户端共享使用,全局EJB引入主键的应用,主键可以标识每个全局EJB。 2、全局EJB的分类 全局EJB分为Bean持续性和容器持续性。它们的区别如下: (1)、Bean持续性将SQL语句写入代码 (2)、容器持续性EJB代码没用SQL处理语句,作用XML文件进行写义,且适应不同的数据库。 3、全局EJB生命周期结构图
 缓冲状态EJB不与数据表的数据建立联系,不存在主键;缓冲状态EJB通过ejbCreate方法和ejbPostCreate方法或者ejbFind方法获得主键,建立与数据表的数据的关系,当缓冲状态EJB取得主键后便处于准备使用状态,如果EJB内的远程方法被使用,ejbLoad方法首先被激活,运行远程方法,然后ejbStore被激活。一个全局EJB可以有多个缓冲状态和准备使用状态。
EJB主文件: 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 ""; }
//在ejbCreate后执行 public void ejbPostCreate() throws CreateException { System.out.println("ejbPostCreate"); }
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"; }
//在执行远程方法前被激活 public void ejbLoad() { System.out.println("ejbCreate"); }
//在执行远程方法后被激活 public void ejbStore() { System.out.println("ejbStore"); }
public void ejbActivate() { System.out.println("ejbActivate"); }
public void ejbPassivate() { System.out.println("ejbPassivate"); }
//当第1次引用创建接口方法时,该方法被激活 public void setEntityContext(EntityContext entityContext) { System.out.println("----------------------------------------------------"); System.out.println("setEntityContext"); this.entityContext = entityContext; }
public void unsetEntityContext() { System.out.println("unsetEntityContext"); 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('" + user + "','" + password + "')"; System.out.println("SQL: " + sql); boolean success = stmt.execute(sql); return success; } catch (Exception ex) { ex.printStackTrace(); } return false; }
}
EJB远程接口: 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创建接口: 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 CreateException, RemoteException;
public HelloRemote findByPrimaryKey(String name) throws FinderException, SQLException, RemoteException; }
客户端: /** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2004</p> * <p>Company: </p> * @author 胡斐 * @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 { //声明链接jboss服务器的属性类 Properties properties; //声明寻找服务器对象的Context类 Context ctx; //设置访问jboss服务器的参数 properties = new Properties(); properties.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory"); properties.put("java.naming.provider.url", "localhost:1099"); //创建JNDI对象的搜索器 ctx = new InitialContext(properties); //使用lookup方法寻找UserTableRemote对象 Object UserTableJNDI = ctx.lookup("Hello"); //取得EJB的Home接口 HelloRemoteHome home = (HelloRemoteHome) PortableRemoteObject.narrow( UserTableJNDI, HelloRemoteHome.class); //声明EJB的的Remote接口 HelloRemote jackUser; try { //取得EJB的Remote接口 jackUser = (HelloRemote) PortableRemoteObject.narrow(home. findByPrimaryKey("wfz"), HelloRemote.class); System.out.println(jackUser.getName()); jackUser.addUser("hx", "san1"); } catch (Exception ex) { ex.printStackTrace(); }
}
} 注意:不能在一个方法内抛出两个异常,将会引起nesting错误. 
|