tomcat数据源配置及JNDI使用

关于tomcat   5.5.12   如何配置连接池。 

(1)admin登陆后,选   Resources   /   Data   Source,Create   New   Data   Source,创建JNDI后conf/server.xml   里含有以下内容: 
(url与driverClassName相对应) 
... 
    <GlobalNamingResources> 
        <Environment 
            name= "simpleValue " 
            type= "java.lang.Integer " 
            value= "30 "/> 
        <Resource 
            auth= "Container " 
            description= "User   database   that   can   be   updated   and   saved " 
            name= "UserDatabase " 
            type= "org.apache.catalina.UserDatabase " 
            pathname= "conf/tomcat-users.xml " 
            factory= "org.apache.catalina.users.MemoryUserDatabaseFactory "/> 
        <Resource 
            name= "jdbc/sqlserver " 
            type= "javax.sql.DataSource " 
            password= "111111 " 
            driverClassName= "net.sourceforge.jtds.jdbc.Driver " 
            maxIdle= "2 " 
            maxWait= "5000 " 
            username= "sa " 
            url= "jdbc:jtds:sqlserver://localhost:1433/demo " 
            maxActive= "4 "/> 
    </GlobalNamingResources> 
... 

(2)conf/context.xml含以下标记 

<Context> 
... 
<ResourceLink   name= "jdbc/sqlserver "   global= "jdbc/sqlserver "   type= "javax.sql.DataSource "/> 
</Context> 

 

注意:并且要将jdbc的驱动包放在tomcat的lib中

(3)在webapps下的项目所在的目录的   WEB-INF/web.xml   里含以下标记 

<web-app   version= "2.4 "> 
... 
<resource-ref> 
<description> MSSQLSERVER   Datasource </description> 
<res-ref-name> jdbc/sqlserver </res-ref-name> 
<res-type> javax.sql.DataSource </res-type> 
<res-auth> Container </res-auth> 
</resource-ref> 
</web-app> 

(4)编写测试   test.jsp 
... 
<%@   page   import= "javax.naming.* "   %> 
<%@   page   import= "javax.sql.* "   %> 
<% 
Context   initContext   =   new   InitialContext(); 
Context   envContext     =   (Context)initContext.lookup( "java:/comp/env "); 
DataSource   ds   =   (DataSource)envContext.lookup( "jdbc/sqlserver "); 
Connection   conn   =   ds.getConnection(); 
if(conn!=null){ 
out.print( "Connection   pool   success <br> "); 
try{ 
PreparedStatement   ps   =   conn.prepareStatement( "select   *   from   table1 "); 
ResultSet   rs   =   ps.executeQuery(); 
while(rs.next()){ 
out.print(rs.getString(2)+ " <br> "); 


catch(Exception   e){ 
out.print( "--------   Error:   "+e+ "   ------------- <br> "); 

finally{ 
conn.close(); //释放连接 


%> 

(5)重启动tomcat,MSSQLSERVER   ,测试通过。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值