您的位置:首页 > 运维架构 > Tomcat

tomcat connection pool(mysql)

2015-06-22 11:00 363 查看
1.简介

org.apache.tomcat.jdbc.pool是tomcat jdbc connection pool,是多线程connection pool,是另一种选择。而且支持java.sql和javax.sql接口,同时支持initSQL属性。

2.支持的jdbc版本:mysql3.23.47、3.23.58、4.0.1alpha、connector/j3.0.11、mm.mysql2.0.14

3.代码实现

A.copy jdbc driver into $TOMCAT_HOME/lib

B.配置Context

    <Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource"

               maxTotal="100" maxIdle="30" maxWaitMillis="10000"

               username="root" password="root" driverClassName="com.mysql.jdbc.Driver"

               url="jdbc:mysql://localhost:3306/test"/>

C.配置web.xml

  <resource-ref>

      <description>MySQL 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>

D.servlet代码

 public void doGet(HttpServletRequest req, HttpServletResponse resp)

                throws ServletException,IOException

    {

      

        resp.setContentType("text/html;charset=gb2312");

      

        String name=req.getParameter("username");

        String pwd=req.getParameter("password");

       DbUtil db=new DbUtil();

       String pwd1=db.getPass(name);

        if(pwd.equals(pwd1))

        {

            resp.sendRedirect("success.html");

        }

        else

        {

            resp.sendRedirect("fail.html");

//            resp.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE,"服务器忙,请稍后再登录!");

        }

      

    }

E.数据库数据读取代码

package com.edu.util;

import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.Statement;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.sql.DataSource;

public class DbUtil {

 public String getPass(String uname){

  String rtn=null;

  

  Context ctx=null;

   DataSource ds=null;

   Connection conn=null;

   Statement stmt=null;

   ResultSet rs=null;

   try{

    ctx = new InitialContext();

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

    conn = ds.getConnection();

    stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);

    String strSql = " select * from weian where name='"+uname+"'";

    rs = stmt.executeQuery(strSql);

    while(rs.next()){ 

     rtn=rs.getString(“password”);

     break;

    }

   } catch(Exception ex) {

    ex.printStackTrace(); 

   }finally{

    try{

     if( rs != null )

     rs.close();

     if( stmt != null )

     stmt.close();

     if( conn != null)

     conn.close();

     if( ctx != null )

     ctx.close();

    }catch(Exception ee){}

   }

   return rtn;

 }

}

F.数据库代码

CREATE TABLE `weian` (

  `Id` int(11) NOT NULL AUTO_INCREMENT,

  `name` char(8) NOT NULL DEFAULT '',

  `password` varchar(255) DEFAULT NULL,

  PRIMARY KEY (`Id`),

  UNIQUE KEY `name` (`name`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

G.数据库数据

id name password

1 weian 1

5 user1 1234

6 user2i 234

4.总结

如果数据库的weian表中,有所输入的数据weian、1234,则登录成功,反之失败。



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: