昨日在网上查找Tomcat5.5 连接池设置,发现网上很多人把错的内容转来转去。现将网上内容整理发布。
内容主要来自http://blog.chinaunix.net/u/4296/showart_66056.html
1、修改%TOMCAT_HONE%/CONF/SERVER.XML文件,在<host></host>节点下添加以下内容:
xml 代码
- <Context crossContext="true" debug="5" docBase="BBS"
- path="/BBS" reloadable="true">
- <Resource name="jdbc/BBS"
- type="javax.sql.DataSource" auth="Container"
- driverClassName="com.mysql.jdbc.Driver" maxActive="100"
- maxIdle="30" maxWait="10000"
- url="jdbc:mysql://localhost:3306/userdb?autoReconnect=true"
- username="root" password="" />
- Context>
2、修改工程下的WEB.XML文件,添加以下内容:
xml 代码
- <resource-ref>
- <description>JNDI DataSource Testdescription>
- <res-ref-name>dbc/BBSres-ref-name>
- <res-type>javax.sql.DataSourceres-type>
- <res-auth>Containerres-auth>
- <res-sharing-scope>Shareableres-sharing-scope>
- resource-ref>
3、将MySql驱动文件添加到%TOMCAT_HONE%/comman/lib目录下
4、进行代码编写
4.1、ConnectionPool类,实现数据库操作
- import java.sql.*;
- import javax.sql.DataSource;
- import javax.naming.InitialContext;
- public class ConnectionPool {
- public void doSomething() throws Exception{
- Connection conn = null;
- InitialContext ctx = new InitialContext();
- DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/BBS");
- Statement stmt = null;
-
- try{
- conn = ds.getConnection();
-
- stmt = conn.createStatement();
- stmt.execute("delete from user");
- stmt.execute("insert into user(uid,password,realName,gender,email,tel,question,validateCode,answer,loginNum) values('1','2','3',4,'5','6','7',8,'9',10)");
- stmt.close();
- stmt = null;
- conn.close();
- conn = null;
- }finally{
- if(stmt!=null){
- try{
- stmt.close();
- }catch(SQLException sqle){
-
- }
- stmt = null;
- }
-
- if(conn != null){
- try{
- conn.close();
- }catch(SQLException sqle){
-
- }
- conn = null;
- }
- }
-
- }
-
- public static void main(String args[]){
- try{
- new ConnectionPool().doSomething();}
- catch(Exception e){
- e.printStackTrace();
- }
- }
- }
4.2、InsertServlet servlet文件,实现调用数据库功能类并实现跳转
java 代码
- import java.io.IOException;
- import java.io.PrintWriter;
-
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpSession;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
-
- public class InsertServlet extends HttpServlet {
-
-
-
-
-
-
-
-
-
-
-
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
-
- ConnectionPool pool = new ConnectionPool();
- try{
- pool.doSomething();
- request.setAttribute("success", "Success");
-
- }catch(Exception e){
- request.setAttribute("success", "failed");
- e.printStackTrace();
- }
-
- this.getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
-
- }
-
-
-
-
-
-
-
-
-
-
-
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
-
- doGet(request,response);
- }
-
- }
4.3 index.jsp文件 进行简单的页面输出功能
jsp 代码
- <!---->
- <!---->
-
- <!---->"-//W3C//DTD HTML 4.01 Transitional//EN">
-
-
- "<!---->">
-
-
- "pragma" content="no-cache">
- "cache-control" content="no-cache">
- "expires" content="0">
- "keywords" content="keyword1,keyword2,keyword3">
- "description" content="This is my page">
- <!---->
-
-
-
- <!---->
-
-
5、多个web应用共享数据库连接池。
5.1、修改%TOMCAT_HONE%/CONF/SERVER.XML文件,在<host></host>节点下添加以下内容:
- <Context path="/dbtest" docBase="dbtestweb" debug="5" reloadable="true" crossContext="true">
- <ResourceLink name="jdbc/Tours" global="jdbc/Tours" type="javax.sql.DataSource"/> Context>
此处用到了ResourceLink,连接全局的Resource
分享到:
相关推荐
Tomcat5.5连接池配置(oracle mysql sqlserver2000)
一个jsp案例,介绍Tomcat5.5连接池的配置,内有案例笔记供参考,新手也能一看就会
tomcat5.5连接池配置
Tomcat5.5连接池配置,内详
Tomcat 5.5数据库连接池配置
tomcat5.5.X域名转向和连接池配置的server.xml文件,用于项目的部署,绝对使用,不用再暴走baidu和google
tomcat5.5配置数据库连接池
Tomcat关于jdbc连接池的配置 包含tomcat6.0与tomcat5.5
tomcat5.5配置数据库连接池.part1
tomcat5.5配置数据库连接池.part2.rar
tomcat5.5配置数据库连接池.part3.rar
Tomcat5.5.X下配置连接池
tomcat5.5配置mysql的连接池.pdf
tomcat5.5+sql2000数据库连接池配置PDF说明文档 一档在手,再也难不倒你啦,哈哈
tomcat各版本连接池配置方法【tomcat5.0,tomcat5.5x,tomcat6.0】
把驱动程序拷贝到Tomcat 5.5commonlib目录下
tomcat5.5+iis6+jkconnetctor简单配置飞扬清风
这个环境下的数据库连接池我搞了几天。终有所成,希望同志们不要走太多的弯路。 网上的太乱了!