• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

EJB访问Weblogic用T3

发布: 2007-6-21 16:03 | 作者:   | 来源:   | 查看: 133次 | 进入软件测试论坛讨论

领测软件测试网

   

CCCCCC">



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

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网