Question

I have Netbean IDE 7.3.1 in windows XP with DataBase MariaDB. But not connecting... this class code ConexionDB.java:

import java.sql.Connection;
import java.sql.DriverManager;
import javax.naming.NamingException;

public class ConexionDB {
    private String enlace;
    private String controlador;
    private String error;
    private String usuario;
    private String contrasenia;

    public ConexionDB(){
        this.enlace = "jdbc:mariadb://localhost:3306/test";
        this.controlador = "org.mariadb.jdbc.Driver";
        this.error = " ";
        this.usuario = "root";
        this.contrasenia = "mariadb";
    }

    public Connection abrirConexion() throws NamingException{
        try{
            Class.forName(getEnlace()).newInstance();
            return DriverManager.getConnection(getEnlace(), getUsuario(), getContrasenia());
        }catch (Exception e){
            setError(e.getMessage());
            System.out.println("error al conectar " + getError());
        }
        return null;
    }

    public void cerrarConexion(Connection salida){
        try{
            salida.close();
        }catch(Exception e){
            setError(e.getMessage());
            System.out.println("error al conectar " + getError());
        }

    }

And this code Main.Java

import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.NamingException;
import javax.swing.JOptionPane;

public class Main {
    public static void main(String[] args) throws NamingException, SQLException{
        ConexionDB instanciaDB = new ConexionDB();
        Connection actualDB;
        actualDB = instanciaDB.abrirConexion();

        if(actualDB != null)
            JOptionPane.showMessageDialog(null, "Conexion Realiazada Correctamete!");
    }
}

Always I've the same error over Output: error al conectar jdbc:mariadb://localhost:3306/test [error to connect jdbc:mariadb://localhost:3306/test] Any idea in this problem! Grettings.

Was it helpful?

Solution

I suggest you should try connecting to MariaDB using some client like DBVisualiser etc and same username and password. This confirms that DB is running and open for connection and credentials are correct. Once connected to DB make sure that you test database exists . If you can verify all of these to be ok then the problem is with the code.

I looked at you code and confused with the method named getEnlace(). I don't know what it is returning ? .

Class.forName(getEnlace()).newInstance();

should't it be like

Class.forName(getControlador()).newInstance();

assuming getControlador returns the name of the jdbc driver class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top