Returns the list of the most popular flavors : Database « Servlet « Java Tutorial

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. JSP
22. JSTL
23. Servlet
24. Web Services SOA
25. Email
26. J2ME
27. J2EE Application
28. XML
29. Design Pattern
30. Log
31. Security
32. Apache Common
Java
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Tutorial » Servlet » Database 
23. 33. 5. Returns the list of the most popular flavors
/**
*  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.
*/
import java.io.*;
import java.net.*;
import java.sql.*;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;

/**
* Returns the list of the most popular flavors
*/
public class FlavorListServlet extends HttpServlet
{
   public static final String JDBC_DRIVER =
      "com.mysql.jdbc.Driver";

   public static final String URL =
      "jdbc:mysql://localhost/IceCream";

   public void doGet(
         HttpServletRequest request,
         HttpServletResponse response)
      throws ServletException, IOException
   {
      PrintWriter out = response.getWriter();
      response.setContentType("text/html");

      // Get the bounds of the ranks to be listed
      // or use defaults

      int lowLimit = getLimit(request.getParameter("lowLimit")0);
      int highLimit = getLimit(request.getParameter("highLimit")100);

      Connection con = null;
      try {
         
         // Connect to the ice cream database

         Class.forName(JDBC_DRIVER);
         con = DriverManager.getConnection(URL);

         // Run a query to get the top flavors

         String sql =
            "SELECT  RANK, NAME"
            "   FROM flavors"
            "   WHERE RANK BETWEEN ? AND ?"
            "   ORDER BY RANK" ;
         PreparedStatement pstmt = con.prepareStatement(sql);
         pstmt.setInt(1, lowLimit);
         pstmt.setInt(2, highLimit);
         ResultSet rs = pstmt.executeQuery();

         // Print as an ordered list

         out.println("<ol>");
         while (rs.next()) {
            int rank = rs.getInt(1);
            String name = rs.getString(2);
            out.println("   <li>" + name + "</li>");
         }
         out.println("</ol>");
      }
      catch (SQLException e) {
         throw new ServletException(e.getMessage());
      }
      catch (ClassNotFoundException e) {
         throw new ServletException(e.getMessage());
      }

      // Close the database

      finally {
         if (con != null) {
            try con.close()}
            catch (SQLException ignore) {}
         }
      }
   }

   /**
   * Subroutine to get the integer value of one of
   * the limit parameters.
   @param parm the parameter value, which may be null
   @param defaultValue the default value
   */
   private static int getLimit(String parm, int defaultValue)
   {
      int limit = defaultValue;
      if (parm != null) {
         try {
            limit = Integer.parseInt(parm);
         }
         catch (NumberFormatException ignore) {
         }
      }
      return limit;
   }
}
23. 33. Database
23. 33. 1. Servlet Database Connection
23. 33. 2. Servlet Update Database
23. 33. 3. Servlet Database ResultSet Display Helper
23. 33. 4. Servlet Database Gif Decoder
23. 33. 5. Returns the list of the most popular flavors
23. 33. 6. Read data from Database and display it in a HTML table
www__._j__a_v___a2s_.___c___o___m__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.