mysql-ds.xml
UserBean.java
/* * 创建日期 2005-1-14 * * 作者:javamxj(分享java快乐) */ package javamxj.ejb.cmp; /** * * <!-- begin-user-doc --> You can insert your documentation for '<em><b>UserBean</b></em>'. <!-- end-user-doc --> * <!-- begin-lomboz-definition --> <?xml version="1.0" encoding="UTF-8"?> <lomboz:EJB xmlns:j2ee="http://java.sun.com/xml/ns/j2ee" xmlns:lomboz="http://lomboz.objectlearn.com/xml/lomboz"> <lomboz:entity> <lomboz:entityEjb> <j2ee:display-name>User</j2ee:display-name> <j2ee:ejb-name>User</j2ee:ejb-name> <j2ee:ejb-class>javamxj.ejb.cmp.UserBean</j2ee:ejb-class> <j2ee:persistence-type>Container</j2ee:persistence-type> <j2ee:prim-key-class>java.lang.String</j2ee:prim-key-class> <j2ee:cmp-version>2.x</j2ee:cmp-version> <j2ee:abstract-schema-name>userSchema</j2ee:abstract-schema-name> <j2ee:primkey-field>eamil</j2ee:primkey-field> </lomboz:entityEjb> <lomboz:fieldMappings> <lomboz:fieldName>email</lomboz:fieldName> <lomboz:fieldType>java.lang.String</lomboz:fieldType> <lomboz:columnName>电子邮件</lomboz:columnName> <lomboz:jdbcType>VARCHAR</lomboz:jdbcType> <lomboz:sqlType>varchar</lomboz:sqlType> <lomboz:readOnly>false</lomboz:readOnly> <lomboz:primaryKey>true</lomboz:primaryKey> </lomboz:fieldMappings> <lomboz:fieldMappings> <lomboz:fieldName>password</lomboz:fieldName> <lomboz:fieldType>java.lang.String</lomboz:fieldType> <lomboz:columnName>密码</lomboz:columnName> <lomboz:jdbcType>VARCHAR</lomboz:jdbcType> <lomboz:sqlType>varchar</lomboz:sqlType> <lomboz:readOnly>false</lomboz:readOnly> <lomboz:primaryKey>false</lomboz:primaryKey> </lomboz:fieldMappings> <lomboz:tableName>userTable</lomboz:tableName> <lomboz:dataSourceName></lomboz:dataSourceName> </lomboz:entity> </lomboz:EJB> <!-- end-lomboz-definition --> * * <!-- begin-xdoclet-definition --> * @ejb.bean name="User" * jndi-name="User" * type="CMP" * primkey-field="email" * schema="userSchema" * cmp-version="2.x" * view-type = "local" * data-source="" * * @ejb.persistence * table-name="userTable" * * @ejb.finder * query="SELECT OBJECT(a) FROM userSchema as a" * signature="java.util.Collection findAll()" * * @jboss.persistence create-table = "true" * remove-table = "false" * * @ejb.pk class="java.lang.String" * <!-- end-xdoclet-definition --> * @generated **/ public abstract class UserBean implements javax.ejb.EntityBean { /** * @ejb.create-method */ public java.lang.String ejbCreate(String email, String password) throws javax.ejb.CreateException { // EJB 2.0 spec says return null for CMP ejbCreate methods. setEmail(email); setPassword(password); return null; } /** * The container invokes this method immediately after it calls ejbCreate. */ public void ejbPostCreate(String email, String password) throws javax.ejb.CreateException { } /** * CMP Field email * @return the email * @ejb.persistent-field * @ejb.persistence * column-name="电子邮件" * jdbc-type="VARCHAR" * sql-type="varchar(64)" * read-only="false" * @ejb.pk-field * * @ejb.interface-method */ public abstract java.lang.String getEmail(); /** * @param java.lang.String the new email value * @ejb.interface-method */ public abstract void setEmail(java.lang.String email); /** * CMP Field password * @return the password * @ejb.persistent-field * @ejb.persistence * column-name="密码" * jdbc-type="VARCHAR" * sql-type="varchar(64)" * read-only="false" * * @ejb.interface-method */ public abstract java.lang.String getPassword(); /** * @param java.lang.String the new password value * @ejb.interface-method */ public abstract void setPassword(java.lang.String password); }
UserManagementBean.java
/* * 创建日期 2005-1-14 * * 作者:javamxj(分享java快乐) */ package javamxj.ejb.cmp; /** * * <!-- begin-user-doc --> A generated session bean <!-- end-user-doc --> * <!-- lomboz.beginDefinition --> <?xml version="1.0" encoding="UTF-8"?> <lomboz:EJB xmlns:j2ee="http://java.sun.com/xml/ns/j2ee" xmlns:lomboz="http://lomboz.objectlearn.com/xml/lomboz"> <lomboz:session> <lomboz:sessionEjb> <j2ee:display-name>UserManagement</j2ee:display-name> <j2ee:ejb-name>UserManagement</j2ee:ejb-name> <j2ee:ejb-class>javamxj.ejb.cmp.UserManagementBean</j2ee:ejb-class> <j2ee:session-type>Stateless</j2ee:session-type> <j2ee:transaction-type>Container</j2ee:transaction-type> </lomboz:sessionEjb> </lomboz:session> </lomboz:EJB> <!-- lomboz.endDefinition --> * * <!-- begin-xdoclet-definition --> * @ejb.bean name="UserManagement" * jndi-name="UserManagement" * type="Stateless" * transaction-type="Container" * view-type = "remote" * * @ejb.ejb-ref * ejb-name = "User" * view-type = "local" * ref-name = "ejb/UserLocal" * * @jboss.ejb-local-ref * ref-name = "UserLocal" * jndi-name = "UserLocal" * * <!-- end-xdoclet-definition --> * @generated */ public abstract class UserManagementBean implements javax.ejb.SessionBean { private UserLocalHome userHome = null; /** * Create method * @ejb.create-method view-type = "remote" */ public void ejbCreate() throws javax.ejb.CreateException, javax.naming.NamingException { userHome = UserUtil.getLocalHome(); } public void ejbPassivate() { userHome = null; } /** * Business method * @ejb.interface-method view-type = "remote" */ public void addUser(String email, String password) throws javax.ejb.CreateException { UserLocal user = userHome.create(email, password); } /** * Business method * @ejb.interface-method view-type = "remote" */ public void removeUser(String email) throws javax.ejb.RemoveException { userHome.remove(email); } /** * Business method * @ejb.interface-method view-type = "remote" */ public boolean verifyPassword(String email, String password) throws javax.ejb.FinderException { UserLocal user = userHome.findByPrimaryKey(email); return user.getPassword().equals(password); } }
CMPClient1.java
/* * 创建日期 2005-1-14 * * 作者:javamxj(分享java快乐) */ package javamxj.ejb.client; import java.rmi.RemoteException; import java.util.Hashtable; import javax.ejb.CreateException; import javax.ejb.FinderException; import javax.ejb.RemoveException; import javax.naming.InitialContext; import javax.naming.NamingException; public class CMPClient1 { private javamxj.ejb.cmp.UserManagementHome getHome() throws NamingException { return (javamxj.ejb.cmp.UserManagementHome) getContext().lookup( javamxj.ejb.cmp.UserManagementHome.JNDI_NAME); } private InitialContext getContext() throws NamingException { Hashtable props = new Hashtable(); props.put(InitialContext.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); props.put(InitialContext.PROVIDER_URL, "jnp://127.0.0.1:1099"); // This establishes the security for authorization/authentication // props.put(InitialContext.SECURITY_PRINCIPAL,"username"); // props.put(InitialContext.SECURITY_CREDENTIALS,"password"); InitialContext initialContext = new InitialContext(props); return initialContext; } public void testBean() { try { javamxj.ejb.cmp.UserManagement myBean = getHome().create(); // 增加4个用户 myBean.addUser("[email protected]", "mypassword"); myBean.addUser("[email protected]", "oxford"); myBean.addUser("[email protected]", "分享Java快乐"); myBean.addUser("[email protected]","ccc"); // 验证用户和密码是否相符 boolean login = false; login = myBean.verifyPassword("[email protected]", "分享Java快乐"); System.out.println("Login =" + login); // 删除用户 myBean.removeUser("[email protected]"); //-------------------------------------- //This is the place you make your calls. //System.out.println(myBean.callYourMethod()); } catch (RemoteException e) { e.printStackTrace(); } catch (CreateException e) { e.printStackTrace(); } catch (NamingException e) { e.printStackTrace(); } catch (FinderException e) { e.printStackTrace(); } catch (RemoveException e) { e.printStackTrace(); } } public static void main(String[] args) { CMPClient1 test = new CMPClient1(); test.testBean(); } }