Search code examples
javams-access-2007ucanaccess

UCanAccess can not find my DB


I'm trying to connect my Java code to a table in access using UCanAccess here is my code:

import java.sql.*;

public class HH {

    public static void main(String[] args) throws SQLException {
        // opens a connection to the database
        Connection conn=DriverManager.getConnection("jdbc:ucanaccess://C:/Users/user/Documents/Database5.accdb‬");
        Statement s = conn.createStatement();
    }
}

for some reason it throws this:

Exception in thread "main" net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::3.0.0 given file does not exist: C:\Users\user\Documents\Database5.accdb‬
    at net.ucanaccess.jdbc.UcanaccessDriver.connect(UcanaccessDriver.java:258)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at HH.main(HH.java:8)
Caused by: java.io.FileNotFoundException: given file does not exist: C:\Users\user\Documents\Database5.accdb‬
    at com.healthmarketscience.jackcess.impl.DatabaseImpl.open(DatabaseImpl.java:362)
    at com.healthmarketscience.jackcess.DatabaseBuilder.open(DatabaseBuilder.java:248)
    at net.ucanaccess.jdbc.DefaultJackcessOpener.open(DefaultJackcessOpener.java:35)
    at net.ucanaccess.jdbc.DBReference.<init>(DBReference.java:160)
    at net.ucanaccess.jdbc.DBReferenceSingleton.loadReference(DBReferenceSingleton.java:51)
    at net.ucanaccess.jdbc.UcanaccessDriver.connect(UcanaccessDriver.java:96)
    ... 3 more

the path is OK, if I enter it into windows explorer it opens my DB.


Solution

  • From my experience with UCanAccess etc I think your code "jdbc:ucanaccess://C:/Users/user/Documents/Database5.accdb‬"

    Should be

    "jdbc:ucanaccess://C:\\Users\\user\\Documents\\Database5.accdb‬"

    Reason: in the filesystem it uses the backslash rather than forwards slash, and in java strings the '\' character is an escape, so to enter just one of that character you need to escape itself: \\ which will appear as \ to the getConnection method