Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Conexion {
- private Connection con;
- private final String driver = "com.mysql.cj.jdbc.Driver";
- private final String usuario = "TestBD";
- private final String contrasena = "pass";
- private final String urlBaseDatos = "jdbc:mysql://localhost:3306/TestBD?serverTimezone=UTC";
- private static Conexion conexion;
- private Conexion(){}
- public static Conexion getInstance() throws SQLException{
- if(conexion == null){
- conexion = new Conexion();
- }
- conexion.conectar();
- return conexion;
- }
- private void conectar() throws SQLException{
- if (con == null || con.isClosed()) {
- try {
- Class.forName(driver);
- } catch (ClassNotFoundException ex) {
- Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
- }
- con = DriverManager.getConnection(urlBaseDatos, usuario, contrasena);
- }
- }
- public boolean existeConexion() throws SQLException{
- return con != null && !con.isClosed();
- }
- public Connection getCon() {
- return con;
- }
- }
- public class TestConexion {
- public static void main(String[] args){
- try {
- if(Conexion.getInstance().existeConexion()){
- System.out.println("Conexión Establecida");
- }
- } catch (SQLException ex) {
- Logger.getLogger(TestConexion.class.getName()).log(Level.SEVERE, null, ex);
- System.out.println("Error al Establecer la Conexión");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement