Connection java and mysql

0

Any questions on how to solve the error? that appears when I try to make a graph in Jfreechart. apparently the error is in the connector.

import java.sql.*;
import javax.swing.*;

public class javaconnect {


     Connection conn = null;
    public static Connection ConnecrDb(){
       try{
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/prueba1","root","marbella2011");
        }
        catch (Exception e){
            System.out.println(e);
        }

        return ConnecrDb();
    }


}

The error that comes to me is as follows.

  

.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: ""   at java.sql.DriverManager.getConnection (DriverManager.java:571)

    
asked by Grindor 04.08.2016 в 07:24
source

2 answers

1

According to the explanation made in similar questions on the site in English:

The problem seems to be that your database has too many open connections, so it can no longer open more connections. You can check the connections made to your MySQL database using the SHOW PROCESSLIST command, you can eliminate those connections using the KILL <processid> command, the <processid> you get as a result of the command to see the connections.

Now, this problem may reappear because you are not closing the connections in your application properly or because your database is configured to have a very limited number of connections (I very much doubt this unless you have been playing with the settings of my.ini or my.cfg since the default value in MySQL is 150). I recommend you check your application and the places where you use a database connection so that you can properly close it by calling the Connection#close ALWAYS method (general advice for the readers of this answer).

In your specific case, this is because your method is recursive and infinite, noticed by this pair of lines:

public static Connection ConnecrDb() {
    //implementación no necesaria para evaluar que es recursivo infinito
    return ConnecrDb(); //retorna el resultado de llamarse a sí mismo
        //lo que resulta en una recursividad infinita puesto que
        //no hay restricciones para evitar este resultado
}

Your code should be written as follows:

public static Connection ConnecrDb(){
    Connection conn = null;
    try {
        //desde Java 7 y JDBC 4 no necesitas llamar a esta línea
        //para abrir una conexión a la base de datos
        //Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/prueba1","root","marbella2011");
    }
    catch (Exception e) {
        //es mejor tener todo el stacktrace del error
        //no solo el mensaje
        //System.out.println(e);
        e.printStackTrace(System.out);
    }
    //return ConnecrDb();
    return conn;
}

Likewise, from the above, remember ALWAYS to close your connections to the database, either by calling the Connection#close method manually or by using try-with-resources available from Java 7.

    
answered by 05.08.2016 в 17:50
0

1.- Create a class of type Java Inteface in the following way:

Since we have created the IConexion class we must put the following code:

import java.sql.Connection;

public interface IConexion {
public Connection getConex();
public Connection establecerDB();
public void getClose();

}

After we have created the interface we continue to the class connection the class that we are going to call ecConexionJRAVBSingleton this class we created it in a normal way and we continue to do the following:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import mx.com.JRAlejandroV.IMetodos.IConexion;

public class ecConexionJRAVBSingleton implements IConexion{

private Connection con = null;
private static ecConexionJRAVBSingleton instancia;

private ecConexionJRAVBSingleton(){

}

public static ecConexionJRAVBSingleton obtenerInstancia(){
    if(instancia == null){
        instancia = new ecConexionJRAVBSingleton();
        System.out.println("Exito en conexion singleton");
    }
    return instancia;
}

@Override
public Connection getConex() {
    return establecerDB();
}

@Override
public Connection establecerDB() {
    try{
        Class.forName("com.mysql.jdbc.Driver");
        String user = "root";
        String url = "jdbc:mysql://localhost:3306/grupoautofinprueba";
        String password = "";
        con = DriverManager.getConnection(url, user, password);
    }catch(ClassNotFoundException ex){
        JOptionPane.showMessageDialog(null,"Driver no encontrado");
    }catch(SQLException ex){
        JOptionPane.showMessageDialog(null,"Fallo al recibir base de datos");
    }catch(Exception ex){
        JOptionPane.showMessageDialog(null, "No hay resultado");
    }finally{
        return con;
    }
}

@Override
public void getClose() {
    try{
        con.close();
    }catch(SQLException ex){

    }
}

}

With this we can give a successful connection to mysql from java

    
answered by 08.08.2016 в 01:48