跟我学EJB 分布式编程技术——SQLServer2000的BMP20实例

跟我学EJB 分布式编程技术——SQLServer2000的BMP20实例
跟我学EJB 分布式编程技术——SQLServer2000的BMP20实例

目录

1.1跟我学EJB 分布式编程技术——SQLServer2000的BMP20实例 (2)

1.1.1编程SQLServer2000的BMP 2.0 EJB组件 (2)

1.1.2完整的程序代码示例 (12)

1.1.3如何查看访问BMP Bean的JNDI名称 (18)

1.1.4编译该BMP 的代码以产生*.jar文件 (19)

1.1.5部署该BMP组件 (19)

1.1.6添加访问该BMP组件的客户段程序 (20)

1.1跟我学EJB 分布式编程技术——SQLServer2000的BMP20实例

1.1.1编程SQLServer2000的BMP

2.0 EJB组件

1、BMP的具体应用场合

由于CMP在某些应用场合下可能不灵活特别在复杂的查询和报表统计时,此时可以采用BMP。

2、新建一个BMP2.0的Project项目文件,名称为BMP20Bean

3、在该项目文件中添加一个EJB组件,名称为JBBMPBean20

最后生成该EJB组件的程序代码

4、在JBuilderX中引入MS SqlServer2000数据库的JDBC驱动程序

点击菜单上的“import Schema From Database”的按钮以导入数据库的结构

如果在JBuilder中已经对数据库配置过,则直接点击“Chooser Existing Connection”按钮,选择现有的连接。

访问SQLServer2000中的pubs数据库,如果没有(第一次操作时),则应该输入

在Driver栏中:com.microsoft.jdbc.sqlserver.SQLServerDriver

在URL栏中:jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs

在UserName栏中:sa

在Password栏中:6407

在JNDI Name栏中:SqlServer2000JNDI(应该与在WebLogic中所配置的JDBC DataSource 的JNDI Name名称保持一致)

并且选中“All schemas”选择项目

然后点击“OK”按钮,JBuilder将开始连接数据库,然后将产生如下的数据库表的结构

5、创建BMP

(1)然后选择某一个数据库表,如:authors表。并右点击它,然后选择“Create BMP Entity Bean”(根据数据库中的数据表来生成对应的EJB组件)。

(2)设置该BMP Bean

其中Always Wrap Primary Key=true代表“复合主键”,本例设置为false,interface 类型设置为local/remote

(3)将产生如下的各个类文件

AuthorsBean.java程序代码

package bmp20bean;

import javax.ejb.*;

public class AuthorsBean implements EntityBean

{

EntityContext entityContext;

https://www.360docs.net/doc/ca12893905.html,ng.String auId;

https://www.360docs.net/doc/ca12893905.html,ng.String auLname;

https://www.360docs.net/doc/ca12893905.html,ng.String auFname;

https://www.360docs.net/doc/ca12893905.html,ng.String phone;

https://www.360docs.net/doc/ca12893905.html,ng.String address;

https://www.360docs.net/doc/ca12893905.html,ng.String city;

https://www.360docs.net/doc/ca12893905.html,ng.String state;

https://www.360docs.net/doc/ca12893905.html,ng.String zip;

https://www.360docs.net/doc/ca12893905.html,ng.Boolean contract;

public https://www.360docs.net/doc/ca12893905.html,ng.String ejbCreate(https://www.360docs.net/doc/ca12893905.html,ng.String auId) throws CreateException {

setAuId(auId);

return null;

}

public void ejbPostCreate(https://www.360docs.net/doc/ca12893905.html,ng.String auId) throws CreateException {

/**@todo Complete this method*/

}

public void ejbRemove() throws RemoveException {

/**@todo Complete this method*/

}

public void setAuId(https://www.360docs.net/doc/ca12893905.html,ng.String auId) {

this.auId = auId;

}

public void setAuLname(https://www.360docs.net/doc/ca12893905.html,ng.String auLname) {

this.auLname = auLname;

}

public void setAuFname(https://www.360docs.net/doc/ca12893905.html,ng.String auFname) {

this.auFname = auFname;

}

public void setPhone(https://www.360docs.net/doc/ca12893905.html,ng.String phone) {

this.phone = phone;

}

public void setAddress(https://www.360docs.net/doc/ca12893905.html,ng.String address) {

this.address = address;

}

public void setCity(https://www.360docs.net/doc/ca12893905.html,ng.String city) {

this.city = city;

}

public void setState(https://www.360docs.net/doc/ca12893905.html,ng.String state) {

this.state = state;

}

public void setZip(https://www.360docs.net/doc/ca12893905.html,ng.String zip) {

this.zip = zip;

}

