From 5aeedf7adc4451fab3132bddbcf21db1925aa414 Mon Sep 17 00:00:00 2001 From: Blallo Date: Mon, 27 Apr 2020 01:04:36 +0200 Subject: [PATCH] Use configs and mysql as default connector --- src/dao/ContattoDao.java | 2 +- src/dao/MySqlConnection.java | 13 +++++++----- src/util/Config.java | 41 ++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 src/util/Config.java diff --git a/src/dao/ContattoDao.java b/src/dao/ContattoDao.java index b231596..dcaa218 100644 --- a/src/dao/ContattoDao.java +++ b/src/dao/ContattoDao.java @@ -12,7 +12,7 @@ import bean.Contatto; public class ContattoDao implements ContattoDaoI { - OracleConnection c = new OracleConnection(); + MySqlConnection c = new MySqlConnection(); @Override public boolean create(String nome, String cognome, String servizio_sociale, String area, String indirizzo, diff --git a/src/dao/MySqlConnection.java b/src/dao/MySqlConnection.java index e15d5fe..71a8247 100644 --- a/src/dao/MySqlConnection.java +++ b/src/dao/MySqlConnection.java @@ -4,8 +4,11 @@ package dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; +import util.Config; public class MySqlConnection { + + Config cfg = new Config(); public static void main(String[] args) { MySqlConnection c = new MySqlConnection(); @@ -17,10 +20,10 @@ public class MySqlConnection { public Connection open() { - String nomeDriver = "com.mysql.driverDriver"; - String utente = "brigate"; //nome utente - String password = "password"; //password - String url = "jdbc:oracle:thin:@localhost:1521:orcl"; + String nomeDriver = "com.mysql.jdbc.Driver"; + String utente = cfg.getDbUser(); //nome utente + String password = cfg.getDbPass(); //password + String url = "jdbc:mysql://" + cfg.getDbHost() + ":" + cfg.getDbPort() + "/brigate?autoReconnect=true&useSSL=false"; try { Class.forName(nomeDriver); @@ -42,4 +45,4 @@ public class MySqlConnection { } } -} \ No newline at end of file +} diff --git a/src/util/Config.java b/src/util/Config.java new file mode 100644 index 0000000..82b9eb0 --- /dev/null +++ b/src/util/Config.java @@ -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; + } +}