明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

Jbuilder6+weblogic6.1開(kāi)發(fā)Entity Bean 全攻略(建議加入精華區(qū))

[摘要]Jbuilder6+weblogic6.1開(kāi)發(fā)Entity Bean 全攻略(建議加入精華區(qū))我現(xiàn)在是邊學(xué)邊干,今天總算給做出來(lái)了,反正文檔是寫(xiě)給公司的,就順便拿到網(wǎng)上來(lái)給大家一起分享了,因?yàn)檫@幾天實(shí)在把我弄得很痛苦,碰到好多困難,走了許多彎路,做出來(lái)才發(fā)現(xiàn)原來(lái)如此簡(jiǎn)單,所以發(fā)出來(lái),讓大家少走彎路...
Jbuilder6+weblogic6.1開(kāi)發(fā)Entity Bean 全攻略(建議加入精華區(qū))

我現(xiàn)在是邊學(xué)邊干,今天總算給做出來(lái)了,反正文檔是寫(xiě)給公司的,就順便拿到網(wǎng)上來(lái)給大家一起分享了,因?yàn)檫@幾天實(shí)在把我弄得很痛苦,碰到好多困難,走了許多彎路,做出來(lái)才發(fā)現(xiàn)原來(lái)如此簡(jiǎn)單,所以發(fā)出來(lái),讓大家少走彎路!

1。首先開(kāi)發(fā)環(huán)境是Jbuilder6+weblogic6.1, 數(shù)據(jù)庫(kù)因?yàn)檫@里是測(cè)試,所以用的是sql server,如果用oracle,相應(yīng)的作修改就可以了。(至于Jbuilder6+Weblogic6.1的環(huán)境配置,請(qǐng)察看精華區(qū)我寫(xiě)過(guò)的一篇《JBUILDER6配置weblogic6.0》)

2.這里以一個(gè)容器管理實(shí)體bean為例,在jbuilder里面先建立一個(gè)工程(注意目錄不能帶空格),然后new-Enterprise里面選ejb1.x entity bean modeler,然后new一個(gè)ejb模塊,我這里取名contain,,其他默認(rèn),點(diǎn)ok,然后next,下面就是連接數(shù)據(jù)庫(kù)的一些設(shè)置了!
driver就是數(shù)據(jù)庫(kù)驅(qū)動(dòng)程序,下拉框可以自己選,oracle,sqlserver的,這里用sqlserver,選jdbc-odbc橋:sun.jdbc.odbc.JdbcOdbcDriver,URL: jdbc:odbc:finance (finance是數(shù)據(jù)源,事先應(yīng)該配好,這里就不說(shuō)了),然后是username和password, JNDI name寫(xiě)上finance,然后next,jbuilder開(kāi)始連接數(shù)據(jù),如果連接成功,會(huì)把數(shù)據(jù)庫(kù)當(dāng)中的表顯示出來(lái),我們這里在數(shù)據(jù)里面只建了一個(gè)只有一個(gè)字段的表name,字段名name,varchar型的。選上name,加到selected里面,NEXT,在NEXT,在出來(lái)的畫(huà)面里面給BEAN選擇主健,然后一路NEXT一直到FINISH,這個(gè)時(shí)候JBUILDER就已經(jīng)把本地,遠(yuǎn)程和實(shí)體BEAN的文件給你健好了。

3.三個(gè)文件名字分別為:
Name.java 遠(yuǎn)程接口
NameBean.java 實(shí)體Bean
NameHome.java 本地接口
下面三個(gè)文件的代碼如下:

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;
}
}

寫(xiě)完以后調(diào)試一下整個(gè)工程,看看有沒(méi)有錯(cuò)誤

4.到weblogic里面去配置連接池和數(shù)據(jù)源
打開(kāi)weblogic的服務(wù)和控制臺(tái),點(diǎn)jdbc-conncetion pools,然后新建立一個(gè)連接池,
我的配置如下:
name:myConnectionPool
url:jdbc:odbc:finance
drive class:sun.jdbc.odbc.JdbcOdbcDriver
Properties:user=sa;password=
然后target里面選上myserver

然后連數(shù)據(jù)源jdbc-datasource,如下:
name:finance
jndi name: finance
pool name: myConnectionPool
然后target里面選上myserver

然后點(diǎn)ejb-選剛才我們建立的bean:contain,還是把target里面選上myserver

如果配置的時(shí)候碰到什么困難,察看一下weblogic的幫助,里面寫(xiě)得非常詳細(xì)!

5.到j(luò)builder里面去寫(xiě)客戶(hù)端代碼

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.
}
}

我這里只寫(xiě)了一個(gè)create(“ssss”),執(zhí)行以后看看數(shù)據(jù)庫(kù)是不是在數(shù)據(jù)庫(kù)name表里面增加一條ssss的記錄了,那就成功了

這就是一個(gè)最基本的容器管理的實(shí)體bean,如果以上調(diào)試你都成功了,那么就可以加大bean的功能了,寫(xiě)上各種find方法和remove功能對(duì)數(shù)據(jù)庫(kù)進(jìn)行查詢(xún),增加,刪除,修改的功能,祝各位好運(yùn)!

如果有什么紕漏或則問(wèn)題請(qǐng)各位與孤獨(dú)的cat聯(lián)系!!!