Control error when connecting to my MySQL database

1

I have the following code to make the connection to my MySQL database, what I want is to control the error that appears when I try to run the program, I miss the error / warning that the Xampp is off. I would like to know if there is any way to control it by, for example, a JOptionpane with a message that says: the data base is not started.

Code for the connection:

public class ConexionBD
{
    public Connection conexion=null;
    public Statement sentencia=null;        //Se usa para enviar sentencias SQL a la BD
    public ResultSet resultado=null;        //Contendrá los datos devueltos de una sentencia SQL

    public ConexionBD()
    {
        conectarBD();
    }
    public void conectarBD()
    {
        // Conectar con MySql
        String url = "jdbc:mysql://localhost/concesionario";

        // Cargar el driver y se genera una nueva instancia y crear conexión
        try 
        {               
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            conexion = DriverManager.getConnection(url, "root", "");
            // Crear sentencia
            sentencia = conexion.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
        } catch (Exception e) {e.printStackTrace();}
    }

    public void cerrarConexionBD()
    {
        try 
        {
            resultado.close();
            sentencia.close();
            conexion.close();
        } catch (SQLException ex) {ex.printStackTrace();}                  
    }
}

Error message:

  

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:   Communications link failure

     

The last packet sent successfully to the server was 0 milliseconds   Aug. The driver has not received any packets from the server. at   sun.reflect.NativeConstructorAccessorImpl.newInstance0 (Native Method)     at sun.reflect.NativeConstructorAccessorImpl.newInstance (Unknown   Source) at   sun.reflect.DelegatingConstructorAccessorImpl.newInstance (Unknown   Source) at java.lang.reflect.Constructor.newInstance (Unknown Source)     at com.mysql.jdbc.Util.handleNewInstance (Util.java:406) at   com.mysql.jdbc.SQLError.createCommunicationsException (SQLError.java:1074)     at   com.mysql.jdbc.ConnectionImpl.createNewIO (ConnectionImpl.java:2214)     at com.mysql.jdbc.ConnectionImpl. (ConnectionImpl.java:781) at   com.mysql.jdbc.JDBC4Connection. (JDBC4Connection.java:46) at   sun.reflect.NativeConstructorAccessorImpl.newInstance0 (Native Method)     at sun.reflect.NativeConstructorAccessorImpl.newInstance (Unknown   Source) at   sun.reflect.DelegatingConstructorAccessorImpl.newInstance (Unknown   Source) at java.lang.reflect.Constructor.newInstance (Unknown Source)     at com.mysql.jdbc.Util.handleNewInstance (Util.java:406) at   com.mysql.jdbc.ConnectionImpl.getInstance (ConnectionImpl.java:352) at   com.mysql.jdbc.NonRegisteringDriver.connect (NonRegisteringDriver.java:284)     at java.sql.DriverManager.getConnection (Unknown Source) at   java.sql.DriverManager.getConnection (Unknown Source) at   bbdd.ConexionBD.conectarBD (ConexionBD.java:28) at   bbdd.ConexionBD. (ConexionBD.java:17) at   Customers.CustomerForm. (ClientForm.java:58) at   panelconcesionario.Concesionario. (Concesionario.java:16) at   main.Main.main (Main.java:8) Caused by:   com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:   Communications link failure

    
asked by Mario Guiber 22.10.2017 в 13:23
source

1 answer

1

at the end of the first catch of the ConnectarBD method, you put another catch with the exception it gives you and the JOptionPane within the catch.

    try{               
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        conexion = DriverManager.getConnection(url, "root", "");
        // Crear sentencia
        sentencia = conexion.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
        } 
          catch (Exception e) {e.printStackTrace();}
          catch (ExcepcionQueQuieresControlar ex) { JOptionPane.... ; }
    
answered by 22.10.2017 / 14:29
source