KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > rubis > servlets > BrowseRegions


1 package edu.rice.rubis.servlets;
2
3 import java.io.IOException JavaDoc;
4 import java.sql.Connection JavaDoc;
5 import java.sql.PreparedStatement JavaDoc;
6 import java.sql.ResultSet JavaDoc;
7
8 import javax.servlet.ServletException JavaDoc;
9 import javax.servlet.http.HttpServletRequest JavaDoc;
10 import javax.servlet.http.HttpServletResponse JavaDoc;
11
12 /** Builds the html page with the list of all region in the database */
13 public class BrowseRegions extends RubisHttpServlet
14 {
15  
16
17
18   public int getPoolSize()
19   {
20     return Config.BrowseRegionsPoolSize;
21   }
22
23 /**
24  * Close both statement and connection.
25  */

26   private void closeConnection(PreparedStatement JavaDoc stmt, Connection JavaDoc conn)
27   {
28     try
29     {
30       if (stmt != null)
31         stmt.close(); // close statement
32
if (conn != null)
33         releaseConnection(conn);
34     }
35     catch (Exception JavaDoc ignore)
36     {
37     }
38   }
39   
40 /**
41  * Get the list of regions from the database
42  */

43   private void regionList(ServletPrinter sp)
44   {
45     PreparedStatement JavaDoc stmt = null;
46     Connection JavaDoc conn = null;
47     String JavaDoc regionName;
48     ResultSet JavaDoc rs = null;
49
50     // get the list of regions
51
try
52     {
53       conn = getConnection();
54
55       stmt = conn.prepareStatement("SELECT name, id FROM regions");
56       rs = stmt.executeQuery();
57     }
58     catch (Exception JavaDoc e)
59     {
60       sp.printHTML("Failed to executeQuery for the list of regions" + e);
61       closeConnection(stmt, conn);
62       return;
63     }
64     try
65     {
66       if (!rs.first())
67       {
68         sp.printHTML(
69           "<h2>Sorry, but there is no region available at this time. Database table is empty</h2><br>");
70         closeConnection(stmt, conn);
71         return;
72       }
73       else
74         sp.printHTML("<h2>Currently available regions</h2><br>");
75
76       do
77       {
78         regionName = rs.getString("name");
79         sp.printRegion(regionName);
80       }
81       while (rs.next());
82       closeConnection(stmt, conn);
83
84     }
85     catch (Exception JavaDoc e)
86     {
87       sp.printHTML("Exception getting region list: " + e + "<br>");
88       closeConnection(stmt, conn);
89     }
90   }
91
92   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
93     throws IOException JavaDoc, ServletException JavaDoc
94   {
95     ServletPrinter sp = null;
96     sp = new ServletPrinter(response, "BrowseRegions");
97     sp.printHTMLheader("RUBiS: Available regions");
98
99     regionList(sp);
100     sp.printHTMLfooter();
101   }
102
103   /**
104    * Clean up the connection pool.
105    */

106   public void destroy()
107   {
108     super.destroy();
109   }
110
111 }
112
Popular Tags