mirror of
https://gitlab.com/tutorial-java-rafa-munoz/tutorial-java-2024-25/tutorialjava2024-25.git
synced 2025-11-09 18:03:09 +01:00
feat(ch 9): first ex
This commit is contained in:
@@ -0,0 +1,22 @@
|
|||||||
|
package tutorialJava.capitulo9_AWT_SWING.ejemplos.ejemplo01_Fabricante.controlador;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
|
||||||
|
public class ConnectionManager {
|
||||||
|
|
||||||
|
private static Connection conn = null;
|
||||||
|
|
||||||
|
|
||||||
|
public static Connection getConnection() throws Exception {
|
||||||
|
if (conn == null) {
|
||||||
|
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||||
|
|
||||||
|
conn =
|
||||||
|
(Connection) DriverManager.getConnection (
|
||||||
|
"jdbc:mysql://localhost:3306/tutorialjavacoches?serverTimezone=UTC",
|
||||||
|
"root", "1234");
|
||||||
|
}
|
||||||
|
return conn;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,27 +9,10 @@ import tutorialJava.capitulo9_AWT_SWING.ejemplos.ejemplo01_Fabricante.modelo.Fab
|
|||||||
|
|
||||||
public class ControladorFabricante {
|
public class ControladorFabricante {
|
||||||
|
|
||||||
private Connection getConexion () throws Exception {
|
|
||||||
String driver = JDBCPropiedades.getProperty("JDBC_DRIVER_CLASS");
|
|
||||||
String user = JDBCPropiedades.getProperty("JDBC_USER");
|
|
||||||
String password = JDBCPropiedades.getProperty("JDBC_PASSWORD");
|
|
||||||
String host = JDBCPropiedades.getProperty("JDBC_HOST");
|
|
||||||
String schema = JDBCPropiedades.getProperty("JDBC_SCHEMA_NAME");
|
|
||||||
String properties = JDBCPropiedades.getProperty("JDBC_PROPERTIES");
|
|
||||||
|
|
||||||
Class.forName(driver);
|
|
||||||
|
|
||||||
Connection conexion = (Connection) DriverManager.getConnection (
|
|
||||||
"jdbc:mysql://" + host + "/" + schema + properties,
|
|
||||||
user, password);
|
|
||||||
|
|
||||||
return conexion;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public Fabricante getPrimerFabricante() {
|
public Fabricante getPrimerFabricante() {
|
||||||
try {
|
try {
|
||||||
Connection conn = getConexion();
|
Connection conn = ConnectionManager.getConnection();
|
||||||
|
|
||||||
Statement s = conn.createStatement();
|
Statement s = conn.createStatement();
|
||||||
ResultSet rs =
|
ResultSet rs =
|
||||||
|
|||||||
@@ -1,69 +0,0 @@
|
|||||||
package tutorialJava.capitulo9_AWT_SWING.ejemplos.ejemplo01_Fabricante.controlador;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.FileReader;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.Properties;
|
|
||||||
|
|
||||||
public class JDBCPropiedades {
|
|
||||||
|
|
||||||
private static Properties propiedades = null;
|
|
||||||
|
|
||||||
public JDBCPropiedades () {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
private static Properties getPropiedades() {
|
|
||||||
if (propiedades == null) {
|
|
||||||
propiedades = new Properties();
|
|
||||||
|
|
||||||
try {
|
|
||||||
File file = new File("./src/tutorialJava/capitulo9_AWT_SWING/ejemplos/ejemplo01_Fabricante/controlador/jdbc.properties");
|
|
||||||
propiedades.load(new FileReader(file));
|
|
||||||
} catch (FileNotFoundException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (IOException e) {
|
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
return propiedades;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @param nombrePropiedad
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public static String getProperty(String nombrePropiedad) {
|
|
||||||
return getPropiedades().getProperty(nombrePropiedad);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @param nombrePropiedad
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public static int getIntProperty (String nombrePropiedad) {
|
|
||||||
return Integer.parseInt(getPropiedades().getProperty(nombrePropiedad));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @param nombrePropiedad
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public static Float getFloatProperty (String nombrePropiedad) {
|
|
||||||
return Float.parseFloat(getPropiedades().getProperty(nombrePropiedad));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
# par<61>metros de conexi<78>n a BBDD
|
|
||||||
JDBC_DRIVER_CLASS=com.mysql.cj.jdbc.Driver
|
|
||||||
JDBC_USER=root
|
|
||||||
JDBC_PASSWORD=1234
|
|
||||||
JDBC_HOST=localhost:3306
|
|
||||||
JDBC_SCHEMA_NAME=tutorialjavacoches
|
|
||||||
JDBC_PROPERTIES=?autoReconnect=true&serverTimezone=Europe/Madrid&useSSL=False&allowPublicKeyRetrieval=TRUE
|
|
||||||
Reference in New Issue
Block a user