package customer.ejb;
import java.rmi.RemoteException;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.ejb.EJBException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.RemoveException;
import javax.ejb.CreateException;
import customer.interfaces.CustomerData;
import java.sql.*;
import java.util.*;
/**
* XDoclet-based BMP entity bean.
*
* To generate EJB related classes using XDoclet:
*
* - Add Standard EJB module to XDoclet project properties
* - Customize XDoclet configuration
* - Run XDoclet
*
* Below are the xdoclet-related tags needed for this EJB.
*
* @ejb.bean name="Customer"
* display-name="Name for Customer"
* description="Description for Customer"
* jndi-name="ejb/Customer"
* type="BMP"
* view-type="local"
*/
public class CustomerBean implements EntityBean {
/** The entity context */
private EntityContext context;
private Connection conn=null;
private String id;
private String psw;
private String name;
private String sex;
int age;
/**
* An example business method
*
* @ejb.interface-method view-type = "local"
*
* @throws EJBException Thrown if method fails due to system-level error.
*/
public String getId() {
return id;
}
/**
* An example business method
*
* @ejb.interface-method view-type = "local"
*
* @throws EJBException Thrown if method fails due to system-level error.
*/
public void setId(String id) {
this.id = id;
}
/**
* An example business method
*
* @ejb.interface-method view-type = "local"
*
* @throws EJBException Thrown if method fails due to system-level error.
*/
public String getName() {
return name;
}
/**
* An example business method
*
* @ejb.interface-method view-type = "local"
*
* @throws EJBException Thrown if method fails due to system-level error.
*/
public void setName(String name) {
this.name = name;
}
/**
* An example business method
*
* @ejb.interface-method view-type = "local"
*
* @throws EJBException Thrown if method fails due to system-level error.
*/
public String getSex() {
return sex;
}
/**
* An example business method
*
* @ejb.interface-method view-type = "local"
*
* @throws EJBException Thrown if method fails due to system-level error.
*/
public void setSex(String sex) {
this.sex = sex;
}
/**
* An example business method
*
* @ejb.interface-method view-type = "local"
*
* @throws EJBException Thrown if method fails due to system-level error.
*/
public int getAge() {
return age;
}
/**
* An example business method
*
* @ejb.interface-method view-type = "local"
*
* @throws EJBException Thrown if method fails due to system-level error.
*/
public void setAge(int age) {
this.age = age;
}
public CustomerBean() {
// TODO Auto-generated constructor stub
}
/**
* There are zero or more ejbCreate<METHOD>(...) methods, whose signatures match
* the signatures of the create<METHOD>(...) methods of the entity bean?s home interface.
* The container invokes an ejbCreate<METHOD>(...) method on an entity bean instance
* when a client invokes a matching create<METHOD>(...) method on the entity bean?s
* home interface.<br>
*
* The entity bean provider?s responsibility is to initialize the instance in the ejbCreate<
* METHOD>(...) methods from the input arguments, using the get and set accessor
* methods, such that when the ejbCreate<METHOD>(...) method returns, the persistent
* representation of the instance can be created. <br>
*
* The entity bean provider must not attempt to modify the values of cmr-fields in an ejbCreate<
* METHOD(...) method; this should be done in the ejbPostCreate<METHOD(...) method instead.<br>
*
* The entity object created by the ejbCreate<METHOD> method must have a unique primary
* key. This means that the primary key must be different from the primary keys of all the existing
* entity objects within the same home. However, it is legal to reuse the primary key of a previously
* removed entity object. The implementation of the bean provider?s ejbCreate<
* METHOD>(...) methods should be coded to return a null.<br>
*
* An ejbCreate<METHOD>(...) method executes in the transaction context determined by
* the transaction attribute of the matching create<METHOD>(...) method.
* The database insert operations are performed by the container within the same
* transaction context after the Bean Provider?s ejbCreate<METHOD>(...) method completes.
*
* @throws CreateException Thrown if method fails due to system-level error.
*
* @throws CreateException
*
* @ejb.create-method
*/
public String ejbCreate(String id,String psw,String name,String sex, int age) throws CreateException {
//创建一条记录
try{
String sql = "insert into Customer values(?,?,?,?,?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, id);
pstmt.setString(2,psw);
pstmt.setString(3, name);
pstmt.setString(4,sex);
pstmt.setInt(5,age);
int ret = pstmt.executeUpdate();
if(ret == 0) throw new EJBException("插入记录失败。");
else{
//保存当前值
this.setId(id);
this.setPsw(psw);
this.setName(name);
this.setAge(age);
this.setSex(sex);
pstmt.close();
}
}catch(Exception ex){
throw new EJBException(ex);
}
return this.id; //一定要返回当前的主键值对象
}
/**
* For each ejbCreate<METHOD>(...) method, there is a matching ejbPostCreate<
* METHOD>(...) method that has the same input parameters but whose return type is
* void. The container invokes the matching ejbPostCreate<METHOD>(...) method on
* an instance after it invokes the ejbCreate<METHOD>(...) method with the same arguments.
* The instance can discover the primary key by calling getPrimaryKey() on its
* entity context object. <br>
*
* The entity object identity is available during the ejbPostCreate<METHOD>(...)
* method. The instance may, for example, obtain the component interface of the associated entity
* object and pass it to another enterprise bean as a method argument.<br>
*
* The entity Bean Provider may use the ejbPostCreate<METHOD>(...) to set the values
* of cmr-fields to complete the initialization of the entity bean instance.
* An ejbPostCreate<METHOD>(...) method executes in the same transaction context as
* the previous ejbCreate<METHOD>(...) method.
*
* @throws CreateException Thrown if method fails due to system-level error.
*/
public void ejbPostCreate() throws CreateException {
}
public void ejbActivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub
//关联主键值
this.id = (String)context.getPrimaryKey(); //不能省
}
public void ejbLoad() throws EJBException, RemoteException {
// TODO Auto-generated method stub
//根据主键值,载入其他字段值
try{
String sql = "select * from Customer where id = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, this.id); //调用ejbActivate之后关联的ID
ResultSet rs = pstmt.executeQuery();
if(rs.next()){
this.setPsw(rs.getString("psw"));
this.setName(rs.getString("name"));
this.setSex(rs.getString("sex"));
this.setAge(rs.getInt("age"));
pstmt.close();
}else throw new EJBException("找不到相应记录。");
}catch(Exception ex){
throw new EJBException(ex);
}
}
public void ejbPassivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub
this.id = null; //移除主键值关联
}
public void ejbRemove()
throws RemoveException,
EJBException,
RemoteException {
// TOD
没有合适的资源?快使用搜索试试~ 我知道了~
ejb2.rar_bmp ejb2_ejb2_ejb2 cmp b
共88个文件
class:23个
java:23个
xml:13个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 174 浏览量
2022-09-14
17:39:33
上传
评论
收藏 72KB RAR 举报
温馨提示
ejb2.1规范下编写的cmp与bmp登陆实例
资源推荐
资源详情
资源评论
收起资源包目录
ejb2.rar (88个子文件)
CustomerCMP
.settings
com.genuitec.jboss.ide.eclipse.xdoclet.run.prefs 120B
org.eclipse.jdt.core.prefs 334B
src
META-INF
jboss.xml 2KB
MANIFEST.MF 36B
jbosscmp-jdbc.xml 2KB
ejb-jar.xml 5KB
customer
ejb
CustomerCMP.java 2KB
CustomerDAOSession.java 887B
CustomerBean.java 9KB
CustomerDAOBean.java 5KB
interfaces
CustomerData.java 4KB
CustomerDAOHome.java 542B
CustomerDAO.java 1020B
CustomerLocal.java 720B
CustomerDAOUtil.java 5KB
CustomerUtil.java 4KB
CustomerLocalHome.java 976B
.project 948B
classes
META-INF
jboss.xml 2KB
MANIFEST.MF 36B
jbosscmp-jdbc.xml 2KB
ejb-jar.xml 5KB
customer
ejb
CustomerCMP.class 2KB
CustomerDAOSession.class 981B
CustomerDAOBean.class 3KB
CustomerBean.class 3KB
interfaces
CustomerLocalHome.class 772B
CustomerData.class 3KB
CustomerUtil.class 4KB
CustomerDAO.class 452B
CustomerDAOUtil.class 4KB
CustomerLocal.class 389B
CustomerDAOHome.class 446B
.mymetadata 173B
.classpath 324B
.xdoclet 23KB
.myeclipse
CustomerBMP
.settings
com.genuitec.jboss.ide.eclipse.xdoclet.run.prefs 120B
org.eclipse.jdt.core.prefs 334B
src
META-INF
jboss.xml 2KB
MANIFEST.MF 36B
jaws.xml 200B
ejb-jar.xml 3KB
customer
ejb
CustomerDAOSession.java 887B
CustomerBean.java 11KB
CustomerDAOBean.java 5KB
CustomerBMP.java 2KB
interfaces
CustomerData.java 2KB
CustomerDAOHome.java 542B
CustomerDAO.java 1020B
CustomerLocal.java 2KB
CustomerDAOUtil.java 5KB
CustomerPK.java 1KB
CustomerUtil.java 4KB
CustomerLocalHome.java 855B
.project 948B
classes
META-INF
jboss.xml 2KB
MANIFEST.MF 36B
jaws.xml 200B
ejb-jar.xml 3KB
customer
ejb
CustomerBMP.class 2KB
CustomerDAOSession.class 981B
CustomerDAOBean.class 3KB
CustomerBean.class 5KB
interfaces
CustomerLocalHome.class 677B
CustomerData.class 2KB
CustomerUtil.class 4KB
CustomerDAO.class 452B
CustomerPK.class 944B
CustomerDAOUtil.class 4KB
CustomerLocal.class 389B
CustomerDAOHome.class 446B
.mymetadata 173B
.classpath 324B
.xdoclet 23KB
.myeclipse
CustomerWEB
.settings
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.jsdt.ui.superType.container 49B
.jsdtscope 500B
org.eclipse.jdt.core.prefs 334B
src
.project 1KB
WebRoot
META-INF
MANIFEST.MF 36B
WEB-INF
web.xml 365B
classes
lib
main.jsp 2KB
login.jsp 1KB
add.jsp 2KB
index.jsp 1KB
successlogin.jsp 2KB
.mymetadata 306B
.classpath 418B
.myeclipse
共 88 条
- 1
资源评论
Kinonoyomeo
- 粉丝: 74
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功