Listify/Lambdas/Lists/src/main/java/DBConnector.java
NMerz de0bd51eaf Basic ListAdd Lambda
Create a basic list adding Lambda.

NOTE: This does not include the following which are still needed:
1) Permanent DB Setup
2) Better RDS access control with security groups
3) Proper input parameters for the List add Lambda
2020-09-26 16:53:44 -04:00

54 lines
1.8 KiB
Java

import org.json.JSONObject;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class DBConnector {
Connection connection;
DBConnector() throws IOException, SQLException, ClassNotFoundException {
this(loadProperties("dbProperties.json"));
}
DBConnector(Properties dbProperties) throws SQLException, ClassNotFoundException {
Class.forName("org.mariadb.jdbc.Driver");
System.out.println(dbProperties);
System.out.println(DBConnector.buildURL(dbProperties));
connection = DriverManager.getConnection(dbProperties.get("url").toString(), dbProperties.get("user").toString(), dbProperties.get("password").toString());
System.out.println(connection);
}
public void close() {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public Connection getConnection() {
return connection;
}
public static Properties loadProperties(String path) throws IOException {
Properties toReturn = new Properties();
String propertiesJSONString = Files.readString(Path.of(path));
JSONObject propertiesJSON = new JSONObject(propertiesJSONString);
propertiesJSON.keys().forEachRemaining(key -> toReturn.setProperty(key, propertiesJSON.get(key).toString()));
return toReturn;
}
public static String buildURL(Properties dbProperties) {
String dbURL = dbProperties.get("url").toString();
dbURL += "?user=" + dbProperties.get("user").toString();
dbURL += "&password=" + dbProperties.get("password").toString();
return dbURL;
}
}