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联系!!!
