tomcat5.5 JNDI配置

转载http://extrimlycold20070206121610.iteye.com/blog/518811
tomcat5.5的JNDI设置,主要注意的两点:

1) 驱动程序在server中所放的位置。

2) 配置

配置文件中添加JNDI有好几种方式,不过以下这种方式比较通用一点.

${TOMCAT_HOME}\conf\context.xml中如下设置:

Java代码
1.<Context>  
2. 
3.    <!-- Default set of monitored resources -->  
4.    <WatchedResource>WEB-INF/web.xml</WatchedResource>  
5.      
6.    <!-- Uncomment this to disable session persistence across Tomcat restarts -->  
7.    <!--  
8.    <Manager pathname="" />  
9.    -->  
10. 
11.<Resource  name="jdbc/test"     
12.           type="javax.sql.DataSource"     
13.           password=""     
14.           driverClassName="com.mysql.jdbc.Driver"     
15.           maxIdle="2"     
16.           maxWait="50"     
17.           username="root"     
18.           url="jdbc:mysql://localhost:3306/mynews"     
19.           maxActive="100"/>      
20.</Context> 
<Context>

    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>

    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->

<Resource  name="jdbc/test"  
           type="javax.sql.DataSource"  
           password=""  
           driverClassName="com.mysql.jdbc.Driver"  
           maxIdle="2"  
           maxWait="50"  
           username="root"  
           url="jdbc:mysql://localhost:3306/mynews"  
           maxActive="100"/>   
</Context>

测试程序调用时如下:

Java代码
1.<%@page import="java.util.*,javax.naming.*,java.sql.*,javax.sql.*"%>  
2.<%@page contentType="text/html;charset=utf-8"%>  
3.<%          
4.     Context ctx = new InitialContext();            
5.     String jndiLookUp = "java:comp/env/jdbc/test";   
6.     DataSource ds =(DataSource) ctx.lookup(jndiLookUp);      
7.     Connection con = ds.getConnection();   
8.     ResultSet rs =null;  
9.     Statement stmt =null;  
10.     try{  
11.                  
12.            stmt =con.createStatement();    
13.            rs = stmt.executeQuery("select * from XXX");  
14.            while(rs.next())  
15.           {  
16.             System.out.println(rs.getString(1));  
17.            }  
18.         }catch(Exception e)  
19.     {  
20.        e.printStackTrace();  
21.          
22.     }finally{  
23.       if(rs!=null)  
24.       rs.close();  
25.       if(stmt!=null)  
26.       stmt.close();  
27.       if(con!=null)  
28.       con.close();  
29.       
30.     }      
31.                
32.%> 
<%@page import="java.util.*,javax.naming.*,java.sql.*,javax.sql.*"%>
<%@page contentType="text/html;charset=utf-8"%>
<%       
     Context ctx = new InitialContext();         
     String jndiLookUp = "java:comp/env/jdbc/test";
     DataSource ds =(DataSource) ctx.lookup(jndiLookUp);   
     Connection con = ds.getConnection();
     ResultSet rs =null;
     Statement stmt =null;
     try{
               
            stmt =con.createStatement(); 
            rs = stmt.executeQuery("select * from XXX");
            while(rs.next())
           {
             System.out.println(rs.getString(1));
            }
         }catch(Exception e)
     {
        e.printStackTrace();
       
     }finally{
       if(rs!=null)
       rs.close();
       if(stmt!=null)
       stmt.close();
       if(con!=null)
       con.close();
    
     }   
             
%>

注意jndi的调用中用的是"java:comp/env/jdbc/test".

在程序的web.xml中有这样的定义:

Java代码
1.<resource-ref>      
2.     <res-ref-name>jdbc/test</res-ref-name>      
3.     <res-type>javax.sql.DataSource</res-type>      
4.     <res-auth>Container</res-auth>      
5.     <res-sharing-scope>Shareable</res-sharing-scope>      
6.  </resource-ref>    
<resource-ref>   
     <res-ref-name>jdbc/test</res-ref-name>   
     <res-type>javax.sql.DataSource</res-type>   
     <res-auth>Container</res-auth>   
     <res-sharing-scope>Shareable</res-sharing-scope>   
  </resource-ref>   

java:comp/env是组件的JNDI上下文的名字.

(实际上这个上下文也作为一种资源来处理了,资源查找的过程可以是这样:

jndictxt = ctxt.lookup("java:comp/env")然后用这个jndictxt来查找资源,

ref = jndictxt.lookup("jdbc/test")。)

jdbc/test是资源引用的JNDI名(The jdbc/test string is the JNDI name for the resource reference,这句话可能意味着资源引用实际上也跟资源一样处理成一种JNDI绑定对象了,但是实际上应该不是这样,因为在部署描述符中它是引用名元素。因为译者也不是高手,所以这里的具体实现细节有待读者自己研究了:)所以JDBC的DataSource对象的JNDI名就存储在java:comp/env/jdbc的上下文子对象中。(组件运行环境的上下文层次需要进一步了解)



Java代码
1.import javax.naming.Context;  
2.import javax.naming.InitialContext;  
3.import javax.naming.NamingException;  
4.import javax.sql.DataSource;  
5. 
6. public static DataSource getDataSource(String dataSourceName){  
7.     Context initCtx;  
8.  try {  
9.   initCtx = new InitialContext();  
10.   Context envCtx = (Context) initCtx.lookup("java:comp/env");  
11.   return (DataSource)envCtx.lookup(dataSourceName);  
12.  } catch (NamingException e) {     
13.   e.printStackTrace();  
14.  }  
15.  return null;  
16. 
17.} 

你可能感兴趣的:(java,sql,tomcat,jdbc,SQL Server)