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 >
< 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 >
<!-- 结束 -->
< 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;
}
}
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 >
<% ... @ 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 的帮助文件中也有