在tomcat中设置数据库连接池

在tomcat中设置数据库连接池
--------------------------------------------------------------------------------------------------------------------

1、 把mysql驱动jar包mysql-connector-java-5.1.6.jar拷贝到<CATALINA_HOME>/lib目录;
      在MyEclipse中新建web project工程,并且把 mysql-connector-java-5.1.6.jar 加入到工程的buildpath中。
--------------------------------------------------------------------------------------------------------------------

2、 在工程的webroot/META-INF目录下增加context.xml文件(实际反映到<CATALINA_HOME>/webapps/app应用/ 目录下),其内容:
<?xml version="1.0" encoding="UTF-8"?>
<Context  reloadable="true" >
  <Resource name="jdbc/stuDB" auth="Container" type="javax.sql.DataSource"
     maxActive="100" maxIdle="30" maxWait="10000"
     username="root" password="1234"
     driverClassName="com.mysql.jdbc.Driver"
     url="jdbc:mysql://localhost:3306/studb?useUnicode=true&characterEncoding=gbk"/>  
</Context>
--------------------------------------------------------------------------------------------------------------------

3、 在web.xml中引用数据库连接池资源:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd>">

        <resource-ref>
                <description>student DB Connection</description>
                <res-ref-name>jdbc/stuDB</res-ref-name>
                <res-type>javax.sql.DataSource</res-type>
                <res-auth>Container</res-auth>
        </resource-ref>

        <welcome-file-list>
                <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
</web-app>

       说明: 注意   jdbc/stuDB 和 第二步中的context.xml中 resource的name属性的值保持一致。
--------------------------------------------------------------------------------------------------------------------

4、 编写cn.tsp2c.stu.JdbcUtil.java
package cn.tsp2c.stu;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public final class JdbcUtil {

        public static Connection getConnection() {
                DataSource ds = null;

                Context context = null;
                try {
                        context = new InitialContext();
                        ds = (DataSource) context.lookup("java:comp/env/jdbc/stuDB");
                        return ds.getConnection();
                } catch (NamingException e) {
                        e.printStackTrace();
                } catch (SQLException e) {
                        e.printStackTrace();
                }
                return null;
        }
}

      说明: 注意 高亮部分中的 jdbc/stuDB 和 第二步中的context.xml中 resource的name属性的值保持一致。
--------------------------------------------------------------------------------------------------------------------

5、编写index.jsp
<%@ page language="java" import="java.util.*,cn.tsp2c.stu.*" pageEncoding="gbk"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.SQLException"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
   
    <title>My JSP 'index.jsp' starting page</title>
        <meta http-equiv="Content-Type" content="text/html; charset=gbk" />
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
  </head>
  
  <body>
    <form method="post" name="stuform" action="">
    <%!
    Connection conn = null;
    PreparedStatement psmt = null;
    ResultSet rs = null;  
    %>
    <%
    try{
            conn = JdbcUtil.getConnection();
            if(conn == null){
                    //
            }
            String sqlStr = "select * from tb_student";
            psmt = conn.prepareStatement(sqlStr);
            rs = psmt.executeQuery();
    %>
    <table border="1">
        <tr><th>学号</th><th>姓名</th><th>年龄</th><th>性别</th><th>电话</th><th>住址</th></tr>
    <%
            while(rs.next()){
    %>
        <tr>    
                    <td><%= rs.getString("sid")%></td>
                    <td><%= rs.getString("sname")%></td>
                    <td><%= rs.getInt("age")%></td>
                    <td><%= rs.getString("gender")%></td>
                    <td><%= rs.getString("phone")%></td>                    
                    <td><%= rs.getString("address")%></td>
            </tr>
    <%
            }
    }catch(Exception e){
            e.printStackTrace();
    }finally{
            try{
                    rs.close();
                    psmt.close();
                    conn.close();
            }catch(SQLException e){
                    e.printStackTrace();
            }
    }    
    %>
    </table>
    </form>
  </body>
</html>

转载于:https://www.cnblogs.com/lljava/archive/2010/12/13/1904798.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值