/[projects]/android/TrainInfoService/src/dk/thoerup/traininfoservice/db/DumpResultSet.java
ViewVC logotype

Annotation of /android/TrainInfoService/src/dk/thoerup/traininfoservice/db/DumpResultSet.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 298 - (hide annotations) (download)
Wed Sep 2 04:52:57 2009 UTC (14 years, 8 months ago) by torben
Original Path: android/TrainInfoService/src/dk/thoerup/traininfoservice/DumpResultSet.java
File size: 2604 byte(s)
add another query to dumpresultset
1 torben 296 package dk.thoerup.traininfoservice;
2    
3     import java.io.IOException;
4     import java.sql.Connection;
5     import java.sql.ResultSet;
6     import java.sql.ResultSetMetaData;
7     import java.sql.Statement;
8    
9     import javax.servlet.ServletException;
10     import javax.servlet.http.HttpServlet;
11     import javax.servlet.http.HttpServletRequest;
12     import javax.servlet.http.HttpServletResponse;
13    
14     public class DumpResultSet extends HttpServlet {
15    
16 torben 298 public DumpResultSet() {
17     super();
18     }
19 torben 296
20 torben 298
21 torben 296 private static final long serialVersionUID = 1L;
22    
23     String dumpResultset(String sql) throws ServletException {
24     StringBuilder sb = new StringBuilder();
25    
26     Connection conn = null;
27     Statement stmt = null;
28     ResultSet rs = null;
29 torben 298
30 torben 296 try {
31     conn = DBConnection.getConnection();
32     stmt = conn.createStatement();
33    
34     rs = stmt.executeQuery(sql);
35     ResultSetMetaData meta = rs.getMetaData();
36     int columns = meta.getColumnCount();
37    
38     sb.append("<h2>").append(sql).append("</h2>");
39 torben 298 sb.append("<table border=1><tr>");
40 torben 296 for (int i=1; i<=columns; i++) {
41     sb.append("<th>").append( meta.getColumnName(i)).append("</th>");
42     }
43     sb.append("</tr>");
44     while(rs.next())
45     {
46     sb.append("<tr>");
47     for (int i=1; i<=columns; i++) {
48     sb.append("<td>").append(rs.getString(i)).append("</td>");
49     }
50     sb.append("</tr>");
51     }
52     sb.append("</table>");
53     } catch (Exception e) {
54     throw new ServletException(e);
55     } finally {
56    
57     try {
58     if (rs != null && !rs.isClosed())
59     rs.close();
60     } catch (Exception e) {}
61     try {
62     if (stmt != null && !stmt.isClosed())
63     stmt.close();
64     } catch (Exception e) {}
65    
66     try {
67     if (conn != null && !conn.isClosed())
68     conn.close();
69     } catch (Exception e) {}
70     }
71    
72     return sb.toString();
73     }
74    
75     @Override
76     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
77     String dump = req.getParameter("dump");
78    
79 torben 298 String res = null;
80 torben 296
81 torben 298 if (dump != null) {
82     if (dump.equals("all")) {
83     res = dumpResultset("SELECT * FROM trainstations");
84     } else if (dump.equals("coords")) {
85     res = dumpResultset("SELECT * FROM trainstations WHERE latitude IS NULL OR longitude IS NULL");
86     } else if (dump.equals("duplicate")) {
87     res = dumpResultset("SELECT name,count(*) FROM trainstations GROUP BY name HAVING count(*) > 1");
88     }
89 torben 296 }
90 torben 298 if (res == null) {
91     res = "<a href=DumpResultSet?dump=all>All</a><br><a href=DumpResultSet?dump=coords>Missing coords</a><br><a href=DumpResultSet?dump=duplicate>Duplicate stations</a>";
92     }
93    
94 torben 296 resp.setContentType("text/html");
95     resp.getWriter().println(res);
96     }
97    
98     }

  ViewVC Help
Powered by ViewVC 1.1.20