Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. import javax.servlet.ServletConfig;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.annotation.WebServlet;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7.  
  8. import com.google.gson.Gson;
  9.  
  10. import java.util.*;
  11. import java.io.*;
  12. import java.sql.*;
  13.  
  14.  
  15. /**
  16. * Servlet implementation class sensorToDB
  17. */
  18. @WebServlet("/DataValidation")
  19. public class DataValidation extends HttpServlet
  20. {
  21. private static final long serialVersionUID = 1L;
  22.  
  23. Gson gson = new Gson();
  24. Connection conn = null;
  25. Statement stmt;
  26.  
  27. public void init(ServletConfig config) throws ServletException
  28. {
  29. // init method is run once at the start of the servlet loading
  30. // This will load the driver and establish a connection
  31. super.init(config);
  32. String user = "iqbali";
  33. String password = "Bredjcwe4";
  34. // Note none default port used, 6306 not 3306
  35. String url = "jdbc:mysql://mudfoot.doc.stu.mmu.ac.uk:6306/"+user;
  36.  
  37. // Load the database driver
  38. try { Class.forName("com.mysql.jdbc.Driver").newInstance();
  39. } catch (Exception e) {
  40. System.out.println(e);
  41. }
  42.  
  43. // get a connection with the user/pass
  44. try {
  45. conn = DriverManager.getConnection(url, user, password);
  46. System.out.println("Sensor to DB server is up and running\n");
  47. System.out.println("Upload sensor data with http://localhost:8080/MADServerSide/SensorServerDB?sensordata=some_sensor_data_in_json_format");
  48. System.out.println("View last sensor reading at http://localhost:8080/MADServerSide/SensorServerDB?getdata=true\n\n");
  49.  
  50. System.out.println("DEBUG: Connection to database successful.");
  51. stmt = conn.createStatement();
  52. } catch (SQLException se) {
  53. System.out.println(se);
  54. System.out.println("\nDid you alter the lines to set user/password in the sensor server code?");
  55. }
  56. } // init()
  57.  
  58. public void destroy() {
  59. try { conn.close(); } catch (SQLException se) {
  60. System.out.println(se);
  61. }
  62. } // destroy()
  63.  
  64. public DataValidation() {
  65. super();
  66. // TODO Auto-generated constructor stub
  67. }
  68.  
  69. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  70. {
  71. response.setStatus(HttpServletResponse.SC_OK);
  72. // Declare a SensorData object to hold the incoming data
  73. SensorData oneSensor = new SensorData("unknown", "unknown");
  74.  
  75. // getdata is null, therefore it is receiving data
  76. // Extract the parameter data holding the sensordata
  77. String sensorJsonString = request.getParameter("sensordata");
  78.  
  79. // Problem if sensordata parameter not sent, or is invalid json
  80. if (sensorJsonString != null)
  81. {
  82. // Convert the json string to an object of type SensorData
  83. oneSensor = gson.fromJson(sensorJsonString, SensorData.class);
  84.  
  85. retrieveSensorData(oneSensor.getSensorname());
  86. // Update sensor values and send back response
  87. //PrintWriter out = response.getWriter();
  88. //out.println(resultsJson);
  89. //out.close();
  90.  
  91. } // endif sensorJsonString not null
  92. // Code to retrieve data
  93.  
  94. }
  95.  
  96.  
  97.  
  98. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  99. {
  100. // Post is same as Get, so pass on parameters and do same
  101. doGet(request, response);
  102. }
  103.  
  104. private Boolean retrieveSensorData(String sensorname)
  105. {
  106. String selectSQL = "select * from validation";
  107. ResultSet rs;
  108.  
  109. // Declare ArrayList of sensors to hold results
  110. ArrayList<String> allSensors = new ArrayList<String>();
  111.  
  112. try
  113. {
  114. // create a result set of selected values
  115. rs = stmt.executeQuery(selectSQL);
  116.  
  117. // Declare a SensorData object to hold individual values,
  118. // initialise to defaults
  119. while (rs.next())
  120. {
  121. // add this sensor to ArrayList of Sensors
  122. allSensors.add(rs.getString("sensorname"));
  123. }
  124.  
  125. return allSensors.contains(sensorname);
  126.  
  127. } catch (SQLException ex)
  128. {
  129. System.out.println("Error in SQL " + ex.getMessage());
  130. System.out.println("UNKNOWN TAG. DOOR LOCKED.");
  131.  
  132. return false;
  133. }
  134. // Convert sensor list to json array and send back to user
  135.  
  136.  
  137. }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement