-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBManager.java
More file actions
129 lines (98 loc) · 4.15 KB
/
DBManager.java
File metadata and controls
129 lines (98 loc) · 4.15 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
package com.dronerecon.ws;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.ArrayList;
public class DBManager {
public String DBLocation = "dronedata.sqlite"; // For running from an IDE w/ DB located in local project folder.
// For running in IDE but accessing full path to DB in Tomcat website (NOTE: May need to adjust 8.5).
//public String DBLocation = "C:\\Program Files\\Apache Software Foundation\\Tomcat 8.5\\webapps\\dronereconportal\\db\\dronedata.sqlite";
// Create connection with DB.
// This is called internally by each CRUD function as a first step.
private Connection connect() {
// SQLite connection string
String url = "jdbc:sqlite:" + DBLocation;
Connection conn = null;
try {
Class.forName("org.sqlite.JDBC");
// Connect to DB.
conn = DriverManager.getConnection(url);
} catch (Exception e) {
System.err.println(e.getMessage());
}
return conn;
}
// Insert DB record into AreaGridTiles table.
// This is the "C" in CRUD.
public void insertAreaGridTile(String sAreaID, int iX, int iY, int iR, int iG){
Connection c = connect();
Statement stmt = null;
try {
c.setAutoCommit(false);
stmt = c.createStatement();
String sql = "INSERT INTO AreaGridTiles (area_id,x,y,r,g,timestamp) " +
"VALUES ('" + sAreaID + "'," + iX + "," + iY + "," + iR + "," + iG + ",datetime());";
stmt.executeUpdate(sql);
stmt.close();
c.commit();
c.close();
} catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
e.printStackTrace();
}
}
// Select DB records from AreaGridTiles table.
// This is the "R" in CRUD.
public ArrayList<AreaGridTile> readAreaGridTiles(String sAreaID){
Connection c = connect();
Statement stmt = null;
// Used to hold tiles retrieved from DB.
ArrayList<AreaGridTile> lstTiles = new ArrayList<>();
try {
c.setAutoCommit(false);
stmt = c.createStatement();
ResultSet rs = stmt.executeQuery( "SELECT * FROM AreaGridTiles WHERE area_id = '" + sAreaID + "';" );
while ( rs.next() ) {
sAreaID = rs.getString("area_id");
int iX = rs.getInt("x");
int iY = rs.getInt("y");
int iR = rs.getInt("r");
int iG = rs.getInt("g");
String sTimestamp = rs.getString("timestamp");
AreaGridTile oTile = new AreaGridTile();
oTile.areaID = sAreaID;
oTile.x = iX;
oTile.y = iY;
oTile.r = iR;
oTile.g = iG;
oTile.timestamp = sTimestamp;
lstTiles.add(oTile);
/* Uncomment for debugging
System.out.println( "Area ID : " + sAreaID);
System.out.println( "x : " + iX);
System.out.println( "y : " + iY);
System.out.println( "r : " + iR);
System.out.println( "g : " + iG);
System.out.println( "timestamp : " + sTimestamp);
*/
}
rs.close();
stmt.close();
c.close();
} catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
}
return lstTiles;
}
// Only for testing in an IDE.
public static void main(String[] args){
DBManager oDBManager = new DBManager();
oDBManager.insertAreaGridTile("abc123",10,10,243,109);
System.out.println("Record inserted.");
ArrayList<AreaGridTile> oTiles = oDBManager.readAreaGridTiles("23abc");
for(AreaGridTile oTile: oTiles){
System.out.println("tile: " + oTile.x + "," + oTile.y);
}
}
}