Use configs and mysql as default connector

This commit is contained in:
sfigato 2020-04-27 01:04:36 +02:00
parent fbbd2c2035
commit 5aeedf7adc
Signed by: blallo
GPG Key ID: 0CBE577C9B72DC3F
3 changed files with 50 additions and 6 deletions

View File

@ -12,7 +12,7 @@ import bean.Contatto;
public class ContattoDao implements ContattoDaoI { public class ContattoDao implements ContattoDaoI {
OracleConnection c = new OracleConnection(); MySqlConnection c = new MySqlConnection();
@Override @Override
public boolean create(String nome, String cognome, String servizio_sociale, String area, String indirizzo, public boolean create(String nome, String cognome, String servizio_sociale, String area, String indirizzo,

View File

@ -4,9 +4,12 @@ package dao;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.SQLException; import java.sql.SQLException;
import util.Config;
public class MySqlConnection { public class MySqlConnection {
Config cfg = new Config();
public static void main(String[] args) { public static void main(String[] args) {
MySqlConnection c = new MySqlConnection(); MySqlConnection c = new MySqlConnection();
c.open(); c.open();
@ -17,10 +20,10 @@ public class MySqlConnection {
public Connection open() { public Connection open() {
String nomeDriver = "com.mysql.driverDriver"; String nomeDriver = "com.mysql.jdbc.Driver";
String utente = "brigate"; //nome utente String utente = cfg.getDbUser(); //nome utente
String password = "password"; //password String password = cfg.getDbPass(); //password
String url = "jdbc:oracle:thin:@localhost:1521:orcl"; String url = "jdbc:mysql://" + cfg.getDbHost() + ":" + cfg.getDbPort() + "/brigate?autoReconnect=true&useSSL=false";
try { try {
Class.forName(nomeDriver); Class.forName(nomeDriver);

41
src/util/Config.java Normal file
View File

@ -0,0 +1,41 @@
package util;
import java.io.FileNotFoundException;
import java.util.*;
import java.util.Properties;
public class Config {
Properties configFile;
public Config() {
configFile = new java.util.Properties();
try {
configFile.load(this.getClass().getClassLoader().getResourceAsStream("/etc/brigate/brigate.conf"));
} catch(FileNotFoundException ex) {
System.out.println("Missing config file");
}catch(Exception eta) {
eta.printStackTrace();
}
}
public String getDbUser() {
String user = this.configFile.getProperty("db_user", "brigate");
return user;
}
public String getDbPass() {
String pass = this.configFile.getProperty("db_password", "password");
return pass;
}
public String getDbHost() {
String host = this.configFile.getProperty("db_url", "db");
return host;
}
public String getDbPort() {
String port = this.configFile.getProperty("db_port", "3306");
return port;
}
}