繁体中文
设为首页
加入收藏
当前位置:JSP技术首页 >> J2EE/EJB/服务器 >> Jbuilder6+weblogic6.1开发Entity Bean 全攻略(建议加入精华区)

Jbuilder6+weblogic6.1开发Entity Bean 全攻略(建议加入精华区)

2004-10-01 08:26:10  作者:  来源:互联网  浏览次数:5  文字大小:【】【】【
简介:Jbuilder6+weblogic6.1开发Entity Bean 全攻略(建议加入精华区) 我现在是边学边干,今天总算给做出来了,反正文档是写给公司的,就顺便拿到网上来给大家一起分享了,因为这几天实在把我弄得很痛苦,碰到好多困...

Jbuilder6+weblogic6.1开发Entity Bean 全攻略(建议加入精华区)

我现在是边学边干,今天总算给做出来了,反正文档是写给公司的,就顺便拿到网上来给大家一起分享了,因为这几天实在把我弄得很痛苦,碰到好多困难,走了许多弯路,做出来才发现原来如此简单,所以发出来,让大家少走弯路!

1。首先开发环境是Jbuilder6+weblogic6.1, 数据库因为这里是测试,所以用的是sql server,如果用oracle,相应的作修改就可以了。(至于Jbuilder6+Weblogic6.1的环境配置,请察看精华区我写过的一篇《JBUILDER6配置weblogic6.0》)

2.这里以一个容器管理实体bean为例,在jbuilder里面先建立一个工程(注意目录不能带空格),然后new-Enterprise里面选ejb1.x entity bean modeler,然后new一个ejb模块,我这里取名contain,,其他默认,点ok,然后next,下面就是连接数据库的一些设置了!

driver就是数据库驱动程序,下拉框可以自己选,oracle,sqlserver的,这里用sqlserver,选jdbc-odbc桥:sun.jdbc.odbc.JdbcOdbcDriver,URL: jdbc:odbc:finance (finance是数据源,事先应该配好,这里就不说了),然后是username和password, JNDI name写上finance,然后next,jbuilder开始连接数据,如果连接成功,会把数据库当中的表显示出来,我们这里在数据里面只建了一个只有一个字段的表name,字段名name,varchar型的。选上name,加到selected里面,NEXT,在NEXT,在出来的画面里面给BEAN选择主健,然后一路NEXT一直到FINISH,这个时候JBUILDER就已经把本地,远程和实体BEAN的文件给你健好了。

3.三个文件名字分别为:

Name.java 远程接口

NameBean.java 实体Bean

NameHome.java 本地接口

下面三个文件的代码如下:

Name.java

import java.rmi.*;

import javax.ejb.*;

public interface Name extends EJBObject {

public String getName() throws RemoteException;

public void setName(String name) throws RemoteException;

}

NameHome.java

import java.rmi.*;

import javax.ejb.*;

public interface NameHome extends EJBHome {

public Name create(String name) throws RemoteException, CreateException;

public Name findByPrimaryKey(String primaryKey) throws RemoteException, FinderException;

}

NameBean.java

import java.rmi.*;

import javax.ejb.*;

import java.sql.*;

import java.util.*;

public class NameBean implements EntityBean {

EntityContext ctx;

public String name;

public String ejbCreate(String name) throws CreateException {

this.name = name;

return null;

}

public void ejbPostCreate(String name) throws CreateException {

}

public void ejbLoad() {

}

public void ejbStore() {

}

public void ejbRemove() throws RemoveException {

}

public void ejbActivate() {

}

public void ejbPassivate() {

}

public void setEntityContext(EntityContext ctx) {

this.ctx = ctx;

}

public void unsetEntityContext() {

ctx = null;

}

public String getName() throws RemoteException {

return name;

}

public void setName(String name) throws RemoteException {

this.name = name;

}

}

写完以后调试一下整个工程,看看有没有错误

4.到weblogic里面去配置连接池和数据源

打开weblogic的服务和控制台,点jdbc-conncetion pools,然后新建立一个连接池,

我的配置如下:

name:myConnectionPool

url:jdbc:odbc:finance

drive class:sun.jdbc.odbc.JdbcOdbcDriver

Properties:user=sa;password=

然后target里面选上myserver

然后连数据源jdbc-datasource,如下:

name:finance

jndi name: finance

pool name: myConnectionPool

然后target里面选上myserver

然后点ejb-选刚才我们建立的bean:contain,还是把target里面选上myserver

如果配置的时候碰到什么困难,察看一下weblogic的帮助,里面写得非常详细!

5.到jbuilder里面去写客户端代码

client.java

import javax.naming.*;

import java.util.Properties;

import javax.rmi.PortableRemoteObject;

public class NameTestClient1 {

private NameHome nameHome = null;

//Construct the EJB test client

public NameTestClient1() {

try {

//get naming context

Context ctx = getInitialContext();

//look up jndi name

Object ref = ctx.lookup("Name");

//cast to Home interface

nameHome = (NameHome) PortableRemoteObject.narrow(ref, NameHome.class);

nameHome.create("sssss");

}

catch(Exception e) {

e.printStackTrace();

}

}

private Context getInitialContext() throws Exception {

String url = "t3://localhost:7001";

String user = null;

String password = null;

Properties properties = null;

try {

properties = new Properties();

properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");

properties.put(Context.PROVIDER_URL, url);

if (user != null) {

properties.put(Context.SECURITY_PRINCIPAL, user);

properties.put(Context.SECURITY_CREDENTIALS, password == null ? "" : password);

}

return new InitialContext(properties);

}

catch(Exception e) {

System.out.println("Unable to connect to WebLogic server at " + url);

System.out.println("Please make sure that the server is running.");

throw e;

}

}

//----------------------------------------------------------------------------

// Utility Methods

//----------------------------------------------------------------------------

public NameHome getHome() {

return nameHome;

}

//Main method

public static void main(String[] args) {

NameTestClient1 client = new NameTestClient1();

// Use the getHome() method of the client object to call Home interface

// methods that will return a Remote interface reference. Then

// use that Remote interface reference to access the EJB.

}

}

我这里只写了一个create(“ssss”),执行以后看看数据库是不是在数据库name表里面增加一条ssss的记录了,那就成功了

这就是一个最基本的容器管理的实体bean,如果以上调试你都成功了,那么就可以加大bean的功能了,写上各种find方法和remove功能对数据库进行查询,增加,删除,修改的功能,祝各位好运!

如果有什么纰漏或则问题请各位与孤独的cat联系!!!

责任编辑:admin
相关文章