public void setContract(https://www.360docs.net/doc/ca12893905.html,ng.Boolean contract) { this.contract = contract;

}

public https://www.360docs.net/doc/ca12893905.html,ng.String getAuId() {

return auId;

}

public https://www.360docs.net/doc/ca12893905.html,ng.String getAuLname() {

return auLname;

}

public https://www.360docs.net/doc/ca12893905.html,ng.String getAuFname() {

return auFname;

}

public https://www.360docs.net/doc/ca12893905.html,ng.String getPhone() {

return phone;

}

public https://www.360docs.net/doc/ca12893905.html,ng.String getAddress() {

return address;

}

public https://www.360docs.net/doc/ca12893905.html,ng.String getCity() {

return city;

}

public https://www.360docs.net/doc/ca12893905.html,ng.String getState() {

return state;

}

public https://www.360docs.net/doc/ca12893905.html,ng.String getZip() {

return zip;

}

public https://www.360docs.net/doc/ca12893905.html,ng.Boolean getContract() {

return contract;

}

public https://www.360docs.net/doc/ca12893905.html,ng.String ejbFindByPrimaryKey(https://www.360docs.net/doc/ca12893905.html,ng.String auId) throws FinderException

{

/**@todo Complete this method*/

return null;

}

public void ejbLoad()

{

/**@todo Complete this method*/

}

public void ejbStore()

/**@todo Complete this method*/

}

public void ejbActivate() {

/**@todo Complete this method*/

}

public void ejbPassivate() {

/**@todo Complete this method*/

}

public void unsetEntityContext() {

this.entityContext = null;

}

public void setEntityContext(EntityContext entityContext) {

this.entityContext = entityContext;

}

}

(4)编程AuthorsBean.java程序代码以实现数据访问(本例只实现ejbFindByPrimaryKey 方法)

●加入如下的Java包的引用

import java.sql.*;

import javax.sql.DataSource;

import javax.naming.*;

●编程ejbFindByPrimaryKey()方法

public https://www.360docs.net/doc/ca12893905.html,ng.String ejbFindByPrimaryKey(https://www.360docs.net/doc/ca12893905.html,ng.String auId) throws FinderException

{

/* 具体的代码请见后面的说明

*/

return auId; //返回主键的值

private void makeConnection() throws NamingException, SQLException //提供数据库的连接方法

{

/* 具体的代码请见后面的说明

*/

}

1.1.2完整的程序代码示例

package bmp20bean;

import javax.ejb.*;

import java.sql.*;

import javax.sql.DataSource;

import javax.naming.*;

import javax.ejb.EJBException;

public class AuthorsBean implements EntityBean

{

private Connection con; //定义出一个数据库连接对象

EntityContext entityContext;

https://www.360docs.net/doc/ca12893905.html,ng.String auId;

https://www.360docs.net/doc/ca12893905.html,ng.String auLname;

https://www.360docs.net/doc/ca12893905.html,ng.String auFname;

https://www.360docs.net/doc/ca12893905.html,ng.String phone;

https://www.360docs.net/doc/ca12893905.html,ng.String address;

https://www.360docs.net/doc/ca12893905.html,ng.String city;

https://www.360docs.net/doc/ca12893905.html,ng.String state;

https://www.360docs.net/doc/ca12893905.html,ng.String zip;

https://www.360docs.net/doc/ca12893905.html,ng.Boolean contract;

public https://www.360docs.net/doc/ca12893905.html,ng.String ejbCreate(https://www.360docs.net/doc/ca12893905.html,ng.String auId) throws CreateException {

setAuId(auId);

return null;

}

public void ejbPostCreate(https://www.360docs.net/doc/ca12893905.html,ng.String auId) throws CreateException {

/**@todo Complete this method*/

}

public void ejbRemove() throws RemoveException {

/**@todo Complete this method*/

}

public void setAuId(https://www.360docs.net/doc/ca12893905.html,ng.String auId) {

this.auId = auId;

}

public void setAuLname(https://www.360docs.net/doc/ca12893905.html,ng.String auLname) {

this.auLname = auLname;

}

public void setAuFname(https://www.360docs.net/doc/ca12893905.html,ng.String auFname) {

this.auFname = auFname;

}

public void setPhone(https://www.360docs.net/doc/ca12893905.html,ng.String phone) {

this.phone = phone;

}

public void setAddress(https://www.360docs.net/doc/ca12893905.html,ng.String address) {

this.address = address;

}

public void setCity(https://www.360docs.net/doc/ca12893905.html,ng.String city) {

this.city = city;

}

public void setState(https://www.360docs.net/doc/ca12893905.html,ng.String state) {

this.state = state;

}

public void setZip(https://www.360docs.net/doc/ca12893905.html,ng.String zip) {

this.zip = zip;

}

public void setContract(https://www.360docs.net/doc/ca12893905.html,ng.Boolean contract) { this.contract = contract;

}

public https://www.360docs.net/doc/ca12893905.html,ng.String getAuId() {

return auId;

}

