如何使用Java程序连接MS Access数据库? [英] How to connect MS Access Database using Java program?

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

问题描述

我想编写一个程序来从MS Access数据库检索数据.我把程序写成波纹管:

I want to write a program to retrieve data from MS Access database. I wrote program as bellow:

package db;

import java.sql.*;

public class MSaccess_archive {
    public static void main(String[] args) {

        try {

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

            String accessFileName = "E:/L4_project/sample/db/Database";

            String connURL="jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ="+accessFileName+".accdb;";

            Connection con = DriverManager.getConnection(connURL, "","");

            Statement stmt = con.createStatement();

            stmt.execute("select * from student"); // execute query in table student

            ResultSet rs = stmt.getResultSet(); // get any Result that came from our query

            if (rs != null)
             while ( rs.next() ){

                System.out.println("Name: " + rs.getString("Name") + " ID: "+rs.getString("ID"));
                }

                stmt.close();
                con.close();
            }
            catch (Exception err) {
                System.out.println("ERROR: " + err);
            }
    }

}

但是我得到了如下异常:

But I got Exception as below:

错误:java.sql.SQLException:[Microsoft] [ODBC Microsoft Access 驱动程序]找不到文件(未知)".

ERROR: java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Could not find file '(unknown)'.

当我使用.mdb文件时,上面的程序可以正常工作,但是如果我使用.accdb文件,则会出现上面的异常.

When I used a .mdb file the above program works correctly but if I use a .accdb file it gives the above exception.

有什么想法吗?

推荐答案

已将JDBC-ODBC桥从Java 8中删除,并且不受支持(参考:此处). UCanAccess 是一种流行的替代方法.有关详细信息,请参见

The JDBC-ODBC Bridge has been removed from Java 8 and is not supported (ref: here and here). UCanAccess is a popular alternative. For details, see

在不使用ODBC的情况下从Java操纵Access数据库

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

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