简体   繁体   English

JDBC驱动程序MS Access连接

[英]JDBC driver MS Access connection

I want connect my MS access file with Java GUI program,but I have problem with connection.... 我想用Java GUI程序连接我的MS访问文件,但我有连接问题....

I have Windows 7 64b, and ms office 2007. When I opened the ODBC driver manager in the control panel I havent found any driver for Microsoft Access (maybe when I started the ODBC is started running the 64bit ODBC, now I think is running the 32bit ODBC. I read this and I make it : "jdbc-odbc connection for window 7 64 bit machine.. 1 . Right click Data source (ODBC)..go to properties change the folloing thing 我有Windows 7 64b和ms office 2007.当我在控制面板中打开ODBC驱动程序管理器时,我没有找到任何Microsoft Access驱动程序(也许当我启动ODBC时开始运行64位ODBC,现在我认为正在运行32位ODBC。我读了这个,我做了:“jdbc-odbc连接窗口7 64位机器.. 1.右键单击数据源(ODBC)..转到属性更改下面的事情

target [ %SystemRoot%\\SysWOW64\\odbcad32.exe ] start in : [ %SystemRoot%\\System32 ] 目标[%SystemRoot%\\ SysWOW64 \\ odbcad32.exe]开始于:[%SystemRoot%\\ System32]

press enter and continue as admin source: source link " ) Now when I start in conctrol pannel the ODBC I can see the driver screenshoot 按回车键继续作为管理员源: 源链接 “)现在,当我在conctrol pannel中启动ODBC时,我可以看到驱动程序的屏幕截图

My program code(I tried two ways but I have same error): 我的程序代码(我试过两种方法,但我有同样的错误):

        public void Connect() {
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

 //    String DatabaseFile = "D:java/Invertory.mdb";       
//            String DATABASE =
//                    "jdbc:odbc:Driver="
//                    + "{Microsoft Access Driver (*.mdb, *.accdb)};"
//                    + "DBQ=" + DatabaseFile;`enter code here`
 String DATABASE ="jdbc:odbc:Driver= Microsoft Access Driver (*.mdb, *.accdb);DBQ=Invertory.mdb";
           CONEX = DriverManager.getConnection(DATABASE);

        } catch (Exception X) {
          X.printStackTrace();
            //JOptionPane.showMessageDialog(null,e);
        }
    }

error 错误

java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified java.sql.SQLException:[Microsoft] [ODBC驱动程序管理器]未找到数据源名称且未指定默认驱动程序

Use UCanAccess JDBC Driver : 使用UCanAccess JDBC驱动程序:

Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
Connection conn=DriverManager.getConnection("jdbc:ucanaccess://<mdb or accdb file path>",user, password); 
for example: 
Connection conn=DriverManager.getConnection("jdbc:ucanaccess://c:/pippo.mdb");

So for your example it will be Connection conn=DriverManager.getConnection("jdbc:ucanaccess://"+path) 所以对于你的例子,它将是Connection conn=DriverManager.getConnection("jdbc:ucanaccess://"+path)

If you are using Windows 64-bit you probably need to go to this path 如果您使用的是Windows 64位,则可能需要转到此路径

C:/Windows/SysWOW64/odbcad32.exe C:/Windows/SysWOW64/odbcad32.exe

Then I noticed that you are using the direct path instead creating new System DSN , your direct path is correct till the path to the access file you must give the full path like this : 然后我注意到你正在使用直接路径而不是创建新的System DSN ,你的直接路径是正确的,直到访问文件的路径你必须给出如下的完整路径:

jdbc:odbc:Driver= Microsoft Access Driver (*.mdb, *.accdb);DBQ=path/to/Invertory.mdb" jdbc:odbc:Driver = Microsoft Access Driver(* .mdb,* .accdb); DBQ = path / to / Invertory.mdb“

To get the path you probably need to use java.io.File that have a method returns the abslute path to the file see the example : 要获取路径,您可能需要使用具有方法的java.io.File返回文件的绝对路径,请参阅示例:

import java.sql.*;
public class TestConnection {
    Connection con ;
    Statement st ;
    ResultSet rs ;
    String db;
    public TestConnection (){
        try{
            String path = new java.io.File("Invertory.mdb").getAbsolutePath();
        db ="JDBC:ODBC:Driver=Microsoft Access Driver (*.mdb, *.accdb); DBQ="+path;
            doConnection();
        } catch(NullPointerException ex){
                ex.printStackTrace();
            }

    }

    public void doConnection(){
        try{
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con = DriverManager.getConnection(db);
            st = con.createStatement();
            rs = st.executeQuery("select * from Invertory");
            while(rs.next()){
                System.out.println(rs.getObject(1));
            }
        }catch(SQLException | ClassNotFoundException ex){
            System.out.println(ex.toString());

        }

    }
    public static void main(String...argS){
        new TestConnection();
    }
}

