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?

3
  • You need to specify the full path to the .mdb file in the JDBC URL. Commented Mar 9, 2014 at 15:01
  • @a_horse_with_no_name: He is not using JDBC. Commented Mar 9, 2014 at 15:23
  • @Sandeep: of course he/she is. It's the JDBC/ODBC bridge that is being used, but it's still JDBC Commented Mar 9, 2014 at 15:48

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I've already done this and still the same error, I'm at a loss to where I've gone wrong if I'm honest.
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();

Comments

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.