gpt4 book ai didi

java - 使用 JDBC 连接数据库

转载 作者:行者123 更新时间:2023-12-02 05:53:05 25 4
gpt4 key购买 nike

我需要使用 java(javaFX 应用程序)连接并访问数据库。当我运行该应用程序时, Controller 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);
}

}

}

异常(exception)情况是 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)

最佳答案

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

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

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

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

关于java - 使用 JDBC 连接数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23360122/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com