package com.dronerecon.ws;.

Added on - 19 Sep 2019

  • 4

    Pages

  • 740

    Words

  • 172

    Views

  • 0

    Downloads

Trusted by +2 million users,
1000+ happy students everyday
Showing pages 1 to 2 of 4 pages
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 inlocal project folder.// For running in IDE but accessing full path to DB in Tomcat website (NOTE: May need toadjust 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 stringString 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");
desklib-logo
You’re reading a preview
Preview Documents

To View Complete Document

Click the button to download
Subscribe to our plans

Download This Document