Tomcat6数据库连接池的配置

1.将Mysql和Oracle的jdbc驱动包复制到%TOMCAT_HOME%/lib/目录下。

 

2.将%TOMCAT_HOME%/conf/context.xml文件的代码改写成如下:

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

<Context reloadable="true">

    <WatchedResource>WEB-INF/web.xml</WatchedResource>
        <Resource name="jdbc/oracleds"
            auth="Container"
            type="javax.sql.DataSource"
            maxActive="100"
            maxIdle="30"
            maWait="10000"
            username="scott"
            password="tiger"
            driverClassName="oracle.jdbc.OracleDriver"
            url="jdbc:oracle:thin:@127.0.0.1:1521:orcl" />
           
        <Resource name="jdbc/mysqlds"
            auth="Container"
            type="javax.sql.DataSource"
            maxActive="100"
            maxIdle="30"
            maWait="10000"
            username="root"
            password="tiger"
            driverClassName="com.mysql.jdbc.Driver"
            url="jdbc:mysql://127.0.0.1:3306/blog" />
 
</Context>

3.在Servlet中的数据库连接池调用方法,详见下列代码:

import java.io.IOException;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import org.apache.commons.dbutils.QueryRunner;

public class BlogServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }


    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       
        //解决网页的乱码问题
        request.setCharacterEncoding("UTF-8");
       
        //定义三个变量接收addBlog.jsp传过来的三个参数
        String title = request.getParameter("title");
        String categoryId = request.getParameter("category");
        String content = request.getParameter("content");
       
        //创建数据源连接池
        DataSource ds = null;
        try {
            Context context = new InitialContext();
            ds = (DataSource)context.lookup("java:/comp/env/jdbc/mysqlds");
        } catch (NamingException e) {
            e.printStackTrace();
        }
       
        try {
            String sql = "insert into blog(category_id, title, content, create_time) value(?,?,?,now())";
            QueryRunner qr = new QueryRunner(ds);
            String[] params = {categoryId, title, content};
            int result = qr.update(sql, params);
            String msg = "";
            if(result == 1) {
                msg = "博文添加成功";
            } else {
                msg = "博文添加失败";
            }
           
            request.setAttribute("msg", msg);
            request.getRequestDispatcher("/addBlogResult.jsp").forward(request, response);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值