Java Web Learning Note-Tomcat data source

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.