Advertisement
Guest User

Untitled

a guest
Apr 7th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1.  
  2. import org.json.simple.JSONArray;
  3. import org.json.simple.JSONObject;
  4. import org.json.simple.parser.JSONParser;
  5. import org.json.simple.parser.ParseException;
  6. import sun.org.mozilla.javascript.internal.json.JsonParser;
  7.  
  8. import java.io.File;
  9. import java.io.FileReader;
  10. import java.io.IOException;
  11. import java.sql.Connection;
  12. import java.sql.DriverManager;
  13. import java.sql.PreparedStatement;
  14. import java.sql.SQLException;
  15. import java.util.Random;
  16.  
  17. public class A {
  18.  
  19. private Connection connection;
  20.  
  21. public void init() {
  22.  
  23.  
  24. String address = "localhost";
  25. String dbName = "vpp";
  26. String username = "root";
  27. String password = "";
  28. try {
  29. Class.forName("com.mysql.jdbc.Driver");
  30. String path = String.format("jdbc:mysql://%s/%s?user=%s&password=%s", address, dbName, username, password);
  31. this.connection = DriverManager.getConnection(path);
  32. } catch (ClassNotFoundException e) {
  33. e.printStackTrace();
  34. } catch (SQLException e) {
  35. e.printStackTrace();
  36. }
  37.  
  38.  
  39. File file = new File(System.getProperty("user.dir"), "data.json");
  40.  
  41. JSONParser parser = new JSONParser();
  42.  
  43. JSONObject obj = null;
  44. try {
  45. obj = (JSONObject) parser.parse(new FileReader(file));
  46. } catch (IOException | ParseException e) {
  47. e.printStackTrace();
  48. }
  49.  
  50. for (Object object : (JSONArray) obj.get("list")) {
  51.  
  52. JSONObject oo = (JSONObject) object;
  53. JSONObject coll = new JSONObject();
  54.  
  55. JSONObject weather_station = new JSONObject();
  56.  
  57. double humidty = (double) ((JSONObject) oo.get("main")).get("humidity");
  58. humidty = new Random().nextBoolean() ? (humidty + (humidty / 100)) : (humidty - (humidty / 100));
  59.  
  60. double temperature = (double)((JSONObject) oo.get("main")).get("temp");
  61. temperature = new Random().nextBoolean() ? (temperature + (temperature / 100)) : (temperature - (temperature / 100));
  62.  
  63.  
  64. double pressure = (double)((JSONObject) oo.get("main")).get("grnd_level");
  65. pressure = new Random().nextBoolean() ? (pressure + (pressure / 100)) : (pressure - (pressure / 100));
  66.  
  67.  
  68. double windSpeed = (double) ((JSONObject) oo.get("main")).get("grnd_level");
  69. windSpeed = new Random().nextBoolean() ? (windSpeed + (windSpeed / 100)) : (windSpeed - (windSpeed / 100));
  70.  
  71.  
  72. weather_station.put("humidity", humidty);
  73. weather_station.put("temperature", humidty);
  74. weather_station.put("pressure", pressure);
  75. weather_station.put("wind_speed", windSpeed);
  76.  
  77. weather_station.put("ldrA", 405.5);
  78. weather_station.put("ldrB", 405.5);
  79. weather_station.put("ldrC", 405.5);
  80. weather_station.put("ldrD", 405.5);
  81.  
  82. coll.put("openweathermap", oo);
  83. coll.put("weather_station", weather_station);
  84.  
  85.  
  86. try {
  87. PreparedStatement statement = this.connection.prepareStatement("INSERT INTO `vpp_sensor_data` (node_id, data, received) VALUES (?, ?, ?)");
  88. statement.setObject(1, "0013A2004071CF15");
  89. statement.setObject(2, coll.toJSONString());
  90. statement.setObject(3, oo.get("dt_txt"));
  91. statement.executeUpdate();
  92. statement.close();
  93. } catch (SQLException e) {
  94. e.printStackTrace();
  95. }
  96.  
  97. System.out.println();
  98.  
  99. }
  100.  
  101. }
  102.  
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement