ms-accessjdbcucanaccess

UcanaccessSQLException: UCAExc:::3.0.6 CryptCodecOpener


While using Java 7 I used to connect to MS Access by using the JDBC-ODBC Bridge, but now I'm using Java 8 with UCanAccess and I'm running into some issues. I have 2 classes:

  1. CryptCodecOpener class

    package javaapplication1;
    
    
    import java.io.File;
    import java.io.IOException;
    import net.ucanaccess.jdbc.JackcessOpenerInterface;
    import com.healthmarketscience.jackcess.CryptCodecProvider;
    import com.healthmarketscience.jackcess.Database;
    import com.healthmarketscience.jackcess.DatabaseBuilder;
    
    public class CryptCodecOpener implements JackcessOpenerInterface {
         @Override
    public Database open(File fl,String pwd) throws IOException {
       DatabaseBuilder dbd =new DatabaseBuilder(fl);
       dbd.setAutoSync(false);
       dbd.setCodecProvider(new CryptCodecProvider(pwd));
       dbd.setReadOnly(false);
       return dbd.open();
    }
    
    }
    

and

  1. JavaApplication1 class

    package javaapplication1;
    import java.sql.*;
    public class JavaApplication1 {
    static Connection con;
    static Statement st;
    static PreparedStatement pst;
    static ResultSet rs;
    
    public static void main(String[] args) 
    {
        // TODO code application logic here
    
        try
        {
            //Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
         String env=System.getenv("ProgramFiles");
         //Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        String dbURL = "jdbc:ucanaccess://C:\\test.accdb;jackcessOpener=CryptCodecOpener";
         //String dbURL = "jdbc:ucanaccess://"+env+"\\RSSBV0\\db\\rssboffdb.accdb";
         //String username="";
         String username=System.getProperty("user.name");
         String password="r$$b231";
         con = DriverManager.getConnection(dbURL,username,password);
         String query = "select username from userstb";
         pst = con.prepareStatement(query);
         rs = null;
    
         try
         {
            rs = pst.executeQuery();
    
            while(rs.next())
            {
                System.out.println(rs.getString("username"));
            }
         }
         catch (Exception e)
         {
           pst.close();
           con.close();
         }
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    
    
    }
    
    }
    

Both classes are in the same packages but when I'm trying to run it, this following error comes up:

net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::3.0.6 CryptCodecOpener

Can anyone help me and give me advice?


Solution

  • The jackcessOpener parameter requires the fully-qualified name of the class that implements JackcessOpenerInterface, even if that class is in the same package as the class that invokes it. So your connection URL

    String dbURL = "jdbc:ucanaccess://C:\\test.accdb;jackcessOpener=CryptCodecOpener";
    

    is incomplete. You need to use

    String dbURL = "jdbc:ucanaccess://C:\\test.accdb;jackcessOpener=javaapplication1.CryptCodecOpener";
    

    Also, be aware that you may have insufficient permissions to work with the database file if it is stored in the root folder of a system drive (C:\). You really should move the database file to another location where you can be sure that you will have full read/write access.