如何从Java访问MS-Access数据库 [英] How to Access MS-Access Databases from Java

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

问题描述

如果我尝试运行下面的代码,我得到错误为java.sql.SQLException:[Microsoft] [ODBC驱动程序管理器]未找到数据源名称,并且没有指定默认驱动程序。

我是错误的,必须做什么修正?有人可以纠正我吗。

If i try to run below code, im getting error as " java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified".
Y am i getting dis error, What correction has to be done? Can some one correct me please.

public class DbAccess {

    public static void main(String args[])throws SQLException
    {
        try{
            Connection con = null;
            Statement st = null;
            ResultSet rs = null;
            String query = "Select * from employee";
            
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String accessFileName ="D:/MS-A/NewDatabase";
            String database = "jdbc:odbc:Driver={Microsoft Access Driver(*.mdb)};DBQ="+accessFileName+".mdb;";
            con=DriverManager.getConnection(database,"username","password");
            st=con.createStatement();
            rs= st.executeQuery(query);
            while((rs!=null) && (rs.next()))
            {
                System.out.println(rs.getString(1) + " : " + rs.getString(2));
            }
            st.close();
            con.close();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
}

推荐答案

Hello Shruthi,



尝试安装访问数据库引擎组件(可用此处 [ ^ ])首先。它将在机器上安装必要的驱动程序,然后你的应用程序应该能够连接到数据库。



另外你也可以尝试可以从Easysoft获得的MS Access JDBC驱动程序 [ ^ ],虽然我从未尝试过这些。



问候,
Hello Shrut

Try installing access database engine component (available here[^]) first. It will install the necessary drivers on the machine and then your app should be able to connect to the database.

Alternatively you can also try MS Access JDBC Driver available from Easysoft[^], though I have never tried those.

Regards,


从我发现的例子和本文访问MS-Access数据库Java [ ^ ]看起来你需要在你的连接字符串中添加一个空格。

目前是:
From the examples I found and this article Access MS-Access Databases from Java[^] it looks like you need to add a space in your connection string.
Currently is is:
"jdbc:odbc:Driver={Microsoft Access Driver(*.mdb)};DBQ="+accessFileName+".mdb;"

看起来它需要

"jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="+accessFileName+".mdb;"



基本上你需要检查ODBC中的驱动程序名称,并在代码中使用完全相同的


这篇关于如何从Java访问MS-Access数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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