2

Just wondering if anyone cane help me, I'm trying to connect to an MS Access Database. I have done it on other projects and used exactly the same code. Can anyone see if I have done anything wrong?

try {
        System.out.println("Attempting Database Connection");
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        String sourceURL = "jdbc:odbc:Driver={Microsoft Access Driver(*.mdb)};DBQ=MotivationDatabase.mdb;";
        connection = DriverManager.getConnection(sourceURL, "", "");
        stmt = connection.createStatement();
        System.out.println("Connection made");
    } catch (Exception e) {
        System.out.println("Database connection attempt failed");
        System.out.println(e);
    }

I keep getting the error:

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

But my database is in the same folder as my project like I've done before s I'm not sure why i am getting this error. Help?

Sandeep Chatterjee
  • 3,220
  • 9
  • 31
  • 47
user3365117
  • 21
  • 1
  • 4

3 Answers3

2
  • Control Panel -> Administrative Tools -> ODBC Data Sources -> Add -> Microsoft Access Driver(*mdb,*accdb)

  • Specify the correct path to MotivationDatabase.mdb corresponding to Data Source name and save the settings.

Refer here.

enter image description here

Code:

public class Main {

    @SuppressWarnings("unused")
    public static void main(String[] args) {

        try {
            System.out.println("Attempting Database Connection");
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String sourceURL = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ="
            + "D:\\MotivationDatabase.mdb";
            Connection connection = DriverManager.getConnection(sourceURL);
            System.out.println("Connection made");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

Output:

enter image description here

P.S: Please learn to work with JDBC as JDBC-ODBC Bridge will be removed in JDK8.See here.

EDIT:

You can also use JDBC along with UCanAccess API to connect to an MSAccess database. You would need the following jars in your project build path.

  1. commons-lang-2.6.jar
  2. commons-logging-1.1.1.jar
  3. hsqldb.jar
  4. jackcess-2.1.0.jar
  5. ucanaccess-2.0.9.5.jar

Code:

connection = DriverManager
.getConnection("jdbc:ucanaccess:////REMOTE-IP-ADDRESS/shared-folder/TestDB.mdb");
System.out.println("CONNECTION ESTABLISHED....");

Works fine with JDK8. You can download the entire source code from here.

Sandeep Chatterjee
  • 3,220
  • 9
  • 31
  • 47
0

Sun JDBC ODBC will notte work with MS access when java 8 will ne release: I suggest you to use apache poi project. It s simple and works great.

Yeah it's correct the right project is jakcess:

import com.healthmarketscience.jackcess.DatabaseBuilder;
import com.healthmarketscience.jackcess.Row;
import com.healthmarketscience.jackcess.Table;
try{Table table = DatabaseBuilder.open(new File("filename")).getTable("tablename");
    righe.add(0);
    for(Row row : table) {
        String articolo=row.get("ColName").toString();
AndreaTaroni86
  • 549
  • 12
  • 27
0

Try this class forename and connection URL.Add the below jar files to your projects:

commons-lang.jar,commons-logging.jar,hsqldb.jar,jackcess.jar,ucanaccess.jar

Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
//change the path with your own accdb file
String URL = "jdbc:ucanaccess://D:\\projects\\test.accdb";
Connection con = DriverManager.getConnection(URL);