add class DBConfig

AddSQL_DB
adb 3 years ago
parent 302b89b14f
commit f1c5199d89

@ -2,7 +2,7 @@
"storePath": "./data/", "storePath": "./data/",
"apiURL": "https://data.sensor.community/airrohr/v1/sensor/35943/", "apiURL": "https://data.sensor.community/airrohr/v1/sensor/35943/",
"interval": 150, "interval": 150,
"sqlConnection": { "dbConfig": {
"db": "", "db": "",
"pw": "", "pw": "",
"user": "", "user": "",

@ -0,0 +1,35 @@
package sh.adb.sensorCommunityAPI;
import org.json.simple.JSONObject;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBConfig {
String pw;
String db;
String user;
int port;
String server;
void setConfig(String pw, String db, String user, int port, String server){
this.pw = pw;
this.db = db;
this.user = user;
this.port = port;
this.server = server;
}
void setJsonConfig(JSONObject config){
this.pw = (String) config.get("pw");
this.db = (String) config.get("db");
this.user = (String) config.get("user");
this.port = (Integer) config.get("port");
this.server = (String) config.get("server");
}
Connection getDbConnection() throws SQLException {
return DriverManager.getConnection(
"jdbc:mariadb://"+this.server+":"+String.valueOf(this.port)+"/"+this.db+"?user="+this.user+"&password="+this.pw
);
}
}

@ -0,0 +1,12 @@
package sh.adb.sensorCommunityAPI;
import java.sql.Connection;
import java.sql.SQLException;
public class DBHandler {
Connection connection;
DBHandler(DBConfig config) throws SQLException {
this.connection = config.getDbConnection();
}
}

@ -6,6 +6,7 @@ import org.json.simple.parser.ParseException;
import java.io.IOException; import java.io.IOException;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
@ -16,13 +17,13 @@ public class DataStoreBot {
String apiURL; String apiURL;
APIParser api; APIParser api;
DataStore store; DataStore store;
dbHandler db; DBHandler db;
DataStoreBot(String storePath, String apiURL) throws MalformedURLException, SQLException { DataStoreBot(String storePath, String apiURL, DBConfig dbConfig) throws MalformedURLException, SQLException {
this.storePath = storePath; this.storePath = storePath;
this.apiURL = apiURL; this.apiURL = apiURL;
this.api = new APIParser(this.apiURL); this.api = new APIParser(this.apiURL);
this.store = new DataStore(); this.store = new DataStore();
this.db = new dbHandler((JSONObject) store.readJSON("config.json").get("sqlConnection")); this.db = new DBHandler(dbConfig);
} }
public void setInterval(int seconds) { public void setInterval(int seconds) {
ScheduledExecutorService ses = Executors.newScheduledThreadPool(1); ScheduledExecutorService ses = Executors.newScheduledThreadPool(1);

@ -3,16 +3,21 @@ package sh.adb.sensorCommunityAPI;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
import java.io.IOException; import java.io.IOException;
import java.sql.SQLException;
public class Main { public class Main {
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException, SQLException {
DataStore store = new DataStore(); DataStore store = new DataStore();
JSONObject config = store.readJSON("config.json"); JSONObject config = store.readJSON("config.json");
if (config == null) config = store.readJSON("example.config.json"); if (config == null) config = store.readJSON("example.config.json");
DBConfig dbConfig = new DBConfig();
dbConfig.setJsonConfig((JSONObject) config.get("dbConfig"));
DataStoreBot bot = new DataStoreBot( DataStoreBot bot = new DataStoreBot(
config.get("storePath").toString(), config.get("storePath").toString(),
config.get("apiURL").toString() config.get("apiURL").toString(),
dbConfig
); );
bot.setInterval((int)(long) config.get("interval")); bot.setInterval((int)(long) config.get("interval"));
} }

@ -1,27 +0,0 @@
package sh.adb.sensorCommunityAPI;
import org.json.simple.JSONObject;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class dbHandler {
String pw;
String db;
String user;
int port;
String server;
Connection connection;
dbHandler(JSONObject config) throws SQLException {
this.pw = (String) config.get("pw");
this.db = (String) config.get("db");
this.user = (String) config.get("user");
this.port = (Integer) config.get("port");
this.server = (String) config.get("server");
Connection connection = DriverManager.getConnection(
"jdbc:mariadb://"+this.server+":"+this.port+"/"+this.db+"?user="+this.user+"&password="+this.pw
);
}
}
Loading…
Cancel
Save