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

Contents of /android/TrainInfoService/src/dk/thoerup/traininfoservice/LoadStations.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 958 - (show annotations) (download)
Mon Jul 5 09:48:06 2010 UTC (13 years, 10 months ago) by torben
File size: 2340 byte(s)
Switch to servlet3.0 annotations
1 package dk.thoerup.traininfoservice;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.sql.Connection;
6 import java.sql.PreparedStatement;
7 import java.sql.ResultSet;
8 import java.sql.SQLException;
9
10 import javax.servlet.ServletException;
11 import javax.servlet.annotation.WebServlet;
12 import javax.servlet.http.HttpServlet;
13 import javax.servlet.http.HttpServletRequest;
14 import javax.servlet.http.HttpServletResponse;
15
16 @Deprecated
17 @WebServlet(urlPatterns={"/LoadStations"})
18
19 public class LoadStations extends HttpServlet {
20 private static final long serialVersionUID = 1L;
21
22 public LoadStations() {
23 super();
24 }
25
26 protected void insertOrUpdate(Connection conn, String type, String code, String name) throws SQLException{
27 String sql = "SELECT id FROM trainstations WHERE stationcode=?";
28 PreparedStatement stmt = conn.prepareStatement(sql);
29 stmt.setString(1, code);
30
31 ResultSet rs = stmt.executeQuery();
32
33 boolean stog = type.equalsIgnoreCase("S2");
34
35 if (rs.next()) {
36 int id = rs.getInt(1);
37 stmt = conn.prepareStatement("UPDATE trainstations SET name=?, stog=?, updated=now() WHERE id=?");
38 stmt.setString(1, name);
39 stmt.setBoolean(2, stog);
40 stmt.setInt(3, id);
41 stmt.execute();
42 } else {
43 stmt = conn.prepareStatement("INSERT INTO trainstations (name,stationcode,stog,updated) VALUES (?,?,?,now())");
44 stmt.setString(1, name);
45 stmt.setString(2, code);
46 stmt.setBoolean(3, stog);
47 stmt.execute();
48 }
49
50 }
51
52 @Override
53 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
54 String url = "http://rafiki.t-hoerup.dk/tog/stations.php";
55
56 String stationStr = DownloadUtil.getContentString(url, 5000, "ISO-8859-1");
57
58 String stations[] = stationStr.split("\n");
59 Connection conn = null;
60 try {
61 conn = DBConnection.getConnection();
62 for (String stationLine : stations) {
63 String fields[] = stationLine.split(";");
64
65
66 insertOrUpdate(conn, fields[0], fields[1], fields[2]);
67 }
68 } catch (Exception e) {
69 throw new ServletException(e);
70 } finally {
71 try {
72 if (conn != null && !conn.isClosed())
73 conn.close();
74 } catch (SQLException e) {}
75 }
76 PrintWriter out = resp.getWriter();
77 out.print("ok");
78 }
79
80 }

  ViewVC Help
Powered by ViewVC 1.1.20