首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

JBuilder 6.0 整合 WebLogic 6.0 實戰 EJB 開發與部署全流程

     編輯:Java 相關知識

本文詳細記錄基於 JBuilder 6.0 與 WebLogic 6.0 的 EJB 1.1 開發部署完整實踐,涵蓋環境搭建、Session Bean 開發、客戶端測試及常見問題排查,為維護 legacy 系統的開發者提供寶貴參考。

一、開發環境搭建

1. WebLogic 6.0 數據源配置

Connection Pool 創建

  1. 拷貝 classes12.zip (Oracle Thin Driver) 至 %WL_HOME%\config\mydomain\lib\
  2. 修改 setenv.cmd:set CLASSPATH=...;%WL_HOME%\config\mydomain\lib\classes12.zip
  3. 控制台創建 Pool:TestConnectionPool
    • URL: jdbc:oracle:thin:@127.0.0.1:1521:oracle
    • Driver: oracle.jdbc.driver.OracleDriver
    • Properties: user=scott; password=tiger; dll=ocijdbc8; protocol=thin
  4. Target 選擇服務器,Apply 生效

Data Source 創建

  • JNDI Name: CRMDBSource
  • Pool Name: TestConnectionPool
  • Target 綁定服務器

Classpath 擴展

set APPLICATIONS=.\applications
set CLIENT_CLASSES=.\applications\testfile
set EX_WEBAPP_CLASSES=%WL_HOME%\config\mydomain\applications\testfile
set CLASSPATH=...;%CLIENT_CLASSES%;%EX_WEBAPP_CLASSES%

2. JBuilder 6.0 整合配置

  • 安裝 Borland AppServer 4.5(前置依賴)
  • Enterprise Setup 配置:
    • BAS 4.5 安裝路徑
    • WebLogic 6.x 安裝路徑、BEA Home、Domain (mydomain)、Server (myserver)、System 密碼
  • 專案屬性添加:WebLogic 6.x Client、j2ee.jar
  • 目標服務器選擇:WebLogic Application Server 6.x
  • 關鍵修復:系統環境變量設置 TEMP=TMP=D:\temp,解決 JAR 生成報錯

二、Session Bean 開發實戰

1. 向導創建 EJB Module

EJB 向導步驟1 EJB 向導步驟2 EJB 向導步驟3 EJB 向導步驟4

自動生成:Remote/Home 接口、Bean 類、ejb-jar.xml、weblogic-ejb-jar.xml,支持可視化編輯。

2. 通用 SQL 執行框架設計

SQL 上下文接口

public interface DefaultSQLContext {
    String getSQL();
    String[] getParameterType();
    int getType(); // 0=查詢, 1=更新
}

具體 SQL 實現

public class TestSQL implements DefaultSQLContext {
    public String getSQL() {
        return "select baseitemid,datasource,baseitemname,maxlength,description from ctm_baseitem where businesstypeid=?";
    }
    public String[] getParameterType() { return new String[]{"varchar"}; }
    public int getType() { return 0; }
}

3. Session Bean 核心實現

Remote 接口

public interface HelloWorld extends EJBObject {
    void execute() throws RemoteException;
    Vector getQueryResult() throws RemoteException;
}

Bean 實現類

public class HelloWorldBean implements SessionBean {
    private SessionContext sessionContext;
    private DefaultSQLContext sql;
    private String sqlContext = "";
    private Connection con = null;
    private Context initCtx = null;
    private Vector vResult, vColumnName;
    private int iReturn = 1;
    String[] paraValue;

    public void init() {
        try {
            initCtx = new InitialContext();
            DataSource ds = (DataSource) initCtx.lookup("DBSource");
            con = ds.getConnection();
        } catch (Exception e) { System.out.println(e); }
    }

    private void initSQLContext() {
        init();
        try {
            Class clazz = Class.forName(sqlContext);
            sql = (DefaultSQLContext) clazz.newInstance();
        } catch (Exception e) { System.out.println("cannot initialize instance" + sqlContext); }
    }

    public void execute() {
        initSQLContext();
        try {
            PreparedStatement ps = con.prepareStatement(sql.getSQL());
            // 參數綁定邏輯:根據類型設置 varchar/int/long/date/boolean/double
            if (sql.getType() == 0) { // 查詢
                ResultSet rs = ps.executeQuery();
                ResultSetMetaData rsm = rs.getMetaData();
                int colCount = rsm.getColumnCount();
                vResult = new Vector();
                while (rs.next()) {
                    Vector row = new Vector();
                    for (int j = 1; j <= colCount; j++) {
                        Object o = rs.getObject(j);
                        row.addElement(o == null ? "" : o.toString());
                    }
                    vResult.addElement(row);
                }
                vColumnName = new Vector();
                for (int i = 1; i <= colCount; i++) vColumnName.addElement(rsm.getColumnName(i));
            } else { // 更新
                iReturn = ps.executeUpdate();
                con.commit();
            }
            ps.close();
        } catch (Exception e) { con.rollback(); System.out.println(e); }
    }

    // ejbCreate 注入 SQL 類名與參數
    public void ejbCreate(String sqlContext, String[] paraValue) {
        this.sqlContext = sqlContext;
        this.paraValue = paraValue;
    }
    // 其他生命週期方法...
}

Home 接口

public interface HelloWorldHome extends EJBHome {
    HelloWorld create(String sqlContext, String[] paraValue) throws RemoteException, CreateException;
}

三、客戶端測試驗證

public class HelloWorldBeanClient1 {
    private static HelloWorldHome helloWorldHome;
    private static HelloWorld hello;

    static {
        Hashtable ht = new Hashtable();
        ht.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
        ht.put(Context.PROVIDER_URL, "t3://localhost:7001");
        Context ctx = new InitialContext(ht);
        Object ref = ctx.lookup("testBean");
        helloWorldHome = (HelloWorldHome) PortableRemoteObject.narrow(ref, HelloWorldHome.class);
        String[] paraValue = new String[]{"16"};
        hello = helloWorldHome.create("hello.TestSQL", paraValue);
    }

    public static void main(String[] args) {
        HelloWorldBeanClient1 client = new HelloWorldBeanClient1();
        Vector colNames = hello.getColumnName();
        Vector result = hello.getQueryResult();
        // 遍歷輸出結果...
    }
}

四、關鍵經驗總結

  • 驅動部署:JDBC 驅動必須放入 WebLogic 域 lib 目錄並配置 classpath
  • JNDI 命名:DataSource JNDI 名稱需與 Bean 代碼 lookup 完全一致
  • 參數綁定:預編譯語句按類型設置參數,防止 SQL 注入與類型轉換錯誤
  • 事務控制:更新操作顯式 commit/rollback,查詢無需事務
  • 環境變量:TEMP/TMP 設置對 JBuilder 生成部署包至關重要

雖然技術棧已過時,但其中分層設計、配置管理、異常處理、資源清理等工程實踐原理仍具普遍指導意義。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved