Advertisement
LaCaraDeLaVerga

Untitled

Sep 15th, 2020
1,306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.13 KB | None | 0 0
  1. package persistencia.conexion;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6. import org.apache.log4j.Logger;
  7.  
  8. public class Conexion
  9. {
  10.     public static Conexion instancia;
  11.     private Connection connection;
  12.     private Logger log = Logger.getLogger(Conexion.class); 
  13.    
  14.     private Conexion()
  15.     {
  16.         try
  17.         {
  18.             Class.forName("com.mysql.jdbc.Driver"); // comentario prueba branch no borrado
  19.             this.connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/agenda","root","root");
  20.             this.connection.setAutoCommit(false);
  21.             log.info("Conexión exitosa");
  22.         }
  23.         catch(Exception e)
  24.         {
  25.             log.error("Conexión fallida", e);
  26.         }
  27.     }
  28.    
  29.    
  30.     public static Conexion getConexion()  
  31.     {                              
  32.         if(instancia == null)
  33.         {
  34.             instancia = new Conexion();
  35.         }
  36.         return instancia;
  37.     }
  38.  
  39.     public Connection getSQLConexion()
  40.     {
  41.         return this.connection;
  42.     }
  43.    
  44.     public void cerrarConexion()
  45.     {
  46.         try
  47.         {
  48.             this.connection.close();
  49.             log.info("Conexion cerrada");
  50.         }
  51.         catch (SQLException e)
  52.         {
  53.             log.error("Error al cerrar la conexión!", e);
  54.         }
  55.         instancia = null;
  56.     }
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement