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 |
torben |
827 |
import java.util.Date; |
9 |
torben |
296 |
|
10 |
|
|
import javax.servlet.ServletException; |
11 |
torben |
958 |
import javax.servlet.annotation.WebServlet; |
12 |
torben |
296 |
import javax.servlet.http.HttpServlet; |
13 |
|
|
import javax.servlet.http.HttpServletRequest; |
14 |
|
|
import javax.servlet.http.HttpServletResponse; |
15 |
|
|
|
16 |
torben |
958 |
@WebServlet(urlPatterns={"/DumpResultSet"}) |
17 |
torben |
296 |
public class DumpResultSet extends HttpServlet { |
18 |
|
|
|
19 |
torben |
298 |
public DumpResultSet() { |
20 |
|
|
super(); |
21 |
|
|
} |
22 |
torben |
296 |
|
23 |
torben |
298 |
|
24 |
torben |
296 |
private static final long serialVersionUID = 1L; |
25 |
|
|
|
26 |
|
|
String dumpResultset(String sql) throws ServletException { |
27 |
|
|
StringBuilder sb = new StringBuilder(); |
28 |
|
|
|
29 |
|
|
Connection conn = null; |
30 |
|
|
Statement stmt = null; |
31 |
|
|
ResultSet rs = null; |
32 |
torben |
298 |
|
33 |
torben |
296 |
try { |
34 |
torben |
304 |
int count = 0; |
35 |
|
|
|
36 |
torben |
296 |
conn = DBConnection.getConnection(); |
37 |
|
|
stmt = conn.createStatement(); |
38 |
|
|
|
39 |
|
|
rs = stmt.executeQuery(sql); |
40 |
|
|
ResultSetMetaData meta = rs.getMetaData(); |
41 |
|
|
int columns = meta.getColumnCount(); |
42 |
|
|
|
43 |
|
|
sb.append("<h2>").append(sql).append("</h2>"); |
44 |
torben |
298 |
sb.append("<table border=1><tr>"); |
45 |
torben |
296 |
for (int i=1; i<=columns; i++) { |
46 |
|
|
sb.append("<th>").append( meta.getColumnName(i)).append("</th>"); |
47 |
|
|
} |
48 |
torben |
304 |
sb.append("</tr>\n\n"); |
49 |
torben |
296 |
while(rs.next()) |
50 |
|
|
{ |
51 |
torben |
304 |
count++; |
52 |
|
|
|
53 |
torben |
296 |
sb.append("<tr>"); |
54 |
|
|
for (int i=1; i<=columns; i++) { |
55 |
torben |
494 |
String value = rs.getString(i); |
56 |
|
|
if (value != null) { |
57 |
|
|
value = value.replace("\n", "<br>"); |
58 |
|
|
} |
59 |
|
|
sb.append("<td>").append( value ).append("</td>"); |
60 |
torben |
296 |
} |
61 |
torben |
304 |
sb.append("</tr>\n"); |
62 |
torben |
296 |
} |
63 |
|
|
sb.append("</table>"); |
64 |
torben |
827 |
sb.append("Rowcount: ").append(count).append("<br>\n"); |
65 |
|
|
sb.append("Generated: ").append(new Date()).append("<br>\n"); |
66 |
torben |
296 |
} catch (Exception e) { |
67 |
|
|
throw new ServletException(e); |
68 |
|
|
} finally { |
69 |
|
|
|
70 |
|
|
try { |
71 |
|
|
if (rs != null && !rs.isClosed()) |
72 |
|
|
rs.close(); |
73 |
|
|
} catch (Exception e) {} |
74 |
|
|
try { |
75 |
|
|
if (stmt != null && !stmt.isClosed()) |
76 |
|
|
stmt.close(); |
77 |
|
|
} catch (Exception e) {} |
78 |
|
|
|
79 |
|
|
try { |
80 |
|
|
if (conn != null && !conn.isClosed()) |
81 |
|
|
conn.close(); |
82 |
|
|
} catch (Exception e) {} |
83 |
|
|
} |
84 |
|
|
return sb.toString(); |
85 |
|
|
} |
86 |
|
|
|
87 |
torben |
733 |
String dumpUpdate(String query) throws ServletException { |
88 |
|
|
StringBuilder sb = new StringBuilder(); |
89 |
|
|
|
90 |
|
|
Connection conn = null; |
91 |
|
|
Statement stmt = null; |
92 |
|
|
try { |
93 |
|
|
conn = DBConnection.getConnection(); |
94 |
|
|
stmt = conn.createStatement(); |
95 |
|
|
stmt.execute(query); |
96 |
|
|
int count = stmt.getUpdateCount(); |
97 |
|
|
|
98 |
|
|
sb.append("<h2>").append(query).append("</h2>"); |
99 |
|
|
sb.append("Affected rows: ").append(count); |
100 |
|
|
} catch (Exception e) { |
101 |
|
|
throw new ServletException(e); |
102 |
|
|
} finally { |
103 |
|
|
try { |
104 |
|
|
if (stmt != null && !stmt.isClosed()) |
105 |
|
|
stmt.close(); |
106 |
|
|
} catch (Exception e) {} |
107 |
|
|
|
108 |
|
|
try { |
109 |
|
|
if (conn != null && !conn.isClosed()) |
110 |
|
|
conn.close(); |
111 |
|
|
} catch (Exception e) {} |
112 |
|
|
} |
113 |
|
|
|
114 |
|
|
return sb.toString(); |
115 |
|
|
} |
116 |
|
|
|
117 |
|
|
|
118 |
|
|
|
119 |
torben |
296 |
@Override |
120 |
|
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
121 |
|
|
String dump = req.getParameter("dump"); |
122 |
|
|
|
123 |
torben |
298 |
String res = null; |
124 |
torben |
296 |
|
125 |
torben |
298 |
if (dump != null) { |
126 |
|
|
if (dump.equals("all")) { |
127 |
torben |
304 |
res = dumpResultset("SELECT * FROM trainstations ORDER BY id"); |
128 |
torben |
298 |
} else if (dump.equals("coords")) { |
129 |
torben |
304 |
res = dumpResultset("SELECT * FROM trainstations WHERE latitude IS NULL OR longitude IS NULL ORDER BY id"); |
130 |
torben |
298 |
} else if (dump.equals("duplicate")) { |
131 |
|
|
res = dumpResultset("SELECT name,count(*) FROM trainstations GROUP BY name HAVING count(*) > 1"); |
132 |
torben |
304 |
} else if (dump.equals("allfull")) { |
133 |
|
|
res = dumpResultset("select *, " + |
134 |
torben |
497 |
"'<a href=\"http://maps.google.dk/?q=' ||latitude || ',' || longitude || '\">Maps</a>' AS maps, " + |
135 |
torben |
314 |
"'<a href=\"http://www.bane.dk/visStation.asp?ArtikelID=4275&W=FJRN&S=' || stationcode_fjrn || '\">Bane.dk Fjern</a>' as banedk1, " + |
136 |
|
|
"'<a href=\"http://www.bane.dk/visStation.asp?ArtikelID=4275&W=S2&S=' || stationcode_stog || '\">Bane.dk stog</a>' as banedk2 " + |
137 |
|
|
"FROM trainstations ORDER BY id"); |
138 |
torben |
377 |
} else if (dump.equals("disabled")) { |
139 |
|
|
res = dumpResultset("SELECT * FROM trainstations WHERE enabled = false ORDER BY id"); |
140 |
torben |
492 |
} else if (dump.equals("noaddress")) { |
141 |
|
|
res = dumpResultset("SELECT * FROM trainstations WHERE address IS NULL or address = '' "); |
142 |
torben |
715 |
} else if (dump.equals("aliases")) { |
143 |
|
|
res = dumpResultset("SELECT * FROM trainstations WHERE aliases IS NOT null"); |
144 |
torben |
733 |
} else if (dump.equals("updatecoords")) { |
145 |
|
|
res = dumpUpdate("UPDATE trainstations SET earth_coord = ll_to_earth(latitude,longitude)"); |
146 |
torben |
961 |
} |
147 |
torben |
296 |
} |
148 |
torben |
298 |
if (res == null) { |
149 |
torben |
813 |
res = "<a href=DumpResultSet?dump=all>All</a><br>" + |
150 |
|
|
"<a href=DumpResultSet?dump=allfull>All with links</a><br>" + |
151 |
|
|
"<a href=DumpResultSet?dump=coords>Missing coords</a><br>" + |
152 |
|
|
"<a href=DumpResultSet?dump=duplicate>Duplicate stations</a><br>" + |
153 |
|
|
"<a href=DumpResultSet?dump=disabled>Disabled stations</a><br>" + |
154 |
|
|
"<a href=DumpResultSet?dump=noaddress>No address</a><br>" + |
155 |
|
|
"<a href=DumpResultSet?dump=aliases>Has aliases</a><br>" + |
156 |
torben |
961 |
"<a href=DumpResultSet?dump=updatecoords>update coords</a><br>"; |
157 |
torben |
298 |
} |
158 |
|
|
|
159 |
torben |
296 |
resp.setContentType("text/html"); |
160 |
|
|
resp.getWriter().println(res); |
161 |
|
|
} |
162 |
|
|
|
163 |
|
|
} |