Access database connection with JAVA

1

Greetings I do not know why the connection does not run, it will be for the jackcess that will be obsolete or for the version of my jdk 1.8? because you already add JAR UCanAccess 4.0.4 to libraries through netbeans

package Frames;
import java.sql.*;
import javax.swing.*;
import java.sql.DriverManager;
/**
 *
* @author 
*/


// creando la clase conexion para conectar a la base de datos en Access
public class Conexion {
static Connection conn = null;
static String driver = "net.ucanaccess.jdbc.UcanaccessDriver";


static String url = "jdbc:ucanaccess://C:\xxxx\xxx.accdb";

public static Connection ejecutarConexion(){
    try{
        if(conn==null){
            Class.forName(driver);
            conn = DriverManager.getConnection(url);
            JOptionPane.showMessageDialog(null, "Conectado");

        }
    } catch (Exception ex){
        ex.printStackTrace();
        conn = null;
    }
    return conn;
}


public static void main(String [] args){
    Connection cn= Conexion.ejecutarConexion();
}
}

The error is as follows:

    
asked by Esme 20.04.2018 в 21:50
source

1 answer

1

The error is in the driver you are using this:

static String driver = "net.ucanaccess.jdbc.UcanaccessDriver";

Try this one with me it worked for me

this.controlador="sun.jdbc.odbc.JdbcOdbcDriver";

And you need createStatement but I'll give you this example of my connection class so you can get an idea of the parts you missed

public class connectBD {


private Connection conexion;
private Statement sentencia;
private final String controlador;
private final String nombre_bd;
private final String usuarioBD;
private final String passwordBD;





public connectBD(){

    this.controlador="sun.jdbc.odbc.JdbcOdbcDriver";

    this.nombre_bd="C:\Users\Raul\Documents\NetBeansProjects\BD_Access\Prueba_Java.accdb";

    this.usuarioBD="";

    this.passwordBD="";
}



public boolean EstablecerConexion() throws SQLException{
    try{
        conexion=DriverManager.getConnection("jdbc:ucanaccess://"+this.nombre_bd,this.usuarioBD,this.passwordBD);
    }catch (SQLException e){
         JOptionPane.showMessageDialog(null,"Error al realizar la conexion "+e);
         return false;
    }



    try {
        this.sentencia=this.conexion.createStatement(
        ResultSet.TYPE_SCROLL_INSENSITIVE,
        ResultSet.CONCUR_READ_ONLY);
    }

    catch (SQLException e) {
        JOptionPane.showMessageDialog(null,"Error al crear el objeto sentencia "+e);
        return false;

    }

    return true;
}



public ResultSet EjecutarSentencia( String sql) throws SQLException{
    ResultSet rs;
    rs = this.sentencia.executeQuery(sql);
    return rs;
}

}
    
answered by 20.04.2018 / 22:13
source