public https://www.360docs.net/doc/ca12893905.html,ng.String getAuLname() {

return auLname;

}

public https://www.360docs.net/doc/ca12893905.html,ng.String getAuFname() {

return auFname;

}

public https://www.360docs.net/doc/ca12893905.html,ng.String getPhone() {

return phone;

}

public https://www.360docs.net/doc/ca12893905.html,ng.String getAddress() {

return address;

}

public https://www.360docs.net/doc/ca12893905.html,ng.String getCity() {

return city;

}

public https://www.360docs.net/doc/ca12893905.html,ng.String getState() {

return state;

}

public https://www.360docs.net/doc/ca12893905.html,ng.String getZip() {

return zip;

}

public https://www.360docs.net/doc/ca12893905.html,ng.Boolean getContract() {

return contract;

}

public https://www.360docs.net/doc/ca12893905.html,ng.String ejbFindByPrimaryKey(https://www.360docs.net/doc/ca12893905.html,ng.String auId) throws FinderException

{

PreparedStatement ps =null;

try

{

makeConnection();

}

catch (Exception ex)

{

throw new javax.ejb.EJBException ("在ejbFindByPrimaryKey方法中不能正确地连接数据库,错误为 " + ex.getMessage());

}

try

{

ps = con.prepareStatement("select * from authors where au_id =?");

ps.setString(1, auId);

ResultSet rs = ps.executeQuery();

if (rs.next())

{

this.auId=rs.getString("au_id");

this.auLname = rs.getString("au_lname");

this.auFname=rs.getString("au_fname");

this.phone = rs.getString("phone");

this.address = rs.getString("address");

this.city = rs.getString("city");

this.state=rs.getString("state");

this.zip=rs.getString("zip");

this.contract=new Boolean(rs.getBoolean("contract")); }

else

{

System.out.println("find error!");

}

}

catch(SQLException e)

{

throw new javax.ejb.EJBException ("Exception in it"+e); }

finally

{

try

{

if (ps != null)

ps.close();

if (con != null)

con.close();

}

catch (Exception ignore)

{

}

}

return auId; //返回主键的值

}

private void makeConnection() throws NamingException, SQLException //提供数据库的连接方法

{

InitialContext ic = new InitialContext();

DataSource ds = (DataSource)ic.lookup("SqlServer2000JNDI");

con = ds.getConnection();

}

public void ejbLoad() {

/**@todo Complete this method*/

}

public void ejbStore() {

/**@todo Complete this method*/

}

public void ejbActivate() {

/**@todo Complete this method*/

}

public void ejbPassivate() {

/**@todo Complete this method*/

}

/*

将数据库的连接和关闭发表放入setEntityContext和unsetEntityContext方法中,这样可以避免在各个数据的访问方法中进行数据库的连接

*/

public void unsetEntityContext() //每当容器卸载EJB Bean时将关闭数据库连接

{

this.entityContext = null;

}

public void setEntityContext(EntityContext entityContext) //每当容器加载EJB Bean时将连接数据库

{

this.entityContext = entityContext;

}

}

1.1.3如何查看访问BMP Bean的JNDI名称

查看访问BMP Bean的JNDI名称以便在客户程序中能够正确地获得它,打开weblogic-ejb-jar.xml文件

因此访问该BMP的远程接口的JNDI的名称为“AuthorsRemote”,但为了避免与CMP 中的JNDI同名称,在本例中修改它为“BMPAuthorsRemote”,local类型的JNDI名称也同时修改为“BMPAuthorsLocal”。

共享访问的问题:由于实体Bean可以由多个客户程序共享。由于多个客户程序可能修改

同一个数据,因此,为实体Bean提供事务(Transaction)支持就很重要。一般地,事务管理机制由EJB容器提供,开发者无需在Bean里面设置事务的界限。事务属性可以在Bean的部署描述器中指定。

1.1.4编译该BMP 的代码以产生*.jar文件

1.1.5部署该BMP组件

右击JBBMPBean20,然后选择“Deploy”(或者选择“Redeploy”以避免与已有的EJB的JNDI名称相同)

1.1.6添加访问该BMP组件的客户段程序

1、设计相关的程序类

类名称为AuthorsTestClient,包名称为bmp20beanclient

相关主题
相关文档
最新文档