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