2

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:

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

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

Any ideas why?

Sicco
  • 5,759
  • 3
  • 41
  • 58
dula
  • 43
  • 1
  • 2
  • 9
  • Not sure but try changing slashes in: String accessFileName = "E:/L4_project/sample/db/Database"; To double backslash: String accessFileName = "E:\\L4_project\\sample\\db\\Database"; – Jaime Hablutzel Jul 30 '11 at 06:34
  • thank you. It's working..... :) – dula Jul 30 '11 at 13:57
  • 1
    Note that the JDBC-ODBC Bridge has been **removed** from Java 8 and is not supported (ref: [here](http://docs.oracle.com/javase/7/docs/technotes/guides/jdbc/bridge.html) and [here](http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6345277)). [UCanAccess](http://ucanaccess.sourceforge.net/site.html) is a popular alternative (details [here](http://stackoverflow.com/q/21955256/2144390)). – Gord Thompson Mar 20 '15 at 13:25

5 Answers5

3

You may revisit steps, Control panel>Administrative Tools>Data Sources>Add>Microsoft Access Drivers(*mdb,*accdb)>ok>ok>ok. It might work.for ODBC connection.

rk_P
  • 230
  • 1
  • 3
  • 12
3

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

Manipulating an Access database from Java without ODBC

Community
  • 1
  • 1
Gord Thompson
  • 98,607
  • 26
  • 164
  • 342
1

I think you need to specify jdbc:odbc:my_access_odbc_dsn as the URL, where my_access_odbc_dsn is your DSN name as configured in the ODBC. In my case, I was accessing an MS Access 2013 database called "PavoDB", so my URL was:

private static final String url="jdbc:odbc:PavoDB";
TimWaters
  • 27
  • 3
1

unless you specifically need to be able to run arbitrary sql queries, you can always give jackcess a try.

jtahlborn
  • 50,774
  • 5
  • 71
  • 112
0

The driver probably hasn't been updated to read that format. .mdb is the original one for Access; .accdb must have been revised and the ODBC driver hasn't kept up.

duffymo
  • 293,097
  • 41
  • 348
  • 541