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