JDBC Application advanced in servlet (v)

Source: Internet
Author: User
Tags connection pooling stmt
Now we're going to combine the Dbconnetionmanager and Dbconnectionpool classes to explain the use of connection pools in the servlet:

First, a brief introduction to the lifecycle of the servlet:

The Servlet API defines the servlet lifecycle as follows:

1, the Servlet is created and initialized (init () method).

2, for 0 or more customer calls to provide services (service () method).

3, the servlet is destroyed, memory is reclaimed (Destroy () method).

Examples of using connection pooling in the servlet

The typical performance of a servlet using a connection pool in three phases is:

1. In Init (), call Dbconnectionmanager.getinstance () and save the returned reference in the instance variable.


2. In Sevice (), call getconnection (), perform a series of database operations, and then call Freeconnection () to return the connection.

3. In Destroy (), call Release () to free all resources and close all connections.

The following example shows how to use a connection pool.

Import java.io.*;

Import java.sql.*;

Import javax.servlet.*;

Import javax.servlet.http.*;

public class Testservlet extends HttpServlet {

Private Dbconnectionmanager connmgr;



public void init (ServletConfig conf) throws Servletexception {

Super.init (conf);

Connmgr = Dbconnectionmanager.getinstance ();

}


public void Service (HttpServletRequest req, httpservletresponse Res)

Throws IOException {

Res.setcontenttype ("text/html");

PrintWriter out = Res.getwriter ();

Connection con = connmgr.getconnection ("IDB");

if (con = = null) {

Out.println ("Cant get connection");

Return

}

ResultSet rs = null;

ResultSetMetaData MD = NULL;

Statement stmt = null;

try {

stmt = Con.createstatement ();

rs = Stmt.executequery ("SELECT * from EMPLOYEE");

md = Rs.getmetadata ();

Out.println ("Employee data");

while (Rs.next ()) {

Out.println ("
");

for (int i = 1; i < Md.getcolumncount (); i++) {

Out.print (rs.getstring (i) + ",");

}

}

Stmt.close ();

Rs.close ();

}

catch (SQLException e) {

E.printstacktrace (out);

}

Connmgr.freeconnection ("IDB", con);

}

public void Destroy () {

Connmgr.release ();

Super.destroy ();

}

}

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.