天天看點

tomcat7下的MySQL資料庫連接配接池的配置

今天花一個上午去鑽研Tomcat7和mysql配置連接配接池,終于成功了,在這和大家分享下此過程

一.設計測試用的資料庫

    1.建立資料庫

     create database testmysql;

    2.建立一個使用者資訊資料表

     create table test(

       username varchar(20) primary key,

       password varchar(20));

    3.給新表插入資料資訊

     insert into test values('keivn','123456');

二. 設計局部資料源和連接配接池

     1.在webapps目錄中建立test目錄,然後在test中分别建立WEB-INF和META-INF目錄,在WEB-INF目錄

 中建立classes和lib目錄,将mysql資料庫驅動文.jar放進lib目錄中

      這裡我使用的版本是mysql-connector-java-5.1.22-bin.jar

    2.在META-INF目錄中建立context.xml檔案,然後将下面内容複制,儲存

     <?xml version='1.0' encoding='utf-8'?>

<!--

  Licensed to the Apache Software Foundation (ASF) under one or more

  contributor license agreements.  See the NOTICE file distributed with

  this work for additional information regarding copyright ownership.

  The ASF licenses this file to You under the Apache License, Version 2.0

  (the "License"); you may not use this file except in compliance with

  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software

  distributed under the License is distributed on an "AS IS" BASIS,

  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

  See the License for the specific language governing permissions and

  limitations under the License.

-->

<!-- The contents of this file will be loaded for each web application -->

<Context>

    <!-- Default set of monitored resources -->

    <WatchedResource>WEB-INF/web.xml</WatchedResource>

    <!-- Uncomment this to disable session persistence across Tomcat restarts -->

    <!--

    <Manager pathname="" />

    -->

    <!-- Uncomment this to enable Comet connection tacking (provides events

         on session expiration as well as webapp lifecycle) -->

    <!--

    <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />

    -->

 <!--Resource name="jdbc/mysql"

          auth="Container"

          driverClassName="com.mysql.jdbc.Driver"

          type="javax.sql.DataSource"

          url="jdbc:mysql://localhost:3306/testmysql"

          username="root" 

          password="admin" 

          maxActive="100"  

          maxIdle="30"

          maxWait="10000" /-->

</Context>

  3.然後在WEB-INF中建立web.xml檔案,然後将下面内容複制,儲存

     <?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

  <display-name>test</display-name>

  <welcome-file-list>

    <welcome-file>index.html</welcome-file>

    <welcome-file>index.htm</welcome-file>

    <welcome-file>index.jsp</welcome-file>

    <welcome-file>default.html</welcome-file>

    <welcome-file>default.htm</welcome-file>

    <welcome-file>default.jsp</welcome-file>

  </welcome-file-list>

  <resource-ref>

  <description>DB Connection</description>

  <res-ref-name>jdbc/mysql</res-ref-name>

  <res-type>javax.sql.DataSource</res-type>

  <res-auth>Container</res-auth>

  </resource-ref>

</web-app>

 4.完成後,重新開機tomcat伺服器。

三.編寫一個jsp頁面測試設定連接配接池,連接配接資料庫是否成功。

 在test目錄中建立test.jsp檔案,然後将下面内容複制,儲存

     <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%@ page import="java.sql.*, javax.sql.*, javax.naming.*" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>test</title>

</head>

<body>

<%

Connection conn=null;

PreparedStatement stmt=null;

ResultSet rs=null;

String sql=null;

String username=null;

String alias=null;

String pwd=null;

String strMsg=null;

try{

    Context cxt = new InitialContext();

    DataSource ds = (DataSource)cxt.lookup("java:/comp/env/jdbc/mysql");

    conn = ds.getConnection();

    sql="select * from  test";

    stmt=conn.prepareStatement(sql);

    rs=stmt.executeQuery();

    if(rs.next()){

        strMsg="連接配接ok";

        out.println(rs.getString("username"));

  out.println(rs.getString("password"));

        out.println("連接配接ok");

    }else{

        out.println("rs.next() fail");

    }

    rs.close();

} catch(Exception e){

    out.println("連接配接失敗:"+e.getMessage());

    //e.printStackTrace();

}

try{

    if(stmt!=null) stmt.close();

}catch(Exception e){}

try{

    if(conn!=null) conn.close();

}catch(Exception e){}

%>

</body>

</html>

使用浏覽器輸入http://localhost:8080/test/test.jsp

頁面顯示kevin 123456 連接配接ok 就表示設定已經成功。

這東西要慢慢專研哦

繼續閱讀