How to set up and apply Database connection Pool in Tomcat
This article mainly introduces how to set up and apply the database connection pool in Tomcat, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.
Configuration: Tomcat5.5+JEE (jsdk1.5) + WINXP
Or simply talk about the file configuration:
1: modify% tomcat%/conf/server.xml in
Add the following after that.
two。 Modify% tomcat%/conf/context.xm; and add it later
3. Modify tomcat%/conf/web.xml
MySQL DB Connection Pool
Jdbc/DBPool
Javax.sql.DataSource
Container
Shareable
This configuration is almost enough. If you don't understand the details, you can see the article you posted last time.
4. Write a program to test. (write a WEB program)
Mine is a program written by Myeclipse. It's a bit inconvenient not to map from the computer here (I want to cry).
1: write a connection class:
DBPool.java
Package com.test
Import javax.naming.Context
Import javax.naming.InitialContext
Import javax.naming.NamingException
Import javax.sql.DataSource
Public class DBPool {
Private static DataSource pool
Static {
Context env = null
Try {
Env = (Context) new InitialContext () .lookup ("java:comp/env")
Pool = (DataSource) env.lookup ("jdbc/DBPool")
If (pool==null)
System.err.println ("'DBPool' is an unknown DataSource")
} catch (NamingException ne) {
Ne.printStackTrace ()
}
}
Public static DataSource getPool () {
Return pool
}
}
2: write a Servlet:
Some of them are used to connect to the database and display query results.
Mytest.java
Package com.test
Import java.io.IOException
Import java.io.PrintWriter
Import java.sql.*
Import javax.servlet.ServletException
Import javax.servlet.http.HttpServlet
Import javax.servlet.http.HttpServletRequest
Import javax.servlet.http.HttpServletResponse
Public class Mytest extends HttpServlet {
/ * *
* Constructor of the object.
, /
Public Mytest () {
Super ()
}
/ * *
* Destruction of the servlet.
, /
Public void destroy () {
Super.destroy (); / / Just puts "destroy" string in log
/ / Put your code here
}
/ * *
* 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 {
Response.setContentType ("text/html;charset=gb2312")
PrintWriter out = response.getWriter ()
String id= (String) request.getParameter ("id")
Connection con=null
Try {
Con=DBPool.getPool () .getConnection ()
Statement stmt=con.createStatement ()
ResultSet rst=stmt.executeQuery ("select * from userinf where userid='" + id+ "'")
If (rst.next ()) {
Out.println ("
ID number: "+ rst.getInt (" userid "))
Out.println ("
User name: "+ com.test.ASSICTOGBR2312.trans (rst.getString (" name ")
Out.println ("
Address: "+ rst.getString (" address "))
Out.println ("
Birthday "+ rst.getDate (" year "))
}
Else {
Out.println ("without this ID")
Stmt.close ()
}
}
Catch (Exception e) {
E.printStackTrace ()
}
Finally {/ / be sure to pay attention to the closure of the database here, otherwise
/ / think about it for yourself.
Try {
If (contextual null) {
Con.close ()
}
}
Catch (Exception e) {
E.printStackTrace ()
}
}
}
/ * *
* 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)
}
/ * *
* Initialization of the servlet.
*
* @ throws ServletException if an error occure
, /
Public void init () throws ServletException {
/ / Put your code here
}
}
3: write a JSP page:
Myjsp.jsp
My JSP 'MyJsp.jsp' starting page