配置数据源包括全局数据源和局部数据源 下文第一种是局部数据源,第二种是全局数据源。
关键字: Tomcat5.5 连接池
昨日在网上查找Tomcat5.5 连接池设置,发现网上很多人把错的内容转来转去。现将网上内容整理发布。
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 {
- /**
- * The doGet method of the servlet.
- *
- * This method is called when a form has its tag value method equals to get.
- *
- * @param request the request send by the client to the server
- * @param response the response send by the server to the client
- * @throws ServletException if an error occurred
- * @throws IOException if an error occurred
- */
- 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);
- }
- /**
- * The doPost method of the servlet.
- *
- * This method is called when a form has its tag value method equals to post.
- *
- * @param request the request send by the client to the server
- * @param response the response send by the server to the client
- * @throws ServletException if an error occurred
- * @throws IOException if an error occurred
- */
- 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 现在 server.xml中的<GlobalNamingResources>元素下建立
- <Resource name="jdbc/mysql"
- 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"
5.2
、修改%TOMCAT_HONE%/CONF/SERVER.XML文件,在<host></host>节点下添加以下内容:
- <Context path="/dbtest" docBase="dbtestweb" debug="5" reloadable="true" crossContext="true">
- <ResourceLink name="jdbc/mysql" global="jdbc/mysql" type="javax.sql.DataSource"/> Context>
此处用到了ResourceLink,连接全局的Resource