primero
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
package es.madrid.red.metro.anp.conexion;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import es.madrid.red.metro.anp.exception.RedMetroException;
|
||||
|
||||
public class ConexionDB {
|
||||
|
||||
private ConexionDB() {}
|
||||
|
||||
public static Connection getConexion() throws RedMetroException {
|
||||
try {
|
||||
Class.forName(ConfigConexion.get("db.driver"));
|
||||
return DriverManager.getConnection(
|
||||
ConfigConexion.get("db.url"),
|
||||
ConfigConexion.get("db.user"),
|
||||
ConfigConexion.get("db.password")
|
||||
);
|
||||
} catch (ClassNotFoundException | SQLException e) {
|
||||
throw new RedMetroException("Error al conectar con la base de datos", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package es.madrid.red.metro.anp.conexion;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Properties;
|
||||
|
||||
import es.madrid.red.metro.anp.exception.RedMetroException;
|
||||
|
||||
public class ConfigConexion {
|
||||
|
||||
private static final String RUTA_CONFIG = "ficheros/config.properties";
|
||||
private static final Properties PROPS = new Properties();
|
||||
|
||||
static {
|
||||
try (FileInputStream fis = new FileInputStream(RUTA_CONFIG)) {
|
||||
PROPS.load(fis);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException("No se pudo leer" + RUTA_CONFIG, e);
|
||||
}
|
||||
}
|
||||
|
||||
private ConfigConexion() {}
|
||||
public static String get(String clave) throws RedMetroException {
|
||||
String valor = PROPS.getProperty(clave);
|
||||
if (valor == null) {
|
||||
throw new RedMetroException("Falta la propiedad: " + clave);
|
||||
}
|
||||
|
||||
return valor;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user