Advantage: Because the HTTP protocol Web program is stateless, when the application uses JDBC, multiple request processing clients will reestablish the connection and will consume a lot of resources if the request is busy.
The database connection pool is responsible for allocating, managing, and freeing the database connection, which allows the application to reuse an existing database connection instead of re-establishing it.
To configure a global database connection pool:
To configure a local database connection pool:
In the Tomcat installation directory, Conf\server.xml
<GlobalNamingResources> under Tags
Add to
<resource name= "jdbc/webdb" auth= "Container" type= "Javax.sql.DataSource" driverclassname= " Com.mysql.jdbc.Driver " url=" Jdbc:mysql://localhost:3306/shop?characterencoding=utf-8 " username=" root " password= "root" maxactive= "maxidle=" maxwait= " />"
In Conf\catalina\localhost
To create a web-named XML
<?xml version= ' 1.0 ' encoding= ' utf-8 '? ><context Path = "/webdemo" docbase= "Webdemo" debug= "0" >< ResourceLink name= "jdbc/webdb" global= "jdbc/webdb" type= "Javax.sql.DataSource"/></context>
Create a servlet
Package Com.demo.servlet;import Java.io.ioexception;import Java.io.printwriter;import java.sql.Connection;import Java.sql.preparedstatement;import Java.sql.resultset;import Javax.naming.context;import Javax.naming.initialcontext;import Javax.naming.namingexception;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Javax.sql.datasource;public class Helloworldservlet extends HttpServlet {public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, ioexception{//Sets the ContentType field value of the HTTP response header Response.setcontenttype ("text/html; Charset=utf-8 ");//Gets the PrintWriter object used to input the output message printwriter out = Response.getwriter (); try {//or Context object Context context = New InitialContext ();D atasource ds = (DataSource) context.lookup ("java:/comp/env/jdbc/webdb");// Get Connetion Object Connection connection = Ds.getconnection (); PreparedStatement statement = Connection.prepArestatement ("SELECT * from Memberlevel"); ResultSet rs = Statement.executequery (); StringBuffer table = new StringBuffer () table.append ("<table border= ' 1 ' >"); Table.append ("<tr><td> Id</td><td> level </td><td> points </td></tr> "), while (Rs.next ()) {Table.append (" <tr ><td> "+rs.getstring" ("ID") + "</td><td>" +rs.getstring ("levelname") + "</td><td>" + Rs.getstring ("favourable") + "</td></tr>");} Table.append ("</table>"); Out.println (table.tostring ());} catch (Exception e) {out.println (E.getmessage ());}}}
Add the MySQL jar package to Lib
Web. XML configuration
After the request
Http://localhost:8080/webdemo/servlet/HelloWorldServlet
Results:
Configure the local database connection pool to replace the contents of the Web-named XML
<context Path = "/webdemo" docbase= "Webdemo" debug= "0" >
。。。。。。
Configuration
</Context>
Configure the Tomcat server data connection pool