1 |
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 |
public DumpResultSet() { |
17 |
super(); |
18 |
} |
19 |
|
20 |
|
21 |
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 |
|
30 |
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 |
sb.append("<table border=1><tr>"); |
40 |
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 |
String res = null; |
80 |
|
81 |
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 |
} |
90 |
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 |
resp.setContentType("text/html"); |
95 |
resp.getWriter().println(res); |
96 |
} |
97 |
|
98 |
} |