Tomcat7和mysql连接池的配置

1. 前提

   导入jdbc驱动包,这是个大前提,很重要的。


2.配置context.xml以及web.xml 
  
context.xml:




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


<Context>
 <Resource name="******"--------------------------------------------------------------------------名字任意,但是和下面的一样,,通常为jdbc/***
         auth="Container"
         driverClassName="com.mysql.jdbc.Driver"
         type="javax.sql.DataSource"
         url="jdbc:mysql://localhost:3306/数据库名字"
         username="数据库用户名"
         password="数据库密码"
         maxActive="100" 
         maxIdle="30"
         maxWait="10000" />
</Context>


web.xml:


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0"
  metadata-complete="true">
 
  <display-name>test</display-name> 可要可不要


  <welcome-file-list>
<welcome-file>test.jsp</welcome-file>
  </welcome-file-list>




  <resource-ref>
  <description>DB Connection</description>  可要可不要
  <res-ref-name>******</res-ref-name>--------------------------------------------------------------名字任意,但是和下面的一样,,通常为jdbc/***
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
  </resource-ref>
 
</web-app>
  
3. 重启tomcat服务器


4. 配置contxet.xml和web.xml一定要在eclipse中配置--server中的contxet.xml和web.xml,不能在tomcat中的contxet.xml和web.xml配置,若是在tomcat中配置的话,
每次启用tomcat的时候,会覆盖tomcat中的配置文件的。


5.测试代码


<%@ 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/数据库名字");
    conn = ds.getConnection();
    sql="select * from  数据表";
    stmt=conn.prepareStatement(sql);
    rs=stmt.executeQuery();
    if(rs.next()){
        strMsg="连接ok";
        out.println(rs.getString("数据表中的属性:id等"));
  out.println(rs.getString("数据表中的属性:name等"));
        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>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值