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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 733 - (show annotations) (download)
Tue May 18 17:56:43 2010 UTC (14 years ago) by torben
File size: 5146 byte(s)
Save a part of the distance calculation in the earth_coord column
1 package dk.thoerup.traininfoservice;
2
3 import java.sql.Connection;
4 import java.sql.PreparedStatement;
5 import java.sql.ResultSet;
6 import java.sql.SQLException;
7 import java.sql.Statement;
8 import java.util.ArrayList;
9 import java.util.List;
10
11 public class StationDAO {
12 private StationBean convertSingleRow(ResultSet res) throws SQLException {
13 StationBean station = new StationBean();
14
15 station.setId( res.getInt(1) );
16 station.setName( res.getString(2) );
17 station.setLatitude( res.getDouble(3) );
18 station.setLongitude( res.getDouble(4) );
19 station.setRegional( res.getString(5) );
20 station.setStrain( res.getString(6) );
21 station.setMetro( res.getString(7) );
22 station.setAddress( res.getString(8) );
23 station.setCalcdist( (int)res.getDouble(9) );
24
25 return station;
26 }
27
28 private List<StationBean> convertResultset(ResultSet res) throws SQLException {
29 List<StationBean> stations = new ArrayList<StationBean>();
30 while (res.next()) {
31 stations.add( convertSingleRow(res) );
32 }
33 return stations;
34
35 }
36
37
38 public StationBean getById(int id) throws SQLException {
39 String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0 " +
40 "FROM trainstations WHERE id=" + id + " AND enabled=true";
41
42 Connection conn = null;
43 Statement stmt = null;
44 ResultSet res = null;
45 StationBean result;
46
47 try {
48 conn = DBConnection.getConnection();
49
50 stmt = conn.createStatement();
51 res = stmt.executeQuery(SQL);
52 res.next();
53 result = convertSingleRow(res);
54 } finally {
55 if (res != null)
56 res.close();
57 if (stmt != null)
58 stmt.close();
59 if (conn != null)
60 conn.close();
61 }
62
63 return result;
64 }
65
66 /*
67 * this code requires theses statements are run on database in order to do ILIKE searches against aliases (which is defines as array of varchar(64) )
68 * create function rlike(text,text) returns bool as
69 * 'select $2 ilike $1' language sql strict immutable;
70 * create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~);
71 */
72 public List<StationBean> getByName(String name) throws SQLException {
73 String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " +
74 "FROM trainstations " +
75 "WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " +
76 "ORDER BY name ";
77
78
79 List<StationBean> result;
80 Connection conn = null;
81 PreparedStatement stmt = null;
82 ResultSet res = null;
83 try {
84 conn = DBConnection.getConnection();
85 stmt = conn.prepareStatement(SQL);
86
87 stmt.setString(1, name + "%");
88 stmt.setString(2, name + "%");
89
90 res = stmt.executeQuery();
91 result = convertResultset(res);
92
93 } finally {
94 if (res != null)
95 res.close();
96 if (stmt != null)
97 stmt.close();
98 if (conn!= null)
99 conn.close();
100 }
101 return result;
102 }
103
104 public List<StationBean> getByLocation(double latitude, double longitude) throws SQLException {
105 String SQL = "SELECT * FROM ( "+
106 " SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address," +
107 " earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist " +
108 " FROM trainstations " +
109 " WHERE enabled = true " +
110 " ) AS trainstations2 " +
111 "ORDER BY calcdist ASC " +
112 "LIMIT 4 ";
113 List<StationBean> result;
114 Connection conn = null;
115 PreparedStatement stmt = null;
116 ResultSet res = null;
117 try {
118 conn = DBConnection.getConnection();
119 stmt = conn.prepareStatement(SQL);
120 stmt.setDouble(1, latitude);
121 stmt.setDouble(2, longitude);
122 res = stmt.executeQuery();
123 result = convertResultset(res);
124
125 } finally {
126 if (res != null)
127 res.close();
128 if (stmt != null)
129 stmt.close();
130 if (conn!= null)
131 conn.close();
132 }
133 return result;
134 }
135
136 public List<StationBean> getByList(String list) throws SQLException {
137 String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro, address,0.0 " +
138 "FROM trainstations " +
139 "WHERE id IN " + list + " AND enabled = true " +
140 "ORDER BY name ";
141
142 Connection conn = null;
143 Statement stmt = null;
144 ResultSet res = null;
145 List<StationBean> result;
146
147 try {
148 conn = DBConnection.getConnection();
149 stmt = conn.createStatement();
150 res = stmt.executeQuery(SQL);
151 result = convertResultset(res);
152 } finally {
153 if (res != null)
154 res.close();
155 if (stmt != null)
156 stmt.close();
157 if (conn!= null)
158 conn.close();
159 }
160
161 return result;
162
163 }
164 public static String getStationName(int stationID) {
165 String station = "";
166
167 Connection conn = null;
168 try {
169 conn = DBConnection.getConnection();
170 Statement stmt = conn.createStatement();
171 ResultSet rs = stmt.executeQuery("SELECT name FROM trainstations WHERE id=" + stationID);
172 if (rs.next()) {
173 station = rs.getString(1);
174 }
175
176 } catch (Exception e) {
177 } finally {
178 try {
179 if (conn != null && !conn.isClosed())
180 conn.close();
181 } catch (Exception e) {}
182 }
183
184 return station;
185 }
186 }

  ViewVC Help
Powered by ViewVC 1.1.20