`

在Weblogic9.2里添加JDBC连接,并用JNDI名称进行调用

阅读更多

基本思路:在weblogic9.2里配置数据库连接然后通过Jndi名称在后台查找得到DataSourse数据源,创建连接对数据库进行查询操作,weblogic数据源配置操作步骤在附件里面。

工具版本:Weblogic9.2  、Mysql4.1

jar:工程里需要引入一个weblogic.jar 具体路径在你的weblogic 安装目录下 \  bea \weblogic92 \ server \ lib

 

 

package jndi;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

public class TestJndi {

	
 static public void main(String[] args) throws Exception{
	@SuppressWarnings("unused")
     Hashtable table = new Hashtable();
	 table.put(Context.INITIAL_CONTEXT_FACTORY, 
			"weblogic.jndi.WLInitialContextFactory");
         table.put(Context.PROVIDER_URL, 
    		  "t3://localhost:7001");
    Context ctx=new InitialContext(table) ;  //获得初始化环境(初始上下文)
    DataSource date= (DataSource)ctx.lookup("xxxhhhfff") ;  //这个就是在Jndi的名字
    Connection conn =date.getConnection();
    System.out.println(conn.isClosed()) ;
    PreparedStatement stm=conn.prepareStatement
    ("select * from users") ;
    ResultSet res=stm.executeQuery() ;
    while(res.next()){
    	System.out.println(res.getString("id")+"==="
    			+res.getString("username")) ;
    	  
     }
  }
}
 

 

分享到:
评论
1 楼 xiaofan_0204 2011-03-27  
太不详细了!

相关推荐

Global site tag (gtag.js) - Google Analytics