From 1b377734dd12fc4e5d8276914800e0b421b9df18 Mon Sep 17 00:00:00 2001 From: David Date: Tue, 7 May 2024 18:02:41 -0600 Subject: [PATCH] =?UTF-8?q?Configuraci=C3=B3n=20conexi=C3=B3n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/mx/uv/Conexion.java | 46 +++++++++++++------ 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/healthmanager_back/src/main/java/mx/uv/Conexion.java b/healthmanager_back/src/main/java/mx/uv/Conexion.java index b03ca2b..e04a0ec 100644 --- a/healthmanager_back/src/main/java/mx/uv/Conexion.java +++ b/healthmanager_back/src/main/java/mx/uv/Conexion.java @@ -1,26 +1,42 @@ package mx.uv; +import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; -import java.sql.Connection; +import java.util.logging.Level; +import java.util.logging.Logger; public class Conexion { - public static String url="mysql://root:tdPXEKculfAIumCBQirpMvZYXnrOuCbZ@monorail.proxy.rlwy.net:51101/railway"; - public static String Drivername="com.mysql.cj.jdbc.Driver"; - public static String username="root"; - public static String password="tdPXEKculfAIumCBQirpMvZYXnrOuCbZ"; - private static Connection conexion=null; + private Connection conexion; + String driver = "com.mysql.cj.jdbc.Driver"; + private String url = "jdbc:mysql://root:tdPXEKculfAIumCBQirpMvZYXnrOuCbZ@monorail.proxy.rlwy.net:51101/railway"; + private final String USUARIO="root"; + private final String CONTRASENA="tdPXEKculfAIumCBQirpMvZYXnrOuCbZ"; - public static Connection getConnection(){ - try { - Class.forName(Drivername); - conexion = DriverManager.getConnection(url, username, password); - } catch (SQLException e) { - System.out.println(" SQL:" + e); - } catch (ClassNotFoundException e){ - System.out.println("Driver:" + e); - } + public Connection obtenerConexion() throws SQLException{ + conecta(); return conexion; } + + private void conecta() throws SQLException{ + try { + Class.forName(driver); + } catch (ClassNotFoundException ex) { + Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex); + } + conexion=DriverManager.getConnection(url,USUARIO,CONTRASENA); + } + + public void cerrarConexion(){ + if(conexion!=null){ + try { + if(!conexion.isClosed()){ + conexion.close(); + } + } catch (SQLException ex) { + Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex); + } + } + } }