Add DBinputt

AddSQL_DB
TechCrafter07 4 years ago
parent f1c5199d89
commit f5a54dfb74

@ -24,7 +24,7 @@ public class DBConfig {
this.pw = (String) config.get("pw"); this.pw = (String) config.get("pw");
this.db = (String) config.get("db"); this.db = (String) config.get("db");
this.user = (String) config.get("user"); this.user = (String) config.get("user");
this.port = (Integer) config.get("port"); this.port = (int) (long) config.get("port");
this.server = (String) config.get("server"); this.server = (String) config.get("server");
} }
Connection getDbConnection() throws SQLException { Connection getDbConnection() throws SQLException {

@ -2,11 +2,37 @@ package sh.adb.sensorCommunityAPI;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement;
public class DBHandler { public class DBHandler {
Connection connection; Connection connection;
DBHandler(DBConfig config) throws SQLException { DBHandler(DBConfig config) throws SQLException {
this.connection = config.getDbConnection(); this.connection = config.getDbConnection();
//storeInDB(); // Löpt
//storeInDB("INSERT INTO Test (id, trash) VALUES (5, 4)"); //löpt to xD
}
public void storeInDB() {
try (Statement stmt = this.connection.createStatement()) {
stmt.executeUpdate("INSERT INTO Test (id, trash) VALUES (1, 2)");
} catch (SQLException e) {
e.printStackTrace();
}
}
public void storeInDB(String SQL) {
try (Statement stmt = this.connection.createStatement()) {
stmt.executeUpdate(SQL);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void storeInDB(String country, int sensorID, String timeStamp, String value_type_p1, int id_p1, float value_p1, String value_type_p2, int id_p2, float value_p2, String allJSON) {
try (Statement stmt = this.connection.createStatement()) {
//stmt.executeUpdate("INSERT INTO apidata (id, country, sensorID, Timestamp, value_type_p1, id_p1, value_p1, value_type_p2, id_p2, value_p2, allJSON ) VALUES (0, " + country + ", " + sensorID + ", " + timeStamp + ", " + value_type_p1 + ", " + id_p1 + ", " + value_p1 + ", " + value_type_p2 + ", " + id_p2 + ", " + value_p2 + ", " + allJSON + ")");
stmt.executeUpdate("INSERT INTO apidata (id, country, sensorID, Timestamp, value_type_p1, id_p1, value_p1, value_type_p2, id_p2, value_p2, allJSON ) VALUES ( NULL , '" + country + "', " + sensorID + " , '" + timeStamp +"', '" + value_type_p1 + "', " + id_p1 + ", " + value_p1 + ", '" + value_type_p2 + "', " + id_p2 + " , " + value_p2 + " , '" + allJSON + "' )");
} catch (SQLException e) {
e.printStackTrace();
}
} }
} }

@ -8,6 +8,8 @@ import java.io.IOException;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -41,6 +43,7 @@ public class DataStoreBot {
this.storeEntry(entry); this.storeEntry(entry);
} }
public void storeEntry(JSONObject entry) throws IOException { public void storeEntry(JSONObject entry) throws IOException {
System.out.println("entry => "); System.out.println("entry => ");
System.out.println(entry); System.out.println(entry);
//get country //get country
@ -50,12 +53,34 @@ public class DataStoreBot {
String sensorID = String.valueOf( String sensorID = String.valueOf(
((JSONObject) entry.get("sensor")).get("id") ((JSONObject) entry.get("sensor")).get("id")
); );
int sensorID2 = Integer.parseInt(sensorID);
//get timestamp //get timestamp
String timestamp = (String) entry.get("timestamp"); String timestamp = (String) entry.get("timestamp");
//get value informations (6)
JSONArray value_array = (JSONArray) entry.get("sensordatavalues");
System.out.println("----------------------------");
Iterator iterator = value_array.iterator();
JSONObject firstArr = (JSONObject) iterator.next();
String value_type_p1 = (String) firstArr.get("value_type");
int id_p1 = (int) (long) firstArr.get("id");
float value_p1 = Float.parseFloat((String) firstArr.get("value")) ;
JSONObject secondArr = (JSONObject) iterator.next();
String value_type_p2 = (String) secondArr.get("value_type");
int id_p2 = (int) (long) secondArr.get("id");
float value_p2 = Float.parseFloat((String) firstArr.get("value")) ;
// Json to String
String allJSON = String.valueOf(entry);
String path = this.storePath + country + "-" + sensorID + "-" + timestamp + ".json"; String path = this.storePath + country + "-" + sensorID + "-" + timestamp + ".json";
path = path.replace(":", "-").replace(" ", "-"); path = path.replace(":", "-").replace(" ", "-");
this.store.storeJSON(path, entry); this.store.storeJSON(path, entry);
//System.out.println(country + sensorID2 + timestamp + value_type_p1 + id_p1 + value_p1 + value_type_p2 + id_p2 + value_p2 + allJSON);
this.db.storeInDB(country, sensorID2, timestamp, value_type_p1, id_p1, value_p1, value_type_p2, id_p2, value_p2, allJSON);
//String country, int sensorID, String timeStamp, String value_type_p1, int id_p1, float value_p1, String value_type_p2, int id_p2, float value_p2, String allJSON
} }
} }

Loading…
Cancel
Save