Connect to Database in Servlet : Database « JSP « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » JSP » Database 
23.52.10.Connect to Database in ServletPrevious/Next

Jsp page

<%--
  Copyright (c2002 by Phil Hanna
  All rights reserved.
  
  You may study, use, modify, and distribute this
  software for any purpose provided that this
  copyright notice appears in all copies.
  
  This software is provided without warranty
  either expressed or implied.
--%>
<%@ page extends="com.jspcr.servlets.NutrientDatabaseServlet" %>
<%--
   This JSP page subclasses the NutrientDatabaseServlet
   parent class, which automatically loads the
   database driver and establishes the connection.
--%>
<%@ page import="java.io.*,java.sql.*" %>
<html>
<head>
<title>Food Groups</title>
</head>
<body>
<h1>Food Groups</h1>
<table border="1" cellpadding="3" cellspacing="0">
<tr><TH>Code</th><TH>Description</th></tr>
<%
   // Execute a query

   Statement stmt = con.createStatement();
   String sql = "SELECT * FROM FD_GROUP ORDER BY FDGP_DESC";
   ResultSet rs = stmt.executeQuery(sql);
   while (rs.next()) {
      String code = rs.getString(1);
      String desc = rs.getString(2);
%>
<tr>
   <td><%= code %></td>
   <td><%= desc %></td>
</tr>
<%
   }
   
   // Close the database objects

   rs.close();
   stmt.close();
%>
</table>
</body>
</html>

NutrientDatabaseServlet.java

/**
*  Copyright (c) 2002 by Phil Hanna
*  All rights reserved.
*  
*  You may study, use, modify, and distribute this
*  software for any purpose provided that this
*  copyright notice appears in all copies.
*  
*  This software is provided without warranty
*  either expressed or implied.
*/
package com.jspcr.servlets;

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;

/**
* An example of a JSP superclass that can be selected with
* the <code>extends</code> attribute of the page
* directive.  This servlet automatically loads the
* JDBC-ODBC driver class when initialized and establishes
* a connection to the USDA nutrient database.
*/
public abstract class NutrientDatabaseServlet
   extends HttpServlet
   implements HttpJspPage
{
   protected Connection con;

   /**
   * Initialize a servlet with the driver
   * class already loaded and the database
   * connection established.
   */
   public void init(ServletConfig config)
      throws ServletException
   {
      super.init(config);
      try {
         Class.forName("com.mysql.jdbc.Driver");
         con = DriverManager.getConnection("jdbc:mysql://localhost/usda15");
      }
      catch (Exception e) {
         throw new UnavailableException(e.getMessage());
      }

      jspInit();
   }

   /**
   * Closes the database connection when
   * the servlet is unloaded.
   */
   public void destroy()
   {
      try {
         if (con != null) {
            con.close();
            con = null;
         }
      }
      catch (Exception ignore) {}

      jspDestroy();
      super.destroy();
   }

   /**
   * Called when the JSP is loaded.
   * By default does nothing.
   */
   public void jspInit() {}

   /**
   * Called when the JSP is unloaded.
   * By default does nothing.
   */
   public void jspDestroy() {}

   /**
   * Invokes the JSP's _jspService method.
   */
   public final void service(
         HttpServletRequest request,
         HttpServletResponse response)
      throws ServletException, IOException
   {
      _jspService(request, response);
   }

   /**
   * Handles a service request.
   */
   public abstract void _jspService(
         HttpServletRequest request,
         HttpServletResponse response)
      throws ServletException, IOException;
}
  Download:  ConnectToDatabaseInServlet.zip( 4 k)
23.52.Database
23.52.1.Make Database connection
23.52.2.Accessing the Database Table
23.52.3.Retrieve data in Database based on form input
23.52.4.Output ResultSet
23.52.5.Joining Tables
23.52.6.Creating a Table
23.52.7.Insert data to a table
23.52.8.Navigate Database Table With Javascript and JSP
23.52.9.Database Table Navigation Based On Form
23.52.10.Connect to Database in Servlet
23.52.11.Using Table Metadata
23.52.12.Output data in database as XML
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.