EJB访问Weblogic用T3

来源:互联网 发布:淘宝怎么给卖家留言 编辑:程序博客网 时间:2024/05/16 18:24
<script type="text/javascript">google_ad_client = "pub-8800625213955058";/* 336x280, 创建于 07-11-21 */google_ad_slot = "0989131976";google_ad_width = 336;google_ad_height = 280;//</script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
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;    }
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 四氧化三铁和氢气 氢氧化钠与水反应 水氢发动机 水氢汽车 饱和食盐水吸收氯化氢 水氢汽车破产申请被驳回 氢动力水 水氢车 氢氧化钠溶于水放热还是吸热 氢氧化钠水溶液 氢水机的价格 水电离的氢离子和氢氧根离子 氢氧化钾溶于水吗 二水磷酸氢钙 过氧化氢是双氧水吗 氢氧化钙和水反应 无水磷酸氢二钠 水是由氢原子和氧原子构成的 氢氧化钠和水反应的方程式 青年汽车集团回应水氢车 氢燃料 氢离子环保油达标吗 氢气球能飞多高 液态氯化氢能导电吗 氢能源为什么不能推广 氢能汽车 氢能油配方 氢能产业 氢能是可再生能源吗 氢能燃料电池 氢脆 氢脆现象 核磁共振氢谱图怎么看 核磁共振氢谱图谱解析 核磁共振氢谱化学位移对应表 氨酚氢考酮片 邻二甲酸氢钾 氢氰酸 偏铝酸根和碳酸氢根 邻苯二甲酸氢钾氢氧化钠 邻苯二甲酸氢钾相对分子质量