Commit a69aa5fa authored by Oleh Astappiev's avatar Oleh Astappiev
Browse files

Move database credentials to DatabaseManager

parent b20a7fab
......@@ -4,13 +4,10 @@ import de.l3s.shs.shsclienttest.test.Runner;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.IOException;
import java.io.InputStream;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class DatabaseLogger {
private static final Logger logger = LogManager.getLogger(Runner.class);
......@@ -40,19 +37,7 @@ public class DatabaseLogger {
private DatabaseManager databaseManager;
public DatabaseLogger(ProbeType probeType, String comment) throws SQLException {
try (InputStream input = DatabaseLogger.class.getClassLoader().getResourceAsStream("database.properties")) {
Properties prop = new Properties();
if (input == null) {
throw new IOException("Sorry, unable to find database.properties");
}
prop.load(input);
databaseManager = new DatabaseManager(prop.getProperty("database.host"), prop.getProperty("database.table"), prop.getProperty("database.username"), prop.getProperty("database.password"));
} catch (IOException ex) {
throw new RuntimeException("Can't read database credentials", ex);
}
databaseManager = new DatabaseManager();
databaseManager.getConnection().setAutoCommit(false);
insertStatement = databaseManager.prepareStatement("INSERT INTO request (probe_id, request_type, value, execution_time, results_count) VALUES (?, ?, ?, ?, ?);");
......
......@@ -3,7 +3,10 @@ package de.l3s.shs.shsclienttest.database;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class DatabaseManager {
......@@ -30,6 +33,24 @@ public class DatabaseManager {
this.password = password;
}
public DatabaseManager() {
try (InputStream input = DatabaseLogger.class.getClassLoader().getResourceAsStream("database.properties")) {
Properties prop = new Properties();
if (input == null) {
throw new IOException("Sorry, unable to find database.properties");
}
prop.load(input);
this.host = prop.getProperty("database.host");
this.database = prop.getProperty("database.database");
this.username = prop.getProperty("database.username");
this.password = prop.getProperty("database.password");
} catch (IOException ex) {
throw new RuntimeException("Can't read database credentials", ex);
}
}
public Connection getConnection() throws SQLException {
if (connection == null) {
connection = DriverManager.getConnection("jdbc:mysql://" + host + "/"+ database + "?user=" + username + "&password=" + password + "&useAffectedRows=true");
......
database.host = "localhost:3306";
database.table = "username";
database.username = "username";
database.password = "password";
database.host = localhost:3306
database.database = username
database.username = username
database.password = password
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment