Search code examples
javajdbcucanaccess

"given file does not exist" issue with UCanAccess connection


I'm trying to connect to a Microsoft Access database in Eclipse (Mars 4.5.0; Java 1.8) on a Mac (el capitaine). I keep getting the error: net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::3.0.4 given file does not exist: Users/sebastianzeki/Documents/BEST2RFA_DBv1.accdb

This is my code:

import java.sql.*;

public class DbAccess
{
    public static void main(String[] args) 
    {

        try
        {
            Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
            Connection conn=DriverManager.getConnection("jdbc:ucanaccess://Users/sebastianzeki/Documents/BEST2RFA_DBv1.accdb;");
            Statement stment = conn.createStatement();
            String qry = "SELECT * FROM Table1";

            ResultSet rs = stment.executeQuery(qry);
            while(rs.next())
            {
                String id    = rs.getString("ID") ;
                String fname = rs.getString("Nama");

                System.out.println(id + fname);
            }
        }
        catch(Exception err)
        {
            System.out.println(err);
        }

    }
}

I'm sure its something to do with the pathname slashes but I've tried every permutation and still get the same error.


Solution

  • I'm not familiar with Mac file systems but have you tried "jdbc:ucanaccess:///..." (including an extra slash)?

    Explanation:

    The path to the database file immediately follows the jdbc:ucanaccess:// prefix of the connection URL, so for

    jdbc:ucanaccess://Users/sebastianzeki/Documents/BEST2RFA_DBv1.accdb;
    

    the path to the database file is

    Users/sebastianzeki/Documents/BEST2RFA_DBv1.accdb
    

    which is interpreted as a relative path, relative to the OS-level current directory in effect when the Java application was launched.

    In order for the path to be interpreted as an absolute path it must start with a forward slash, i.e.,

    /Users/sebastianzeki/Documents/BEST2RFA_DBv1.accdb
    

    therefore the connection URL needs to be

    jdbc:ucanaccess:///Users/sebastianzeki/Documents/BEST2RFA_DBv1.accdb