使用JDBC连接到数据库 [英] Connect to database using JDBC

查看:96
本文介绍了使用JDBC连接到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

}

}

例外在 Class.forName(this.controlador);

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)

推荐答案

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

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

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

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

这篇关于使用JDBC连接到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