当前位置: 代码迷 >> SQL >> 用tomcat施用连接池连接到MSSQL(转)
  详细解决方案

用tomcat施用连接池连接到MSSQL(转)

热度:70   发布时间:2016-05-05 13:09:19.0
用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"/>
Javaif.Com
?
<ResourceParams?name="jdbc/pmsdb_bh">

???????????
<parameter>
?????????????
<name>factory</name>

Javaif.Com


?????????????
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
???????????
</parameter>
???????????
<parameter>
Javaif.Com

????????????????
<name>maxActive</name>
????????????????
<value>100</value>
???????????
</parameter>?
???????????
<parameter>
????????????????
<name>maxIdle</name>
????????????????
<value>30</value>

?


???????????
</parameter>

???????????
<parameter>
???????????????
<name>maxWait</name> Java世界
???????????????
<value>10000</value>
???????????
</parameter>

????????????
<parameter>


?????
<name>username</name>
?????
<value>sa</value>
????
</parameter> Javaif.Com
????
<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>

Java世界


??????
<value>jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename=pmsdb_bh</value>
????
</parameter>


????
</ResourceParams>? Java世界


???????
</Context>

? Javaif.Com

3. web.xml 代码
? Java世界

<web-app>
????
<display-name>Struts?Examples?Application</display-name>
?
?
<!--?开始?-->

<resource-ref>
?????
<description>DB?Connection</description>
?????
<res-ref-name>jdbc/pmsdb_bh</res-ref-name> Java世界
?????
<res-type>javax.sql.DataSource</res-type>
?????
<res-auth>qjz</res-auth>
?
</resource-ref> Javaif.Com
?
<!--?结束?-->


??
4. 写一个类 Javaif.Com

?

Java世界

?package?zziss.db;
import?javax.naming.*;
import?java.sql.Connection;
import?java.lang.String;

Javaif.Com


import?javax.sql.DataSource;
public?class?ConnPool...{
Javaif.Com

??????
private?Connection?aConn;
??????
private?String?errorMessage;
??????
public?ConnPool()...{?
??????????
??????????
try...{

Java世界


??????????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?";
??????????}

??????????}
Java世界
??????????
catch(Exception?e)...{

?


?????????????
this.errorMessage?=?e.toString();
??????????}

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

??????
public?String?getErrorMessage()...{

?


??????????
return?this.errorMessage;
??????}

??????
public?Connection?getConnection()...{

Java世界


??????????
return?this.aConn;
??????}

??????
}
?

? Javaif.Com

??5. 调用


Javaif.Com

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

?
<%...

?


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

?
<h2>Ms?sql?server?2000?java?search?Results</h2>

?


????
<%=?cp.getErrorMessage()?%><br/>
??

?
</body>
</html>

?



?

?

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

  相关解决方案