0

I need to connect and access a database with java (javaFX application). When I run the application, I have an error in the controller sun.jdbc.odbc.JdbcOdbcDriver. I searched a lot but I didnt find a good solution. I use jdk 1.8 and I use this class:

public class conexionBD {

private Connection conexion;
private Statement sentencia;

private String controlador;
private String nombre_bd;
private String usuarioBD;
private String passwordBD;


public conexionBD(){
    this.controlador="sun.jdbc.odbc.JdbcOdbcDriver"; 
    this.nombre_bd="C:\\Users\\SANDRA\\Documents\\Access_BD\\Contactos.mdb";
    this.usuarioBD="";
    this.passwordBD="";

}

public void EstablecerConexion(){

    try{
        Class.forName (this.controlador);
    }catch (ClassNotFoundException e){
        JOptionPane.showMessageDialog(null, "Error al cambiar el controlador");
        e.printStackTrace();
    }

    try{
        String DSN="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="+this.nombre_bd;
        conexion=DriverManager.getConnection(DSN,this.usuarioBD,this.passwordBD); 
    }catch (SQLException e){
         JOptionPane.showMessageDialog(null,"Error al realizar la conexion "+e);
    }

    try { 
        this.sentencia=this.conexion.createStatement( 
        ResultSet.TYPE_SCROLL_INSENSITIVE, 
                ResultSet.CONCUR_READ_ONLY); 
    } 
    catch (Exception e) { 
        JOptionPane.showMessageDialog(null,"Error al crear el objeto sentencia "+e);
    }

}

}

The exception is in Class.forName (this.controlador);

java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:259)
at net.facturacion.controller.conexionBD.EstablecerConexion(conexionBD.java:41)
at net.facturacion.view.PantallaPrincipalController.initialize(PantallaPrincipalController.java:34)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3230)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3191)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3164)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3140)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3120)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3113)
at net.facturacion.gestor.principalMain.start(principalMain.java:24)
at com.sun.javafx.application.LauncherImpl$8.run(LauncherImpl.java:837)
at com.sun.javafx.application.PlatformImpl$7.run(PlatformImpl.java:335)
at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:301)
at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:298)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$6.run(PlatformImpl.java:298)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$300(WinApplication.java:39)
at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112)
at java.lang.Thread.run(Thread.java:745)
James_D
  • 201,275
  • 16
  • 291
  • 322
sandrita
  • 363
  • 1
  • 6
  • 17
  • 1
    Java 8 no longer provides access to ODBC. You need to find another JDBC driver that allows either access to an access database, or that provides access to ODBC. – Mark Rotteveel Apr 29 '14 at 09:04
  • 1
    officiel oracle blog statement: https://blogs.oracle.com/Lance/entry/removal_of_the_jdbc_odbc – user432 Apr 29 '14 at 09:05

1 Answers1

2

As comments say, it seems that the old JDBC/ODBC driver is deprecated and does not exist any more since Java 8 was released. Have a look at these alternative implementations that claim to be pure java JDBC drivers for Access:

http://www.easysoft.com/applications/microsoft-access/jdbc-odbc.html

http://ucanaccess.sourceforge.net/site.html

The first one is a commercial implementation with a 14-day trial license and the second one is free. Hope you find it useful!

Jorge_B
  • 9,712
  • 2
  • 17
  • 22