Tomcat 6 通過 JDBC 連接池連接 SQL Server 2000 和 MySQL 5 的設置

SQL Server 2000 連接池配置方法按順序操作如下

1. 下載 SQL Server 2000 JDBC Driver 放在\Tomcat6\lib,我現在使用的是 msbase.jar、mssqlserver.jar、msutil.jar 這3個文件
2. 在 Tomcat 6.0\webapps 下建立文件夾,比如 poolmssql
3. poolmssql 文件夾內建 META-INF 夾,再建 context.xml 文件,內容:
<Context path="/poolmssql" docBase="poolmssql" debug="5"
reloadable="true" crossContext="true">
<Resource name="java/pool_mssql" auth="Container"
   type="javax.sql.DataSource" maxActive="100" maxIdle="30"
   maxWait="10000"
   driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
   url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=master"
   username="sa" password="sa" />
</Context>

4. poolmssql 文件夾內建 WEB-INF 夾,再建 web.xml 文件,內容:(可以不改此文件)

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<resource-ref>
   <description>MsSQL Datasource</description>
   <res-ref-name>jdbc/pool_mssql</res-ref-name>
   <res-type>javax.sql.DataSource</res-type>
   <res-auth>Container</res-auth>
</resource-ref>
<welcome-file-list>
   <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

5.在 poolmssql 文件夾下建立測試程序頁index.jsp,內容:

<%@ page language="java" contentType="text/html;charset=gbk"
import="java.sql.*,javax.sql.*,javax.naming.*"%>
<html>
<head>
   <meta http-equiv="content-type" content="text/html; charset=gkb" />
   <title>JDBC MsSQL連接池</title>
</head>
<body>
   <%
    DataSource ds = null;
    InitialContext ctx = new InitialContext();
    ds = (DataSource) ctx.lookup("java:comp/env/pool_mssql");
    Connection conn = ds.getConnection();
    Statement stmt = conn.createStatement();
    String strSql = "select name from sysdatabases";
    ResultSet rs = stmt.executeQuery(strSql);
    while (rs.next()) {
     out.print(rs.getString(1) + "<br />");
    }
    out.print("連接成功!");
   %>
</body>
</html>

MySQL 5 連接池配置方法

1. 下載 Connector/J 放在\Tomcat6\lib,我現在使用的是 mysql-connector-java-5.0.4-bin.jar
2. 在 Tomcat 6.0\webapps 下建立文件夾,比如 poolmysql
3. poolmysql 文件夾內建 META-INF 夾,再建 context.xml 文件,內容:

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/poolmysql" docBase="poolmysql" debug="5"
reloadable="true" crossContext="true">
<Resource name="java/pool_mysql" auth="Container"
   type="javax.sql.DataSource" maxActive="100" maxIdle="30"
   maxWait="10000" driverClassName="com.mysql.jdbc.Driver"
   url="jdbc:mysql://localhost:3306/information_schema?autoReconnect=true"
   username="root" password="root" />
</Context>

4. poolmssql 文件夾內建 WEB-INF 夾,再建 web.xml 文件,內容:(可以不改此文件)

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<resource-ref>
   <description>MySQL Datasource</description>
   <res-ref-name>jdbc/pool_mysql</res-ref-name>
   <res-type>javax.sql.DataSource</res-type>
   <res-auth>Container</res-auth>
</resource-ref>
<welcome-file-list>
   <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

5.在 poolmssql 文件夾下建立測試程序頁index.jsp,內容:

<%@ page language="java" contentType="text/html;charset=gbk"
import="java.sql.*,javax.sql.*,javax.naming.*"%>
<html>
<head>
   <meta http-equiv="content-type" content="text/html; charset=gbk" />
   <title>JDBC MySQL連接池</title>
</head>
<body>
   <%
    DataSource ds = null;
    InitialContext ctx = new InitialContext();
    ds = (DataSource) ctx.lookup("java:comp/env/pool_mysql");
    Connection conn = ds.getConnection();
    Statement stmt = conn.createStatement();
    String strSql = "show databases";
    ResultSet rs = stmt.executeQuery(strSql);
    while (rs.next()) {
     out.print(rs.getString(1) + "<br />");
    }
    out.print("連接成功!");
   %>
</body>
</html>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章