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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 344 - (hide annotations) (download)
Thu Sep 24 21:04:34 2009 UTC (14 years, 8 months ago) by torben
File size: 4052 byte(s)
Don't write the sql string to system out
1 torben 292 package dk.thoerup.traininfoservice;
2    
3     import java.io.IOException;
4     import java.sql.Connection;
5     import java.sql.PreparedStatement;
6     import java.sql.ResultSet;
7     import java.sql.SQLException;
8    
9     import javax.servlet.ServletException;
10     import javax.servlet.http.HttpServlet;
11     import javax.servlet.http.HttpServletRequest;
12     import javax.servlet.http.HttpServletResponse;
13    
14     import dk.thoerup.traininfoservice.DBConnection;
15    
16     /**
17     * Servlet implementation class LocateStations
18     */
19     public class LocateStations extends HttpServlet {
20     private static final long serialVersionUID = 1L;
21    
22     /**
23     * @see HttpServlet#HttpServlet()
24     */
25     public LocateStations() {
26     super();
27     // TODO Auto-generated constructor stub
28     }
29 torben 322
30 torben 292
31 torben 322
32     protected String getStations(Connection conn, double latitude, double longitude, String name) throws SQLException {
33     String SQL = "";
34    
35     if (latitude >= 0.0 && longitude >= 0.0)
36     {
37 torben 292 //inner select is workaround from not being able to use a calculated column directly in where clause
38 torben 322 SQL = "SELECT * FROM ( "+
39 torben 309 " SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, " +
40 torben 292 " earth_distance( ll_to_earth(latitude,longitude), ll_to_earth(?,?))::int AS calcdist " +
41     " FROM trainstations " +
42 torben 301 " WHERE latitude IS NOT NULL AND longitude IS NOT NULL " +
43 torben 292 " ) AS trainstations2 " +
44     "ORDER BY calcdist ASC " +
45     "LIMIT 4 ";
46 torben 322 } else if (name != null) {
47     SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,0.0 " +
48     "FROM trainstations " +
49     "WHERE name ILIKE ? AND latitude IS NOT NULL AND longitude IS NOT NULL " +
50     "ORDER BY name ";
51     } else throw new SQLException("not enough parameters");
52 torben 292
53    
54     PreparedStatement stmt = null;
55     ResultSet res = null;
56    
57     StringBuffer buff = new StringBuffer();
58    
59     buff.append("<?xml version=\"1.0\" ?>\n");
60     buff.append("<stations>\n");
61     try
62     {
63     stmt = conn.prepareStatement(SQL);
64 torben 322 if (latitude >= 0 && longitude >= 0) {
65     stmt.setDouble(1, latitude);
66     stmt.setDouble(2, longitude);
67     }
68     if (name != null) {
69     stmt.setString(1, "%" + name + "%");
70     }
71 torben 292
72     res = stmt.executeQuery();
73    
74     while (res.next()) {
75     buff.append("<station>\n");
76    
77 torben 309 buff.append("<id>").append( res.getInt(1) ).append("</id>\n");
78     buff.append("<name>").append( res.getString(2) ) .append("</name>\n");
79     buff.append("<latitude>").append( res.getDouble(3) ) .append("</latitude>\n");
80     buff.append("<longitude>").append( res.getDouble(4) ) .append("</longitude>\n");
81     res.getString(5);
82     buff.append("<fjerntog>").append( !res.wasNull() ) .append("</fjerntog>\n");
83     res.getString(6);
84     buff.append("<stog>").append( !res.wasNull() ) .append("</stog>\n");
85     buff.append("<calcdist>").append( res.getInt(7) ) .append("</calcdist>\n");
86 torben 292
87     buff.append("</station>\n");
88     }
89     } finally {
90     if (res != null && !res.isClosed())
91     res.close();
92     if (stmt != null && !stmt.isClosed())
93     stmt.close();
94     }
95     buff.append("</stations>\n");
96     return buff.toString();
97     }
98    
99     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
100    
101 torben 322 double latitude = -1.0;
102     if (request.getParameter("latitude") != null)
103     latitude = Double.parseDouble( request.getParameter("latitude") );
104    
105     double longitude = -1.0;
106     if (request.getParameter("latitude") != null)
107     longitude = Double.parseDouble( request.getParameter("longitude") );
108    
109     String name = request.getParameter("name");
110 torben 292
111     Connection conn = null;
112     try {
113     conn = DBConnection.getConnection();
114    
115 torben 322 String xml = getStations(conn, latitude, longitude, name);
116 torben 292
117     response.setContentType("text/xml");
118     response.getWriter().print(xml);
119    
120     conn.close();
121     conn = null;
122    
123     } catch (Exception e) {
124     throw new ServletException(e);
125     } finally {
126     try {
127     if (conn != null)
128     conn.close();
129     } catch (Throwable t) {}
130     }
131     }
132    
133     }

  ViewVC Help
Powered by ViewVC 1.1.20