## example.servlet.database
Class QueryServlet

    java.lang.Object
      HttpServlet
          example.servlet.database.QueryServlet

    public class QueryServlet

extends HttpServlet

Example of a database query from a servlet.

Database access has two components: looking up the data source using JNDI and using JDBC calls to retrieve the data.

The JNDI lookup finds the DataSource configured in the resin.conf or web.xml. Since the JNDI lookup only needs to happen once, it's usually put in the init() method. The database configuration looks something like the following: ``

     <resource-ref>
       <res-ref-name>jdbc/test
       <res-type>javax.sql.DataSource
       <init-param driver-name="com.caucho.jdbc.mysql.Driver"/>
       <init-param url="jdbc:mysql_caucho://localhost:3306/test"/>
       <init-param max-connections="20"/>
       <init-param max-idle-time="30"/>
     </resource-ref>

Different databasees will have different values for driver-name and url. The max-connections and max-idle-time are configuration values for Resin's database pooling. The servlet doesn't need to worry about database pooling; Resin will take care of it automatically. (The APIs used in the example are all from the J2EE standard.)

Applications should always use the following pattern for all database access. The try ... finally pattern is vital to make sure the connection is released to the pool. ``

     Connection conn = null;
     try {
       conn = dataSource.getConnection();
       ...
     } finally {
       if (conn != null)
         conn.close();
     }

Constructor Summary
`QueryServlet()`

Method Summary
`void` `doGet(HttpServletRequest request, HttpServletResponse response)` Handles GET requests.
`void` `init()` Initialize the servlet, caching the JNDI lookup of the DataSource.

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Constructor Detail

### QueryServlet

    public QueryServlet()

Method Detail

### init

    public void init()
              throws ServletException

  Initialize the servlet, caching the JNDI lookup of the DataSource.

  Throws:
    `ServletException`

### doGet

    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
               throws java.io.IOException,
                      ServletException

  Handles GET requests. Resin will call the doGet method when the browser sends a GET request.

  Parameters:
    `request` - the request object contains the data from the browser's request.
    `response` - the response object contains methods to send data back to the browser.
  Throws:
    `java.io.IOException`
    `ServletException`