I answered a similar question enter link description here a while back. 我回答了一个类似的问题, 在这里输入链接描述

Basically at that time: 基本上那个时候:

  1. You could connect to Ms-Access from 32 bit java through the JDBC-ODBC bridge 您可以通过JDBC-ODBC桥从32位Java连接到Ms-Access
  2. You could not connect to a 32 bit Odbc driver through the JDBC-ODBC from 64 bit java. 您无法通过64位Java的JDBC-ODBC连接到32位Odbc驱动程序。 There was a message telling you that you can only connect from a 32 bit programs 有消息告诉您,您只能从32位程序连接
  3. While Microsoft does provide a 64 bit Ms-Access driver, it did not work with Java's 64 bit JDBC-ODBC driver. 虽然Microsoft确实提供了64位Ms-Access驱动程序,但它无法与Java的64位JDBC-ODBC驱动程序一起使用。

Since then there seems to be a new open-source Ms-Access JDBC Driver Ms-Access JDBC driver . 从那时起,似乎有一个新的开源Ms-Access JDBC驱动程序Ms-Access JDBC驱动程序 I have no Idea how good it is. 我不知道它有多好。

You just missing something in your code right here : 您刚刚在代码中遗漏了一些内容:

db ="JDBC:ODBC:Driver=Microsoft Access Driver (*.mdb, *.accdb); DBQ="+path;

You need to add {} between Driver= and )=; 你需要在Driver=)=;之间添加{} )=; . Like this Below 像这样下面

db ="JDBC:ODBC:Driver={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ="+path;
final String fileName = "c:/myDataBase.mdb"
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="+fileName;
Connection con = DriverManager.getConnection(url,username,password);

The problem is that you should run on Java 32 bit try to install latest JDK and it will work 问题是你应该运行Java 32位尝试安装最新的JDK,它会工作

I run it using JDK version "jdk-7u67-windows-i586.exe" 我使用JDK版本“jdk-7u67-windows-i586.exe”运行它

On a 64 bit system, you should: 在64位系统上,您应该:

  1. run as admin accessdatabaseengine_64.exe 以admin accessdatabaseengine_64.exe运行
  2. run java - 7-64 bit - jre. 运行java - 7-64位 - jre。

如果您在NETBEANS中工作,那么在解压缩ucanacess.zip文件后,使用项目的属性窗口在类路径中添加所有jar文件,单击编译选项卡并添加jar文件,然后编译并测试应用程序。

JDBC-ODBC MS-ACCESS CONNECTION STOPPED WORKING IN JDK8. JDBC-ODBC MS-ACCESS连接已停止在JDK8中工作。 I solved the issue by installing JDK7 along with JDK8 in the same PC, once installed JDK7 I assigned it as the JDK version to use in my project as follows in Netbeans: 我通过在同一台PC上安装JDK7和JDK8来解决了这个问题,一旦安装了JDK7,我就把它作为JDK版本分配给我在我的项目中使用如下Netbeans:

1.RIGHT CLICK THE PROJECT IN THE LIST > CLICK PROPERTIES 1.右键单击列表中的项目>点击属性

2.CLICK LIBRARIES ON THE LEFT NAVIGATION TREE 2.在左导航树上列出图书馆

3.CLICK BUTTON MANAGE PLATFORMS > CLICK BUTTON ADD PLATFORM... 3.CLICK按钮管理平台>点击按钮添加平台...

4.FOLLOW WIZARD, DESPITE IT SHOWS JAVA STANDARD EDITION CLICK NEXT 4.FOLLOW WIZARD,尽管它显示JAVA STANDARD EDITION点击下一步

5.NAVIGATE TO C:\\Program Files (x86)\\Java AND SELECT THE FOLDER OF JDK7 > CLICK NEXT 5.NAVIGATE到C:\\ Program Files(x86)\\ Java和SELECT JDK7的文件夹>点击下一步

6.THE FIELD AUTOFILL WITH THE RIGHT INFO... > THEN CLICK FINISH 6.具有正确信息的现场自动填充...>然后单击完成

7.SELECT THE JDK PLATFORM FROM THE LIST > CLICK CLOSE > OK 7.从列表中选择JDK平台>点击关闭>确定

8.JDK7 SHOULD SHOW IN LIBRARIES PACKAGE. 8.JDK7应该在图书馆包装中显示。

JDK7 in Libraries Package Click Back in Browser to return here after looking at the image... 库包中的JDK7单击浏览器中的返回以查看图像后返回此处...

From here on everything must run smoothly. 从这里开始,一切都必须顺利进行。

Hope it solves your problem. 希望它能解决你的问题。

Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM