package com.learnweblogic.examples.ch9.bmp;
import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import javax.ejb.DuplicateKeyException;
import javax.ejb.EntityContext;
import javax.ejb.EntityBean;
import javax.ejb.EJBException;
import javax.ejb.NoSuchEntityException;
import javax.ejb.ObjectNotFoundException;
import javax.ejb.RemoveException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
// The source files in this book use explicit imports instead of
// importing a package.*. While this is a matter of personal
// preference, using explicit imports allows the user to quickly scan
// the file and learn which package contains a class.
public class StudentBean implements EntityBean {
// The next three variables are instance variables, but are not tied
// to a particular identity/primary key. These variables are set in
// the setEntityContext method before the bean has an identity and
// are used for the lifetime of the bean instance.
private EntityContext ctx;
private DataSource dataSource;
private String tableName;
// The name, ssn, and grade variables are the bean's fields. They
// will be initialized in ejbCreate, loaded from the database in
// ejbLoad, and stored to the database in ejbStore. The ssn field
// is the student's social security number and serves as the primary
// key for this bean.
private String name;
private Integer ssn; // primary key
private int grade;
// The setEntityContext method receives an EntityContext reference
// which it stores in a member variable. It also uses the EJB's
// environment context to look up some deployment-specific
// parameters. When the EJB is deployed, the deployer will specify
// values for the tableName and poolName parameters in the
// ejb-jar.xml which match the deployment environment. This allows
// these values not to be hard-coded into the bean. The
// setEntityContext method also gets a DataSource object from the
// environment. This will be used to get database connections from
// the connection pool.
public void setEntityContext(EntityContext c) {
ctx = c;
try {
Context envCtx = (Context) new InitialContext().lookup("java:/comp/env");
tableName = (String) envCtx.lookup("tableName");
String poolName = (String) envCtx.lookup("poolName");
dataSource = (DataSource) envCtx.lookup("/jdbc/"+poolName);
} catch (NamingException ne) {
// EJB was not deployed properly
throw new EJBException(ne);
}
}
public void unsetEntityContext() {
ctx = null;
}
// The ejbCreate method will initialize the member variables from the
// passed parameters and then insert the new entity bean into the
// database. If the insert fails, a SQLException will be thrown.
// The ejbCreate method then uses its ejbFindByPrimaryKey method to
// determine whether the key already exists in the table. If the
// key is already present in the table, ejbFindByPrimaryKey will
// return successfully, and the ejbCreate method will throw the
// DuplicateKeyException to inform the caller. Otherwise, an
// EJBException is thrown with the SQLException.
public Integer ejbCreate(String name, int ssn, int grade)
{
this.name = name;
this.ssn = new Integer(ssn);
this.grade = grade;
Connection con = null;
PreparedStatement ps = null;
try {
con = dataSource.getConnection();
ps = con.prepareStatement("insert into "+tableName+
" (name, ssn, grade) values (?,?,?)");
ps.setString(1, name);
ps.setInt(2, ssn);
ps.setInt(3, grade);
ps.executeUpdate();
return this.ssn;
} catch (SQLException sqe) {
try {
ejbFindByPrimaryKey(this.ssn);
throw new DuplicateKeyException("A student with social "+
"security number: "+ssn+" already exists.");
} catch (Exception Ignore) {}
sqe.printStackTrace();
throw new EJBException (sqe);
} finally {
try {
if (ps != null) ps.close();
if (con != null) con.close();
} catch (Exception ignore) {}
}
}
// This implementation requires no post-create initialization so
// this required method is empty
public void ejbPostCreate(String name, int ssn, int grade) {}
// The ejbRemove method is responsible for deleting the instance
// from the database. This method uses a SQL delete to delete the instance.
public void ejbRemove()
throws RemoveException
{
Connection con = null;
PreparedStatement ps = null;
try {
con = dataSource.getConnection();
ps = con.prepareStatement("delete from "+tableName+
" where ssn=?");
ps.setInt(1, ssn.intValue());
if (ps.executeUpdate() < 1) {
throw new RemoveException ("Error removing Student with ssn: "+ssn);
}
} catch (SQLException sqe) {
throw new EJBException (sqe);
} finally {
try {
if(ps != null) ps.close();
if(con != null) con.close();
} catch (Exception ignore) {}
}
}
// ejbLoad reads the entity bean's current state from the database
// and assigns the values to its member variables. The primary key
// is available from the EntityContext member variable. If the
// entity bean no longer exists, NoSuchEntityException is thrown.
// This might occur if the entity bean was deleted by another client
// or directly from the database.
public void ejbLoad() {
ssn = (Integer) ctx.getPrimaryKey();
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = dataSource.getConnection();
ps = con.prepareStatement("select name, grade from "
+tableName+ " where ssn=?");
ps.setInt(1, ssn.intValue());
ps.executeQuery();
rs = ps.getResultSet();
if (rs.next()) {
name = rs.getString(1);
grade = rs.getInt(2);
} else {
throw new NoSuchEntityException("Student with social "+
"security number: "+ssn+" no longer exists.");
}
} catch (SQLException sqe) {
throw new EJBException(sqe);
} finally {
try {
if (rs != null) rs.close();
if (ps != null) ps.close();
if (con != null) con.close();
} catch (Exception ignore) {}
}
}
// The ejbStore method will be called to write the entity bean's
// state back to the database. The primary key field is not written
// since primary keys should never change. An optimized version of
// this bean could also skip writing the name field since it is
// never updated.
public void ejbStore() {
Connection con = null;
PreparedStatement ps = null;
try {
con = dataSource.getConnection();
ps = con.prepareStatement("update "+tableName+
" SET name=?, grade=? " +
" where ssn=?");
ps.setString(1, name);
ps.setInt(2, grade);
ps.setInt(3, ssn.intValue());
ps.executeUpdate();
} catch (SQLException sqe) {
throw new EJBException(sqe);
} finally {
try {
if (ps != null) ps.close();
if (con != null) con.close();
} catch (Exception ignore) {}
}
}
// The ejbActivate and ejbPassivate methods are required by the
// EntityBean interface, but this bean does not use these callbacks.
public void ejbActivate() {}
public void ejbPassivate() {}
// The ejbFindByPrimary
没有合适的资源?快使用搜索试试~ 我知道了~
BMP-EJB.rar_EJB Entity
共32个文件
java:18个
cmd:6个
xml:5个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 140 浏览量
2022-09-19
12:55:15
上传
评论
收藏 16KB RAR 举报
温馨提示
一个数据库记录实体Bean实例:介绍编写CMP和BMP实体BEAN的方法。
资源详情
资源评论
资源推荐
收起资源包目录
BMP-EJB.rar (32个子文件)
bmp
StudentHome.java 607B
Student.java 389B
StudentBMPClient.java 2KB
StudentBean.java 10KB
tables.ddl 128B
weblogic-ejb-jar.xml 590B
deploy.cmd 99B
build.cmd 606B
ejb-jar.xml 2KB
runclient.cmd 96B
cmp
TeacherCMPBean.java 999B
StudentHome.java 815B
StudentCMPBean.java 2KB
Teacher.java 269B
StudentCMPClient.java 9KB
Student.java 579B
BookCMPBean.java 1KB
LockerCMPBean.java 820B
tables.ddl 711B
weblogic-ejb-jar.xml 3KB
TeacherHome.java 434B
BookHome.java 451B
deploy.cmd 99B
LockerHome.java 411B
build.cmd 899B
BookPK.java 746B
ejb-jar.xml 8KB
Locker.java 219B
Book.java 378B
weblogic-cmp-rdbms.xml 4KB
runclient.cmd 96B
www.pudn.com.txt 218B
共 32 条
- 1
御道御小黑
- 粉丝: 58
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0