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

Annotation of /android/TrainInfoServiceGoogle/src/dk/thoerup/traininfoservice/StationDAO.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 650 - (hide annotations) (download)
Mon Apr 19 19:04:34 2010 UTC (14 years, 1 month ago) by torben
Original Path: android/TrainInfoService/src/dk/thoerup/traininfoservice/StationDAO.java
File size: 4730 byte(s)
Move function getStationName() function do DAO class
1 torben 588 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 torben 589 Statement stmt = null;
44     ResultSet res = null;
45 torben 588 StationBean result;
46    
47     try {
48     conn = DBConnection.getConnection();
49    
50 torben 589 stmt = conn.createStatement();
51     res = stmt.executeQuery(SQL);
52 torben 588 res.next();
53     result = convertSingleRow(res);
54     } finally {
55 torben 589 if (res != null)
56     res.close();
57     if (stmt != null)
58     stmt.close();
59 torben 588 if (conn != null)
60     conn.close();
61     }
62    
63     return result;
64     }
65    
66    
67     public List<StationBean> getByName(String name) throws SQLException {
68     String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " +
69     "FROM trainstations " +
70     "WHERE name ILIKE ? AND enabled = true " +
71     "ORDER BY name ";
72    
73    
74     List<StationBean> result;
75     Connection conn = null;
76 torben 589 PreparedStatement stmt = null;
77     ResultSet res = null;
78 torben 588 try {
79     conn = DBConnection.getConnection();
80 torben 589 stmt = conn.prepareStatement(SQL);
81 torben 588
82     stmt.setString(1, name + "%");
83    
84 torben 589 res = stmt.executeQuery();
85     result = convertResultset(res);
86 torben 588
87     } finally {
88 torben 589 if (res != null)
89     res.close();
90     if (stmt != null)
91     stmt.close();
92     if (conn!= null)
93 torben 588 conn.close();
94     }
95     return result;
96     }
97    
98     public List<StationBean> getByLocation(double latitude, double longitude) throws SQLException {
99     String SQL = "SELECT * FROM ( "+
100     " SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address," +
101     " earth_distance( ll_to_earth(latitude,longitude), ll_to_earth(?,?))::int AS calcdist " +
102     " FROM trainstations " +
103     " WHERE enabled = true " +
104     " ) AS trainstations2 " +
105     "ORDER BY calcdist ASC " +
106     "LIMIT 4 ";
107     List<StationBean> result;
108     Connection conn = null;
109 torben 589 PreparedStatement stmt = null;
110     ResultSet res = null;
111 torben 588 try {
112     conn = DBConnection.getConnection();
113 torben 589 stmt = conn.prepareStatement(SQL);
114 torben 588 stmt.setDouble(1, latitude);
115     stmt.setDouble(2, longitude);
116 torben 589 res = stmt.executeQuery();
117     result = convertResultset(res);
118 torben 588
119     } finally {
120 torben 589 if (res != null)
121     res.close();
122     if (stmt != null)
123     stmt.close();
124     if (conn!= null)
125 torben 588 conn.close();
126     }
127     return result;
128     }
129    
130     public List<StationBean> getByList(String list) throws SQLException {
131     String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro, address,0.0 " +
132     "FROM trainstations " +
133     "WHERE id IN " + list + " AND enabled = true " +
134     "ORDER BY name ";
135    
136     Connection conn = null;
137 torben 589 Statement stmt = null;
138     ResultSet res = null;
139 torben 588 List<StationBean> result;
140    
141     try {
142     conn = DBConnection.getConnection();
143 torben 589 stmt = conn.createStatement();
144     res = stmt.executeQuery(SQL);
145 torben 588 result = convertResultset(res);
146     } finally {
147 torben 589 if (res != null)
148     res.close();
149     if (stmt != null)
150     stmt.close();
151 torben 588 if (conn!= null)
152     conn.close();
153     }
154    
155     return result;
156    
157     }
158 torben 650 public static String getStationName(int stationID) {
159     String station = "";
160    
161     Connection conn = null;
162     try {
163     conn = DBConnection.getConnection();
164     Statement stmt = conn.createStatement();
165     ResultSet rs = stmt.executeQuery("SELECT name FROM trainstations WHERE id=" + stationID);
166     if (rs.next()) {
167     station = rs.getString(1);
168     }
169    
170     } catch (Exception e) {
171     } finally {
172     try {
173     if (conn != null && !conn.isClosed())
174     conn.close();
175     } catch (Exception e) {}
176     }
177    
178     return station;
179     }
180 torben 588 }

  ViewVC Help
Powered by ViewVC 1.1.20