1. Database Source
The previous JDBC connection steps were:
1. Load the database driver
2. Get the database connection via Drivermanger connection
3. Execute the preparestatement response SQL statement via connection.
4. Close Pstmt,conn
For each access to the database, 1,2,4 is a repetitive operation, how to simplify these operations, which requires a data source.
2. Database Connection Pool
A database connection pool, which is a pre-prepared n ready-to-use database connection in a Web container, which can be released at any time with the application being closed.
3. Corresponding Configuration method
Since the database connection pool is placed through the Web container, it needs to be configured in the Server.xml of the Tomcat Web container
..............
<context path= "/jspstudy" docbase= "E:\Java\jsp_study_web" >//Data resource configuration is for a specific Web application directory
<resource name= "Jdbc/mldn"
Auth= "Container"
Type= "Javax.sql.DataSource"
maxtotal= "100"
Maxidle= "30"
Maxwaitmills= "10000"
Username= "Root"
Password= "123456"
Driverclassname= "Org.gjt.mm.mysql.Driver"
Url= "Jdbc:mysql://localhost:3306/mldn"/>
</Context>
.....
In addition, the Web. XML in the directory of a particular website also needs to be configured as a data source reference
<resource-ref>
<res-ref-name>jdbc/mldn</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
Finally, it can be applied directly in the JSP, or in a servlet.
<% @page contenttype= "text/html" pageencoding= "GBK"%>
<% @page import= "javax.naming.*"%>
<% @page import= "javax.sql.*"%>
<% @page import= "java.sql.*"%>
<title> database connection Pool exercises </title>
<body>
<%
String dsname = "JAVA:COMP/ENV/JDBC/MLDN";
Context CTX = new InitialContext ();
DataSource ds = (DataSource) ctx.lookup (dsname);
Connection conn = Ds.getconnection ();
%>
<%=conn%>
<%
Conn.close ();//means to put the connection back into the pool
%>
</body>
Java Web Learning Note-Tomcat data source