使用JDBC连接到数据库

时间:2014-04-29 09:01:27

标签: java jdbc

我需要使用java(javaFX应用程序)连接和访问数据库。当我运行应用程序时,我在控制器sun.jdbc.odbc.JdbcOdbcDriver中出错。我搜索了很多,但我没有找到一个很好的解决方案。我使用jdk 1.8,我使用这个类:

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);
    }

}

}

例外是在      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)

1 个答案:

答案 0 :(得分:2)

正如评论所说,似乎旧的JDBC / ODBC驱动程序已被弃用,并且自Java 8发布以来不再存在。看看声称是Access的纯Java JDBC驱动程序的这些替代实现:

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

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

第一个是商业实施,具有14天试用许可证,第二个是免费的。希望你觉得它很有用!