QueryWithContext.java
9.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package com.servlet.utilities;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public final class QueryWithContext {
public static void queryCalibration(PrintWriter out) throws NamingException {
Context context = null;
DataSource datasource = null;
Connection connect = null;
Statement statement = null;
try {
// Get the context and create a connection
context = new InitialContext();
datasource = (DataSource) context.lookup("java:/comp/env/jdbc/ips");
connect = datasource.getConnection();
// Create the statement to be used to get the results.
statement = connect.createStatement();
String query = "SELECT * FROM location";
// Execute the query and get the result set.
ResultSet resultSet = statement.executeQuery(query);
out.println("<strong>Printing result using context file...</strong><br>");
while (resultSet.next()) {
int id = resultSet.getInt("id");
float x = resultSet.getFloat("x");
float y = resultSet.getFloat("y");
float z = resultSet.getFloat("z");
int bssid = resultSet.getInt("bssid");
out.println("id: " + id +
", x: " + x +
", y: " + y +
", z: " + z +
", bssid: " + bssid );
}
} catch (SQLException e) { e.printStackTrace(out);
} finally {
// Close the connection and release the resources used.
try { statement.close(); } catch (SQLException e) { e.printStackTrace(out); }
try { connect.close(); } catch (SQLException e) { e.printStackTrace(out); }
}
}
/**
Add location into database.
* @param bssid a string
* @param x the x coordinate of the location
* @param y the y coordinate of the location
* @param z the z coordinate of the location
*/
public static void queryCalibrationAddLocation(String bssid, String x, String y, String z) throws NamingException {
Context context = null;
DataSource datasource = null;
Connection connect = null;
Statement statement = null;
PrintStream out = null;
try {
// Get the context and create a connection
context = new InitialContext();
datasource = (DataSource) context.lookup("java:/comp/env/jdbc/ips");
connect = datasource.getConnection();
// Add data to Location table
statement = connect.createStatement();
String insert = "INSERT INTO location (bssid, x, y, z) VALUES ('" + bssid + "'," + x + "," + y + "," + z + ");";
// Execute the query and get the result set.
statement.executeUpdate(insert);
} catch (SQLException e) { System.out.println(e.getMessage());
} finally {
// Close the connection and release the resources used.
try { statement.close(); } catch (SQLException e) { e.printStackTrace(out); }
try { connect.close(); } catch (SQLException e) { e.printStackTrace(out); }
}
}
/**
Get all the waypoints MAC address.
* @return an array of all the MAC addresses
*/
public static ArrayList<String> queryCalibrationSelectWaypoints() throws NamingException {
ArrayList<String> waypoints = new ArrayList<String>();
Context context = null;
DataSource datasource = null;
Connection connect = null;
Statement statement = null;
PrintStream out = null;
try {
// Get the context and create a connection
context = new InitialContext();
datasource = (DataSource) context.lookup("java:/comp/env/jdbc/ips");
connect = datasource.getConnection();
// Create the statement to be used to get the results.
statement = connect.createStatement();
String query = "SELECT mac FROM WAYPOINT";
// Execute the query and get the result set.
ResultSet resultSet = statement.executeQuery(query);
while (resultSet.next()) {
String mac = resultSet.getString("mac");
waypoints.add(mac);
}
} catch (SQLException e) { e.printStackTrace(out);
} finally {
// Close the connection and release the resources used.
try { statement.close(); } catch (SQLException e) { e.printStackTrace(out); }
try { connect.close(); } catch (SQLException e) { e.printStackTrace(out); }
}
return waypoints;
}
/**
Add location into database.
* @param ypmac a string of the MAC address of the waypoint
* @param x the x coordinate of the location
* @param y the y coordinate of the location
* @param z the z coordinate of the location
* @param value the RSSI value
*/
public static void queryCalibrationAddMeasurement(String ypmac, String x, String y, String z, Double value) throws NamingException {
Context context = null;
DataSource datasource = null;
Connection connect = null;
Statement statement = null;
PrintStream out = null;
try {
// Get the context and create a connection
context = new InitialContext();
datasource = (DataSource) context.lookup("java:/comp/env/jdbc/ips");
connect = datasource.getConnection();
// Add data to measurement table
statement = connect.createStatement();
String yp_id = "SELECT id FROM WAYPOINT WHERE mac = \"" + ypmac + "\"";
ResultSet yp_ids = statement.executeQuery(yp_id);
yp_ids.next();
yp_id = yp_ids.getString(1);
String loc_id = "SELECT id FROM LOCATION WHERE x = " + x + " AND y = " + y + " AND z = "+ z;
ResultSet loc_ids = statement.executeQuery(loc_id);
loc_ids.next();
loc_id = loc_ids.getString(1);
String insert = "INSERT INTO MEASUREMENT (YP_ID, LOC_ID, SS_VALUE) VALUES (" + yp_id + ", " + loc_id + ", " + value + ")";
// Execute the query and get the result set.
statement.executeUpdate(insert);
System.out.println("DONE");
} catch (SQLException e) { System.out.println(e.getMessage());
} finally {
// Close the connection and release the resources used.
try { statement.close(); } catch (SQLException e) { e.printStackTrace(out); }
try { connect.close(); } catch (SQLException e) { e.printStackTrace(out); }
}
}
/**
Get all the measurements.
* @return an array of all the fingerprints
*/
public static ArrayList<Fingerprint> queryGetAllMeasurement() throws NamingException {
ArrayList<Fingerprint> fingerprints = new ArrayList<Fingerprint>();
Context context = null;
DataSource datasource = null;
Connection connect = null;
Statement statement = null;
PrintStream out = null;
try {
// Get the context and create a connection
context = new InitialContext();
datasource = (DataSource) context.lookup("java:/comp/env/jdbc/ips");
connect = datasource.getConnection();
// Create the statement to be used to get the results.
statement = connect.createStatement();
String query =
"SELECT LOC.X, LOC.Y, LOC.Z, MEA.ss_value, YP.MAC "
+ "FROM LOCATION LOC, MEASUREMENT MEA, WAYPOINT YP "
+ "WHERE LOC.ID = MEA.LOC_ID "
+ "AND YP.ID = MEA.YP_ID "
+ "ORDER BY LOC.X, LOC.Y, LOC.Z";
// Execute the query and get the result set.
ResultSet resultSet = statement.executeQuery(query);
float x = -1;
float y = -1;
float z = -1;
HashMap<String, Double> hm = null;
RssiSample rs;
Location loc = null;
boolean firstTime = true;
while (resultSet.next()) {
if (!(x == resultSet.getFloat("X") && y == resultSet.getFloat("Y") && z == resultSet.getFloat("Z"))){
if (!firstTime){
// Add fp to fingerprints
rs = new RssiSample(hm);
Fingerprint fp = new Fingerprint(loc, rs);
fingerprints.add(fp);
}
x = resultSet.getFloat("X");
y = resultSet.getFloat("Y");
z = resultSet.getFloat("Z");
loc = new Location(x, y, z);
hm = new HashMap<String, Double>();
}
String ypmac = resultSet.getString("mac");
float rssi = resultSet.getFloat("ss_value");
hm.put(ypmac, (double) rssi);
firstTime = false;
}
rs = new RssiSample(hm);
Fingerprint fp = new Fingerprint(loc, rs);
fingerprints.add(fp);
} catch (SQLException e) { e.printStackTrace(out);
} finally {
// Close the connection and release the resources used.
try { statement.close(); } catch (SQLException e) { e.printStackTrace(out); }
try { connect.close(); } catch (SQLException e) { e.printStackTrace(out); }
}
return fingerprints;
}
}