1 |
torben |
961 |
package dk.thoerup.traininfoservice; |
2 |
|
|
|
3 |
|
|
|
4 |
|
|
import java.io.IOException; |
5 |
|
|
import java.sql.Connection; |
6 |
|
|
import java.sql.ResultSet; |
7 |
|
|
import java.sql.SQLException; |
8 |
|
|
import java.sql.Statement; |
9 |
|
|
import java.util.ArrayList; |
10 |
|
|
import java.util.List; |
11 |
|
|
|
12 |
|
|
import javax.servlet.ServletException; |
13 |
|
|
import javax.servlet.annotation.WebServlet; |
14 |
|
|
import javax.servlet.http.HttpServlet; |
15 |
|
|
import javax.servlet.http.HttpServletRequest; |
16 |
|
|
import javax.servlet.http.HttpServletResponse; |
17 |
|
|
|
18 |
|
|
|
19 |
|
|
@WebServlet(urlPatterns={"/SavedStatistics"}) |
20 |
|
|
public class SavedStatistics extends HttpServlet { |
21 |
|
|
private static final long serialVersionUID = 1L; |
22 |
|
|
|
23 |
|
|
|
24 |
|
|
protected SavedStatBean convertRow(ResultSet res) throws SQLException { |
25 |
|
|
SavedStatBean bean = new SavedStatBean(); |
26 |
|
|
|
27 |
torben |
964 |
bean.label = res.getString(1); |
28 |
torben |
961 |
bean.station = res.getString(2); |
29 |
|
|
bean.location = res.getString(3); |
30 |
|
|
bean.name = res.getString(4); |
31 |
|
|
bean.favorites = res.getString(5); |
32 |
|
|
bean.departure = res.getString(6); |
33 |
|
|
bean.depcache = res.getString(7); |
34 |
|
|
bean.deperror = res.getString(8); |
35 |
|
|
bean.timetable = res.getString(9); |
36 |
|
|
bean.timecache = res.getString(10); |
37 |
|
|
bean.timeerror = res.getString(11); |
38 |
|
|
|
39 |
|
|
return bean; |
40 |
|
|
|
41 |
|
|
} |
42 |
|
|
|
43 |
|
|
|
44 |
|
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
45 |
|
|
|
46 |
|
|
Connection conn = null; |
47 |
|
|
try { |
48 |
|
|
conn = DBConnection.getConnection(); |
49 |
|
|
Statement stmt = conn.createStatement(); |
50 |
|
|
List<SavedStatBean> stats = new ArrayList<SavedStatBean>(); |
51 |
|
|
|
52 |
|
|
|
53 |
|
|
String sqlAvg = "SELECT count(*) AS count, 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, " + |
54 |
|
|
"round(avg(departure),1) as departure, round(avg(depcache),1) as depcache, round(avg(deperror),1) as deperror, " + |
55 |
|
|
"round(avg(timetable),1) as timetable, round(avg(timecache),1) as timecache, round(avg(timeerror),1) as timeerror " + |
56 |
|
|
"FROM trainstatistics"; |
57 |
|
|
|
58 |
|
|
ResultSet resAvg = stmt.executeQuery(sqlAvg); |
59 |
|
|
resAvg.next(); |
60 |
|
|
SavedStatBean avgBean = convertRow(resAvg); |
61 |
|
|
|
62 |
|
|
//////////////////////////////////////////////// |
63 |
|
|
|
64 |
|
|
String SQL = "SELECT statisticsdate, location+name+favorites AS station, location,name,favorites,departure,depcache,deperror,timetable,timecache,timeerror " + |
65 |
|
|
"FROM trainstatistics ORDER BY statisticsdate DESC limit 14"; |
66 |
|
|
|
67 |
|
|
|
68 |
|
|
ResultSet res = stmt.executeQuery(SQL); |
69 |
|
|
|
70 |
|
|
|
71 |
|
|
|
72 |
|
|
|
73 |
|
|
while (res.next()) { |
74 |
|
|
SavedStatBean bean = convertRow(res); |
75 |
|
|
stats.add(bean); |
76 |
|
|
|
77 |
|
|
} |
78 |
torben |
967 |
|
79 |
|
|
conn.close(); |
80 |
torben |
961 |
|
81 |
|
|
|
82 |
torben |
962 |
request.setAttribute("avg", avgBean); |
83 |
torben |
961 |
request.setAttribute("stats", stats); |
84 |
|
|
request.getRequestDispatcher("/savedstatistics.jsp").forward(request, response); |
85 |
|
|
|
86 |
|
|
|
87 |
|
|
} catch (SQLException e) { |
88 |
|
|
try { |
89 |
|
|
if (conn != null) conn.close(); |
90 |
|
|
} catch (SQLException se) {} |
91 |
|
|
throw new ServletException(e); |
92 |
|
|
} |
93 |
|
|
} |
94 |
|
|
|
95 |
|
|
} |