java应用程序JNDI方式使用weblogic连接池的方法

第一步准备工作:首先需要明确一点,要获取weblogic的连接就必须依赖weblogic相关jar,不是网上所说的weblogic.jar,如果你把weblogic.jar配置在你的classpath里面是不够的,因为会报出java类文件找不到的错误诸如[Caused by: weblogic.security.subject.AbstractSubject
java.lang.NoClassDefFoundError: weblogic/security/subject/AbstractSubject]之类的异常。那么我们怎么办呢,其实很简单只要进入到你webloigc_home路径下进入到server/lib目录,并且运行[jdk1.6 命令“java -jar wljarbuilder.jar”;jdk1.5命令“java -jar wljarbuilder.jar -profile wlfullclient5 ”],拷贝“wlfullclient.jar或者wlfullclient5.jar”到你的classpath环境下;
第二步编码工作:新建一个java工程,新建一个java类,命名为Connector.java;首先我们初始化获取数据库连接的环境,即初始化context;
Hashtable<String, String> ht=new Hashtable<String, String>();
ht.put(Context.INITIAL_CONTEXT_FACTORY,INITIAL_CONTEXT_FACTORY);
ht.put(Context.PROVIDER_URL, PROVIDER_URL);
try {
long t1=System.currentTimeMillis();
_context=new InitialContext(ht);
long t2=System.currentTimeMillis();
System.out.println(“initial the context cost:”+(t2-t1)+” millseconds!”);
} catch (NamingException e) {
System.out.println(“initial context failured with Exception:”+e.getMessage());
e.printStackTrace();
}
接下来从context上查找JNDI获取到Datasource对象:javax.sql.DataSource ds=(javax.sql.DataSource)ctx.lookup(JNDI);
再就是从Datasource中得到java.sql.Connection对象了。ds.getConnection();
3 具体的示例代码请参考如下内容,笔者将源码完全贴出:
import java.sql.*;
import java.util.*;
import javax.naming.*;
/**
* <pre>
* to get the connections of weblogic connection pool by JNDI
* the required jars include “wlfullclient.jar”,
* I know you will ask me how to get this jar? the following is the answer:
* first you must has installed the weblogic,WL_HOME means the weblogic setup directory.
* (a).Creating a wlfullclient.jar for JDK 1.6 client applications
* ===================================================================
* 1.Change directories to the server/lib directory.
* cd WL_HOME/server/lib
* 2.Use the following command to create wlfullclient.jar in the “server/lib” directory:
* java -jar wljarbuilder.jar
* this may take a few minutes ,just wait!
* 3.You can now copy and bundle the wlfullclient.jar with client applications.
* 4.Add the wlfullclient.jar to the client application’s classpath.
* ===================================================================
* (b).Creating a wlfullclient5.jar for JDK 1.5 client applications
* ===================================================================
* 1. the same as (a).1
* 2. Use the following command to create wlfullclient.jar in the server/lib directory:
* java -jar wljarbuilder.jar -profile wlfullclient5
* 3. the same as (a).3
* 4. the same as (a).4
* ===================================================================
*
*
* </pre>
* @author ubuntu
*
*/
public class Connector {
/**
* the const variable of initial context Factory,this can’t modified!
*/
final static String INITIAL_CONTEXT_FACTORY=”weblogic.jndi.WLInitialContextFactory”;
/**
* the const variable of the t3 provider URl,this need to modified by your own !
*/
final static String PROVIDER_URL= “t3://10.254.0.9:7001″;
/**
* the const variable of the JNDI name ,this need to modified by your own !
*/
final static String JNDI=”jdbc/powererp”;
/**
* the method that get Connecton from weblogic JNDI
* @return the Connection Object
*/
public Connection getConnection(){
Context ctx=null;
Connection con=null;
try{
long t1=System.currentTimeMillis();
ctx=_context;
long t2=System.currentTimeMillis();
//System.out.println(“initial the context cost:”+(t2-t1)+” millseconds!”);
javax.sql.DataSource ds=(javax.sql.DataSource)ctx.lookup(JNDI);
long t3=System.currentTimeMillis();
System.out.println(“get the datasource cost:”+(t3-t2)+” millseconds!”);
con=ds.getConnection();
long t4=System.currentTimeMillis();
System.out.println(“get the connection cost:”+(t4-t3)+” millseconds!”);
}catch(Exception e){
e.printStackTrace();
}
return con;
}
static Context _context=null;
static{
Hashtable<String, String> ht=new Hashtable<String, String>();
ht.put(Context.INITIAL_CONTEXT_FACTORY,INITIAL_CONTEXT_FACTORY);
ht.put(Context.PROVIDER_URL, PROVIDER_URL);
try {
long t1=System.currentTimeMillis();
_context=new InitialContext(ht);
long t2=System.currentTimeMillis();
System.out.println(“initial the context cost:”+(t2-t1)+” millseconds!”);
} catch (NamingException e) {
System.out.println(“initial context failured with Exception:”+e.getMessage());
e.printStackTrace();
}
}
/**
* the method that test whether the connection is correct got
* @param con
*/
private void testconn(Connection con){
ResultSet rs=null;
Statement stmt=null;
String sql=”select sysdate from dual”;
if(con==null)throw new RuntimeException(“the connection is null.please check!”);
try {
stmt=con.prepareStatement(sql);
rs=stmt.executeQuery(sql);
if(rs!=null&&rs.next()){
String sysdate=rs.getString(1);
System.out.println(“the oracle db sysdate is :”+sysdate);
}
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(“the excepiton occured:”+e.getMessage());
}finally{
try{
if(rs!=null)rs.close();
if(stmt!=null)stmt.close();
}
catch(Exception e){}
}
}
/**
* test the connection by jndi
*/
private void invokeTestDbConnectionByJndi(){
Connection con=null;
try{
long tstart=System.currentTimeMillis();
con= getConnection();
long tend=System.currentTimeMillis();
System.out.println(“execute the method getConnection() cost:”+(tend-tstart)+” millseconds!”);
testconn(con);
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(“when execute invokeTestDbConnectionByJndi(),” +
“the excepiton occured:”+e.getMessage());
}finally{
try{
if(con!=null)con.close();
}
catch(Exception e){}
}
}
/**
* the main method
* @param args args allow null
*/
public static void main(String[] args) {
Connector cct=new Connector();
cct.invokeTestDbConnectionByJndi();
cct.invokeTestDbConnectionByJndi();
}
//—————-the following his -the test result :———————//
/**
*
* initial the context cost:5666 millseconds!
* get the datasource cost:225 millseconds!
* get the connection cost:968 millseconds!
* execute the method getConnection() cost:1193 millseconds!
* the oracle db sysdate is :2010-08-24 13:31:50.0
* get the datasource cost:4 millseconds!
* get the connection cost:33 millseconds!
* execute the method getConnection() cost:37 millseconds!
* the oracle db sysdate is :2010-08-24 13:31:51.0
*
*/
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值