tomcat使用连接池连接到 mssql

tomcat使用连接池连接到 mssql

1. 把 mssql 需要的三个文件拷贝到 java_home/lib/ext 目录中
2. tomcat 的 server.xml 的代码
   

  < Context  path ="/j"  docBase ="D:Downloadsjsp_lx"  reloadable ="true"  crossContext ="true"  debug ="0"    >
               
    
< Resource  name ="jdbc/pmsdb_bh"
              auth
="qjz"
              type
="javax.sql.DataSource" />

 
< ResourceParams  name ="jdbc/pmsdb_bh" >

           
< parameter >
             
< name > factory </ name >
             
< value > org.apache.commons.dbcp.BasicDataSourceFactory </ value >
           
</ parameter >
           
< parameter >
                
< name > maxActive </ name >
                
< value > 100 </ value >
           
</ parameter >
           
< parameter >
                
< name > maxIdle </ name >
                
< value > 30 </ value >
           
</ parameter >

           
< parameter >
               
< name > maxWait </ name >
               
< value > 10000 </ value >
           
</ parameter >

            
< parameter >
     
< name > username </ name >
     
< value > sa </ value >
    
</ parameter >
    
< parameter >
     
< name > password </ name >
     
< value > sa </ value >
    
</ parameter >
    

    
<!--  Class name for mm.mysql JDBC driver  -->
    
< parameter >
       
< name > driverClassName </ name >
       
< value > com.microsoft.jdbc.sqlserver.SQLServerDriver </ value >
    
</ parameter >
    
< parameter >
      
< name > url </ name >
      
< value > jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename=pmsdb_bh </ value >
    
</ parameter >


    
</ ResourceParams >  


       
</ Context >

 

3. web.xml 代码
 

< web-app >
    
< display-name > Struts Examples Application </ display-name >

 
<!--  开始  -->

< resource-ref >
     
< description > DB Connection </ description >
     
< res-ref-name > jdbc/pmsdb_bh </ res-ref-name >
     
< res-type > javax.sql.DataSource </ res-type >
     
< res-auth > qjz </ res-auth >
 
</ resource-ref >
 
<!--  结束  -->


  
4. 写一个类

 

  package  zziss.db;
import  javax.naming. * ;
import  java.sql.Connection;
import  java.lang.String;
import  javax.sql.DataSource;
public   class  ConnPool {
      
private Connection aConn;
      
private String errorMessage;
      
public ConnPool(){
          
          
try{
          Context _context 
= new InitialContext();
          DataSource _ds 
= (DataSource)_context.lookup("java:comp/env/jdbc/pmsdb_bh");
          
if (_ds == null)
          
{
               
this.errorMessage = "无法找到 jndi 名:java:comp/env/jdbc/pmsdb_bh";
          }
 else
          
{
               aConn 
= _ds.getConnection();
               
this.errorMessage = "取得连接对象 connection ";
          }

          }

          
catch(Exception e){
             
this.errorMessage = e.toString();
          }

//System.out.println("ok");
      }

      
public String getErrorMessage(){
          
return this.errorMessage;
      }

      
public Connection getConnection(){
          
return this.aConn;
      }

      
}
 

 

  5. 调用

 

<% @ page contentType="text/html;charset=gb2312" %>
<% @ page import ="zziss.db.*"  %>
<% @ page import ="java.sql.*"  %>
< html >
 
< head >
   
< title > DB Test </ title >
 
</ head >
 
< body >

 
<%
   ConnPool cp 
= new ConnPool();
   
 
%>

 
< h2 > Ms sql server 2000 java search Results </ h2 >
    
<% =  cp.getErrorMessage()  %> < br />
  

 
</ body >
</ html >

 

关于怎么使用连接池在 tomcat 的帮助文件中也有

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值