程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> J2EE >> EJB訪問Weblogic用T3

EJB訪問Weblogic用T3

編輯:J2EE
package com.newmodern.etm.utility;

/**
* Title:
* Description:
* Copyright:    Copyright (c) 2001
* Company: newmodern
* @author Colin
* @version 1.0
*/

import Java.util.*;
import Javax.naming.*;
import Javax.ejb.*;
import Javax.rmi.PortableRemoteObject;
import Javax.sql.*;
import Java.sql.*;

public class EJBUtil {

    private static Context context = null;
    private static final String url = "t3://localhost:7001";
    private static final String JDBC_JNDI = "jdbc/XAOracle";

    public static Object getEJBHome(String lookupName, Class homeClass) {
        try {
            if (context==null) {
                context = getInitialContext();
            }
            Object home = PortableRemoteObject.narrow(
                                context.lookup(lookupName),
                                homeClass);
            return home;
        } catch (NamingException ne) {
            throw new EJBException(ne.getMessage());
        }
    }

    public static Connection getConnection(String lookupName) {
        try {
            if (context==null) {
                context = getInitialContext();
            }
            DataSource ds = (DataSource)PortableRemoteObject.narrow(
                                context.lookup(lookupName),
                                DataSource.class);
            return ds.getConnection();
        } catch (NamingException ne) {
            throw new EJBException(ne.getMessage());
        } catch (SQLException e) {
            throw new EJBException(e.getMessage());
        }
    }

    public static Connection getConnection() {
        try {
            if (context==null) {
                context = getInitialContext();
            }
            DataSource ds = (DataSource)PortableRemoteObject.narrow(
                                context.lookup(JDBC_JNDI),
                                DataSource.class);
            return ds.getConnection();
        } catch (NamingException ne) {
            throw new EJBException(ne.getMessage());
        } catch (SQLException e) {
            throw new EJBException(e.getMessage());
        }
    }

    private static Context getInitialContext() throws NamingException {
        try {
          Properties h = new PropertIEs();
          h.put(Context.INITIAL_CONTEXT_FACTORY,
              "weblogic.jndi.WLInitialContextFactory");
          h.put(Context.PROVIDER_URL, url);
          return new InitialContext(h);
        } catch (NamingException ne) {
            throw ne;
        }
    }

}

我們可以用下面方式調用該類: lookup找Home:

    public void insertCategory(String codeSubscriber, String systemUsed,
        String codeCategory, String codeCategoryGroup, String categoryDesc,
        String actionBy, Timestamp newTimestamp)
        throws ETMDataAccessException, ETMDataAlreadyExistsException {
            try {
                CategoryHome home =(CategoryHome)EJBUtil.getEJBHome(
                "MyMasterCategory",CategoryHome.class);
                home.create(codeSubscriber, systemUsed, codeCategory,
                codeCategoryGroup, categoryDesc, actionBy, newTimestamp);
            } catch (DuplicateKeyException e) {
                throw new ETMDataAlreadyExistsException();
            } catch (RemoteException e) {
                throw new ETMDataAccessException(e);
            } catch (CreateException e) {
                throw new ETMDataAccessException(e);
            }
    }

    String subscriberSql = "Select code_subscriber, subscriber_name " +
        "From subscriber Where status = 'A' ";

    public Collection getSubscriberList() throws ETMDataAccessException {
        Connection con = null;
        PreparedStatement pStmt = null;
        ResultSet res = null;
        SubscriberEntity subscriber = null;
        Collection page = new ArrayList();
        try {
           //getConnection();
            con = EJBUtil.getConnection("jdbc/XAOracle");
            pStmt = con.prepareStatement(subscriberSql);
            SystemLogger.getInstance().logDebug("SQL:"+subscriberSql+"<-START->");
            res = pStmt.executeQuery();
            SystemLogger.getInstance().logDebug("SQL:"+subscriberSql+"<-END->");
            while (res.next()) {
                subscriber = new SubscriberEntity(
                    res.getString("code_subscriber"),
                    res.getString("subscriber_name"));
                page.add(subscriber);
            }
        } catch (SQLException e) {
            throw new ETMDataAccessException(e);
        } finally {
           if (res != null) {
               try {
                   res.close();
               } catch (Exception e) {}
           }
            if (pStmt != null) {
                try {
                    pStmt.close();
                } catch (Exception e) {}
            }
            if (con != null) {
                try {
                    con.close();
                    con = null;
                } catch (Exception e) {}
            }
        }
        return page;
    }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved