Tomcat5.5.15配置MySQL数据库连接池的简易方法

前提:Tomcat5.5.15安装并成功测试,将MySQL的驱动程序放置到%Catalina%/common/lib目录下

1.配置为公共连接池
 登录 Tomcat Web Server Administration Tool,点击左侧的Data Sources。
 
在右侧Available Actions下拉菜单中选择Create New Data Source.
 
输入
JNDI Name:jdbc/mysql
Data Source URL:jdbc:mysql://localhost:3306/db_ga         
JDBC Driver Class:com.mysql.jdbc.Driver
User Name:admin
Password:admin
Max.Active Connections:4
Max.Idle Connections:2
Max.Wait for Connection:5000

注:使用MySQL的官方JDBC驱动,Class=com.mysql.jdbc.Driver;
  URL=jdbc:mysql://localhost:3306/db_ga; 根据自己的情况具体配置
输入完毕,点击“Save”按钮保存。
 点击页面右上侧“Commit Changes”按钮,保存刚才的设置。(这一步很重要哦!)
  接下来打开%Catalina%/webapps/你的项目所在目录/META-INF,打开context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context>
<ResourceLink
    global="jdbc/mysql"
    name="jdbc/mysql"
    type="javax.sql.DataSource"/>
</Context>

注:加黑部分为添加的内容,引用刚才添加的全局DataSource资源

2.配置为针对web应用的连接池

 在%Catalina%/conf目录,打开server.xml
寻找以下内容:

  <GlobalNamingResources>
    <Environment
      name="simpleValue"
      type="java.lang.Integer"
      value="30"/>
    <Resource
      auth="Container"
      description="User database that can be updated and saved"
      name="UserDatabase"
      type="org.apache.catalina.UserDatabase"
      pathname="conf/tomcat-users.xml"
      factory="org.apache.catalina.users.MemoryUserDatabaseFactory"/>

    <Resource
      name="jdbc/mysql"
      type="javax.sql.DataSource"
      password="987654"
      driverClassName="com.mysql.jdbc.Driver"
      maxIdle="2"
      maxWait="5000"
      username="root"
      url="jdbc:mysql://192.168.0.137:3306/db_ga"
      maxActive="4"/>

  </GlobalNamingResources>

加黑部分为使用Tomcat Web Server Administration Tool程序自动产生的部分,将加黑部分剪切至
%Catalina%/webapps/你的项目所在目录/META-INF/context.xml

此时的context.xml内容应为

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

    <Resource
      name="jdbc/mysql"
      type="javax.sql.DataSource"
      password="987654"
      driverClassName="com.mysql.jdbc.Driver"
      maxIdle="2"
      maxWait="5000"
      username="root"
      url="jdbc:mysql://192.168.0.137:3306/db_ga"
      maxActive="4"/>

<ResourceLink
    global="jdbc/mysql"
    name="jdbc/mysql"
    type="javax.sql.DataSource"/>

</Context>

3.编写测试文件:jndi.jsp

<%@ page import = "java.sql.*" %>
<%@ page import = "javax.naming.*" %>
<%@ page import = "javax.sql.*" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>测试JNDI</title>
</head>
<body>
<%
Context ctx=null;
DataSource ds=null;
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;

try{
 ctx = new InitialContext();
 if( ctx == null )
  out.println("no context");
 ds = (DataSource) ctx.lookup("java:comp/env/jdbc/mysql");
 if( ds == null )
  out.println("no datasource");
 conn = ds.getConnection();
 stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
  ResultSet.CONCUR_READ_ONLY);
 String strSql = " select * from t_user";
 rs = stmt.executeQuery(strSql);
 while(rs.next()){
  out.println(rs.getString("username")+"<br>");
 }
}
catch(Exception ex){
 ex.printStackTrace();
 out.println(ex.toString());
}
finally{
 if( rs != null )
  rs.close();
 if( stmt != null )
  stmt.close();
 if( conn != null)
  conn.close();
 if( ctx != null )
  ctx.close();
}
%>

</body>
</html>

测试通过,配置成功!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值