It must have been those who stepped on the pit before they were eager to fill the pit.
Tomcat database connection pool configuration, originally is a very simple thing, there are a lot of articles on the Internet, but mostly only text description, no Diagram Wizard, for beginners, the information is too large, it is easy to find out where is where, so after bypassing countless bends on the deep pit, This small white decided for the vast number of beginners to write this text and graphic collocation article.
Tools
(1) eclipse-jee-mars-r-win32-x86_64
(2) mysql-5.5.39-winx64
(3) JBDC plug-in: Mysql-connector-java.jar
(4) Tomcat 7.0
put the JDBC plug-in into the Tomcat Lib directory
Modify Tomcat's 2 configuration files
(1) Modify Context.xml
Before the end tag of the context, add:
<Resource name="jdbc/t1" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="root" password="123456" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/t1"/>
My database name is: T1
Table name is: Bnutalk
User name is root
The password is 123456.
You just need to modify the corresponding place, you can add the following effect:
(2) modify Web. XML
Before the end tag of WebApp, add:
<resource-ref> <description>DB Connection</description> <res-ref-name>jdbc/t1</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>
Just change the T1 to your database name.
test is configured successfully
(1) Create a new serve and a dynamic Web project
(2) New one JSP file, name any
The index.jsp code is as follows:
<%@ page language="java" contenttype="text/html" pageencoding= "GBK" import=" java.sql.*,javax.sql.*,javax.naming.* "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" ><html><head><meta http-equiv="Content-type" Content="text/html; Charset=iso-8859-1 "><title>Insert Title here</title></head><body><% Context ctx=new initialcontext (); Connection conn=null; DataSource ds= (DataSource) ctx.lookup ("java:comp/env/jdbc/t1"); Conn=ds.getconnection (); Statement stmt=conn.createstatement (); ResultSet rs=stmt.executequery ("Select Id,age from Bnutalk"); while (Rs. Next ()) {System.out.println (Rs.getint ("id") +","+rs.getint ("age"); } rs.close (); Stmt.close (); Conn.close (); %></body></html>
(3) under SRC, new a package, and a new servlet.
The Serverlet code is as follows:
PackageCom.imooc.servlet;ImportJava.io.IOException;ImportJava.io.PrintWriter;ImportJavax.servlet.ServletException;ImportJavax.servlet.annotation.WebServlet;ImportJavax.servlet.http.HttpServlet;ImportJavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;/** * Servlet Implementation class Myservlet */@WebServlet("/myservlet") Public class myservlet extends httpservlet { Private Static Final LongSerialversionuid =1L/** * @see httpservlet#httpservlet () */ Public Myservlet() {Super();//TODO auto-generated constructor stub}/** * @see httpservlet#doget (httpservletrequest request, httpservletresponse response) */ protected void Doget(HttpServletRequest request, httpservletresponse response)throwsServletexception, IOException {}/** * @see httpservlet#dopost (httpservletrequest request, httpservletresponse response) */ protected void DoPost(HttpServletRequest request, httpservletresponse response)throwsServletexception, IOException { This. doget (Request, response); }}
The contents of the Bnutalk table in my T1 database are:
Let's see if the output matches the database:
Right-click Web Project->run on server
The output results are as follows:
OK, so far, the database connection pool for Tomcat has been configured successfully.
Tomcat Database Connection Pool configuration