Advertisement
Guest User

Untitled

a guest
Dec 6th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.50 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("/Validation")
  19. public class Validation extends HttpServlet {
  20.  
  21. private static final long serialVersionUID = 1L;
  22.  
  23. Gson gson = new Gson();
  24.  
  25. Connection conn = null;
  26. Statement stmt;
  27. String tagItemsList = null;
  28.  
  29. public void init(ServletConfig config) throws ServletException {
  30. // init method is run once at the start of the servlet loading
  31. // This will load the driver and establish a connection
  32. super.init(config);
  33. String user = "hussamoh";
  34. String password = "Brenkoil8";
  35. // Note none default port used, 6306 not 3306
  36. String url = "jdbc:mysql://mudfoot.doc.stu.mmu.ac.uk:6306/"+user;
  37.  
  38. // Load the database driver
  39. try { Class.forName("com.mysql.jdbc.Driver").newInstance();
  40. } catch (Exception e) {
  41. System.out.println(e);
  42. }
  43.  
  44. // get a connection with the user/pass
  45. try {
  46. conn = DriverManager.getConnection(url, user, password);
  47. System.out.println("Sensor to DB server is up and running\n");
  48. System.out.println("Upload sensor data with http://localhost:8080/MobileAppServer/Validation?sensordata=some_sensor_data_in_json_format");
  49. System.out.println("View last sensor reading at http://localhost:8080/MobileAppServer/Validation?getdata=true\n\n");
  50.  
  51. System.out.println("DEBUG: Connection to database successful.");
  52. stmt = conn.createStatement();
  53. } catch (SQLException se) {
  54. System.out.println(se);
  55. System.out.println("\nDid you alter the lines to set user/password in the sensor server code?");
  56. }
  57. } //init()
  58.  
  59. public void destroy() {
  60. try { conn.close(); } catch (SQLException se) {
  61. System.out.println(se);
  62. }
  63. } // destroy()
  64.  
  65.  
  66. public Validation() {
  67. super();
  68. // TODO Auto-generated constructor stub
  69. }
  70.  
  71.  
  72. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  73. response.setStatus(HttpServletResponse.SC_OK);
  74. // Declare a SensorData object to hold the incoming data
  75. SensorData oneSensor = new SensorData("unknown", "unknown", "unknown", "unknown");
  76.  
  77.  
  78.  
  79. // if no getdata parameter, client is sending data
  80. // getdata is null, therefore it is receiving data
  81. // Extract the parameter data holding the sensordata
  82. String sensorJsonString = request.getParameter("sensordata");
  83. System.out.println(sensorJsonString);
  84. // Problem if sensordata parameter not sent, or is invalid json
  85. if (sensorJsonString != null) {
  86. // Convert the json string to an object of type SensorData
  87. oneSensor = gson.fromJson(sensorJsonString, SensorData.class);
  88.  
  89. Boolean result = retrieveSensorData("", oneSensor.getSensorName());
  90. PrintWriter out = response.getWriter();
  91. if (result == true )
  92. out.println("true");
  93. else out.println("false");
  94. out.close();
  95.  
  96. } // endif sensorJsonString not null
  97. } // end if getdata is null
  98. // Retrieve and return data (JSON format)
  99. // Code to retrieve data
  100.  
  101. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  102. // Post is same as Get, so pass on parameters and do same
  103. doGet(request, response);
  104. }
  105.  
  106. private void updateSensorTable(SensorData oneSensor){
  107. try {
  108. // Create the INSERT statement from the parameters
  109. // set time inserted to be the current time on database server
  110. String updateSQL =
  111. "insert into sensorusage(userid, sensorname, sensorvalue, sensordate) " +
  112. "values('"+oneSensor.getUserId() + "'," +
  113. oneSensor.getSensorName() + "','" +
  114. oneSensor.getSensorValue() + "','" +
  115. "now());";
  116.  
  117. System.out.println("DEBUG: Update: " + updateSQL);
  118. stmt.executeUpdate(updateSQL);
  119. System.out.println("DEBUG: Update successful ");
  120. } catch (SQLException se) {
  121. // Problem with update, return failure message
  122. System.out.println(se);
  123. System.out.println("\nDEBUG: Update error - see error trace above for help. ");
  124. return;
  125. }
  126. }
  127.  
  128. public boolean retrieveSensorData(String locks, String tagname) {
  129.  
  130. try {
  131. PreparedStatement ps = null;
  132. String query = "select * from unlocks where tagname ='" +tagname+"'";
  133. System.out.println (query);
  134. ps = conn.prepareStatement(query);
  135. ResultSet rs = ps.executeQuery();
  136. if (rs.next()) {
  137. return true;
  138. } else {
  139. return false;
  140. }
  141. } catch (SQLException ex) {
  142.  
  143. }
  144.  
  145. return false;
  146. }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement