在 java 中与 MS Access 数据库建立 SQL 连接时找不到合适的驱动程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23255766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 21:32:49  来源:igfitidea点击:

No suitable driver found when making SQL connection to MS Access Database in java

javasqlswingnetbeansjdbc

提问by steinbitur

I have a Jbutton (GetDataFromDB) in a simple java application that is suppose to load the data from the database depicted in the path in the code below into a Jtable in the application.

我在一个简单的 java 应用程序中有一个 Jbutton (GetDataFromDB),它假设将下面代码中路径中描述的数据库中的数据加载到应用程序中的 Jtable 中。

Edited answer into code:

将答案编辑为代码:

private void GetDataFromDBActionPerformed(java.awt.event.ActionEvent evt) {                                              
    Connection con;
    ResultSet rs = null;
    Statement stmt;

    try {

    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    con = DriverManager.getConnection("jdbc:odbc:Driver={MS Access Driver (*.mdb, *.accdb)};Dbq=C:\Users\Bruger\Documents\Database11.accdb");

    stmt = con.createStatement();
    String query = null;
    query = "select * from cost";

    rs = stmt.executeQuery(query);

    i = 0;

    while (rs.next()){
        i = i + 1;
        jTable.getModel().setValueAt(rs.getString(1), i, 1);
        jTable.getModel().setValueAt(rs.getString(2), i, 2);
    }


    rs.close();
    stmt.close();
    con.close();
    } catch(Exception err){
    System.out.println(err.getMessage());
}

}  

When I press the button I get the following message in the run output window:

当我按下按钮时,我在运行输出窗口中收到以下消息:

No suitable driver found for jdbc:odbc:Driver={Microsoft Access Driver (.mdb, .accdb)};Dbq=C:\Users\Bruger\Documents\Database11.accdb

没有找到适合 jdbc 的驱动程序:odbc:Driver={Microsoft Access Driver (.mdb, .accdb)};Dbq=C:\Users\Bruger\Documents\Database11.accdb

I have at the top of my code the import:

我在代码的顶部有导入:

import java.sql.*;

I have also tried changing from "Microsoft Access Driver" to "MS Access Driver" but I get the same message in the run output window i.e.

我也尝试从“Microsoft Access Driver”更改为“MS Access Driver”,但在运行输出窗口中收到相同的消息,即

No suitable driver found for jdbc:odbc:Driver={MS Access Driver (.mdb, .accdb)};Dbq=C:\Users\Bruger\Documents\Database11.accdb

没有找到适合 jdbc 的驱动程序:odbc:Driver={MS Access Driver (.mdb, .accdb)};Dbq=C:\Users\Bruger\Documents\Database11.accdb

I'm really thankful for all your help, input and feedback.

我真的很感谢您的所有帮助、意见和反馈。

采纳答案by GoldenJam

Depending on the driver and If you are pre JDK 6**!

取决于驱动程序,如果您是 JDK 6 之前的版本**!

You need to register the driver.

您需要注册驱动程序。

Try adding:

尝试添加:

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 

So:

所以:

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
con = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=C:\Users\Bruger\Documents\Database11.accdb");

It's also worth mentioning you don't need to do this every time you get a connection, just once to make sure the class is loaded.

还值得一提的是,您无需在每次获得连接时都执行此操作,只需执行一次即可确保类已加载。

There are a lot of stackoverflow questions relating to this, but the reason for it is below:

有很多与此相关的stackoverflow问题,但原因如下:

Source - From The Java Tutorial:

来源 - 来自 Java 教程:

In previous versions of JDBC, to obtain a connection, you first had to initialize your JDBC driver by calling the method Class.forName. This methods required an object of type java.sql.Driver. Each JDBC driver contains one or more classes that implements the interface java.sql.Driver. ... Any JDBC 4.0 drivers that are found in your class path are automatically loaded. (However, you must manually load any drivers prior to JDBC 4.0 with the method Class.forName.)

在以前的 JDBC 版本中,要获得连接,首先必须通过调用 Class.forName 方法来初始化 JDBC 驱动程序。此方法需要一个 java.sql.Driver 类型的对象。每个 JDBC 驱动程序都包含一个或多个实现接口 java.sql.Driver 的类。...在您的类路径中找到的任何 JDBC 4.0 驱动程序都会自动加载。(但是,您必须使用 Class.forName 方法手动加载 JDBC 4.0 之前的任何驱动程序。)



On a related and very important note.

关于一个相关且非常重要的说明。

I would also recommend looking up how to handle connections. External Resources like database connections and cursors are easy to leak if you are not familiar with them. Look up 'try finally blocks', or more recently in java 7+ 'try-with-resources'.

我还建议查找如何处理连接。如果您不熟悉数据库连接和游标等外部资源,则很容易泄漏。查找“try finally blocks”,或者最近在 java 7+ 的“try-with-resources”中查找。