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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20