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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 301 - (hide annotations) (download)
Mon Sep 7 12:23:35 2009 UTC (14 years, 8 months ago) by torben
File size: 2226 byte(s)
LocateStations: make sure lat+long isn't null

Create a servlet for loading station table from bane.dk data
1 torben 301 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.http.HttpServlet;
12     import javax.servlet.http.HttpServletRequest;
13     import javax.servlet.http.HttpServletResponse;
14    
15    
16     public class LoadStations extends HttpServlet {
17     private static final long serialVersionUID = 1L;
18    
19     public LoadStations() {
20     super();
21     }
22    
23     protected void insertOrUpdate(Connection conn, String type, String code, String name) throws SQLException{
24     String sql = "SELECT id FROM trainstations WHERE stationcode=?";
25     PreparedStatement stmt = conn.prepareStatement(sql);
26     stmt.setString(1, code);
27    
28     ResultSet rs = stmt.executeQuery();
29    
30     boolean stog = type.equalsIgnoreCase("S2");
31    
32     if (rs.next()) {
33     int id = rs.getInt(1);
34     stmt = conn.prepareStatement("UPDATE trainstations SET name=?, stog=?, updated=now() WHERE id=?");
35     stmt.setString(1, name);
36     stmt.setBoolean(2, stog);
37     stmt.setInt(3, id);
38     stmt.execute();
39     } else {
40     stmt = conn.prepareStatement("INSERT INTO trainstations (name,stationcode,stog,updated) VALUES (?,?,?,now())");
41     stmt.setString(1, name);
42     stmt.setString(2, code);
43     stmt.setBoolean(3, stog);
44     stmt.execute();
45     }
46    
47     }
48    
49     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
50     String url = "http://rafiki.t-hoerup.dk/tog/stations.php";
51    
52     String stationStr = DownloadUtil.getContentString(url, 5000, "ISO-8859-1");
53    
54     String stations[] = stationStr.split("\n");
55     Connection conn = null;
56     try {
57     conn = DBConnection.getConnection();
58     for (String stationLine : stations) {
59     String fields[] = stationLine.split(";");
60    
61    
62     insertOrUpdate(conn, fields[0], fields[1], fields[2]);
63     }
64     } catch (Exception e) {
65     throw new ServletException(e);
66     } finally {
67     try {
68     if (conn != null && !conn.isClosed())
69     conn.close();
70     } catch (SQLException e) {}
71     }
72     PrintWriter out = resp.getWriter();
73     out.print("ok");
74     }
75    
76     }

  ViewVC Help
Powered by ViewVC 1.1.20