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)
tomcat5.5中文手册tomcat5.5中文手册tomcat5.5中文手册tomcat5.5中文手册tomcat5.5中文手册tomcat5.5中文手册tomcat5.5中文手册tomcat5.5中文手册tomcat5.5中文手册tomcat5.5中文手册tomcat5.5中文手册tomcat5.5中文...
tomcat5.5 免安装版+配置教程 tomcat5.5 免安装版+配置教程 tomcat5.5 免安装版+配置教程 tomcat5.5 免安装版+配置教程
tomcat5.5连接池配置
一个jsp案例,介绍Tomcat5.5连接池的配置,内有案例笔记供参考,新手也能一看就会
中文版的Tomcat5.5文档中文版的Tomcat5.5文档
hibernate 3.1+tomcat 5.5.x(配置jndi)hibernate 3.1+tomcat 5.5.x(配置jndi)hibernate 3.1+tomcat 5.5.x(配置jndi)hibernate 3.1+tomcat 5.5.x(配置jndi)hibernate 3.1+tomcat 5.5.x(配置jndi)hibernate 3.1+...
Tomcat5.5连接池配置,内详
tomcat5.5 解压缩后直接可以使用
如何配置tomcat 5.5的admin页面。
WEB应用服务器TOMCAT5.5版本下载
tomcat5.0.x与tomcat5.5.x的配置有较大区别在</context></host>之间加入下列代码:
tomcat5.5安装版
tomcat5.5 dbcp 的配置方式
该安装包包括tomcat5.0和tomcat5.5安装程序
将tomcat5.5 admin部署到 Tomcat7.0上
jdk5.5+tomcat5.5 环境配置 jdk5.5+tomcat5.5 环境配置是 Java 开发中常用的服务器配置,下面将详细介绍其配置过程。 一、下载和安装 jdk 和 tomcat 首先,需要下载最新的 jdk 和 tomcat 软件包。目前,最新的 ...
tomcat5 安装包,适合图方便的人,呵呵
Apache Tomcat 5.5 安装版
tomcat5.5 压缩包 很好用 供大家免费下载