Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.servlet.ServletConfig;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import com.google.gson.Gson;
- import java.util.*;
- import java.io.*;
- import java.sql.*;
- /**
- * Servlet implementation class sensorToDB
- */
- @WebServlet("/DataValidation")
- public class DataValidation extends HttpServlet
- {
- private static final long serialVersionUID = 1L;
- Gson gson = new Gson();
- Connection conn = null;
- Statement stmt;
- public void init(ServletConfig config) throws ServletException
- {
- // init method is run once at the start of the servlet loading
- // This will load the driver and establish a connection
- super.init(config);
- String user = "iqbali";
- String password = "Bredjcwe4";
- // Note none default port used, 6306 not 3306
- String url = "jdbc:mysql://mudfoot.doc.stu.mmu.ac.uk:6306/"+user;
- // Load the database driver
- try { Class.forName("com.mysql.jdbc.Driver").newInstance();
- } catch (Exception e) {
- System.out.println(e);
- }
- // get a connection with the user/pass
- try {
- conn = DriverManager.getConnection(url, user, password);
- System.out.println("Sensor to DB server is up and running\n");
- System.out.println("Upload sensor data with http://localhost:8080/MADServerSide/SensorServerDB?sensordata=some_sensor_data_in_json_format");
- System.out.println("View last sensor reading at http://localhost:8080/MADServerSide/SensorServerDB?getdata=true\n\n");
- System.out.println("DEBUG: Connection to database successful.");
- stmt = conn.createStatement();
- } catch (SQLException se) {
- System.out.println(se);
- System.out.println("\nDid you alter the lines to set user/password in the sensor server code?");
- }
- } // init()
- public void destroy() {
- try { conn.close(); } catch (SQLException se) {
- System.out.println(se);
- }
- } // destroy()
- public DataValidation() {
- super();
- // TODO Auto-generated constructor stub
- }
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
- {
- response.setStatus(HttpServletResponse.SC_OK);
- // Declare a SensorData object to hold the incoming data
- SensorData oneSensor = new SensorData("unknown", "unknown");
- // getdata is null, therefore it is receiving data
- // Extract the parameter data holding the sensordata
- String sensorJsonString = request.getParameter("sensordata");
- // Problem if sensordata parameter not sent, or is invalid json
- if (sensorJsonString != null)
- {
- // Convert the json string to an object of type SensorData
- oneSensor = gson.fromJson(sensorJsonString, SensorData.class);
- retrieveSensorData(oneSensor.getSensorname());
- // Update sensor values and send back response
- //PrintWriter out = response.getWriter();
- //out.println(resultsJson);
- //out.close();
- } // endif sensorJsonString not null
- // Code to retrieve data
- }
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
- {
- // Post is same as Get, so pass on parameters and do same
- doGet(request, response);
- }
- private Boolean retrieveSensorData(String sensorname)
- {
- if (tagItemsList.contains(tagStr))
- {
- String selectSQL = "select * from validation";
- ResultSet rs;
- // Declare ArrayList of sensors to hold results
- Boolean allSensors = new ArrayList<String>();
- try
- {
- // create a result set of selected values
- rs = stmt.executeQuery(selectSQL);
- // Declare a SensorData object to hold individual values,
- // initialise to defaults
- while (rs.next())
- {
- // add this sensor to ArrayList of Sensors
- allSensors.add(rs.getString("sensorname"));
- }
- return true;
- } catch (SQLException ex)
- {
- System.out.println("Error in SQL " + ex.getMessage());
- System.out.println("UNKNOWN TAG. DOOR LOCKED.");
- return false;
- }
- // Convert sensor list to json array and send back to user
- // return this String from method
- return allSensors ;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement