在Java中建立与MS Access数据库的SQL连接时找不到合适的驱动程序 [英] No suitable driver found when making SQL connection to MS Access Database in java

查看:165
本文介绍了在Java中建立与MS Access数据库的SQL连接时找不到合适的驱动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

将答案编辑为代码:

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:

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

我的代码顶部有导入:

import java.sql.*;

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

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.

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

非常感谢您的帮助,投入和反馈.

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

推荐答案

取决于驱动程序以及JDK 6之前的版本**!

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

您需要注册驱动程序.

尝试添加:

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

所以:

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.

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

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

来源-来自Java教程:

Source - From The Java Tutorial:

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

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.)


有关且非常重要的说明.


On a related and very important note.

我还建议您查找如何处理连接.如果您不熟悉外部资源,例如数据库连接和游标,则很容易泄漏它们.在Java 7+"try-with-resources"中查找"try try blocks",或者最近查找.

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'.

这篇关于在Java中建立与MS Access数据库的SQL连接时找不到合适的驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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