How to make a connection to MySql?

0

I'm doing an application that inserts an excel sheet to a table in MySQL, the application is inserting the data well, but the error is that after inserting 149 rows I get this error "An error has been generated when setting the link to the DB.From type: com.mysql.jdbc.exceptionsjdbc4.MySQLNonTransientConnectionException: could not create connection to database server Attempted reconnect 3 times.Giving up "
Could you help me solve that problem? This is the code of my connection:

 public class ConexionMySQL {

String db = "palp";
String url = "jdbc:mysql://localhost/"+ db+"?autoReconnect=true&useSSL=false";
String user = "root";
String pass = "123456";

public Connection Conectar(){
    Connection link = null;
    try{
        //Cargar el DRIVER de conexion con el
        //SGDB Mysql
        Class.forName("com.mysql.jdbc.Driver");
        //Creacion de un enlace hacia la base de datos
        link = DriverManager.getConnection(this.url,this.user, this.pass);
    }catch (Exception ex){
        JOptionPane.showMessageDialog(null, "Se ha generado un"
                + "error al establecer el enlace a la BD."
                + "De tipo: " + ex);
    }
    return link;
}

}

    
asked by Daniel Vasquez 26.04.2017 в 17:56
source

1 answer

0

You need the mysql library, I use mysql-connect-java-3.1.12

public class Conexion{
    String driver = "com.mysql.jdbc.Driver";
    String url = "jdbc:mysql://localhost:3306/TU_BASE_DE_DATOS";
    String user = "TU_USUARIO";
    String pass = "TU_CLAVE";

    public Connection getConexion(){
    Connection con = null;
    try {
        Class.forName(driver);
        con = DriverManager.getConnection(url, user, pass);
        return con;
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "Error al conectarse a la BD: "+e.getMessage());
    }
    return null;
}


}
    
answered by 26.04.2017 в 19:36