tomcat connection pool(mysql)

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,则登录成功,反之失败。



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值