Reputation: 13
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:
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
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?
Upvotes: 1
Views: 2377
Reputation: 5495
Finally i solved by install :
UcAnaccess
library version 4.0.4
if you are using maven you can use the following :
<!-- https://mvnrepository.com/artifact/net.sf.ucanaccess/ucanaccess -->
<dependency>
<groupId>net.sf.ucanaccess</groupId>
<artifactId>ucanaccess</artifactId>
<version>4.0.4</version>
</dependency>
and Jackcess
library version 1.1.10 ,and also jackcess-encrypt
library with version 3.0.0.
Maven :
<!-- https://mvnrepository.com/artifact/jackcess/jackcess -->
<dependency>
<groupId>jackcess</groupId>
<artifactId>jackcess</artifactId>
<version>1.1.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.healthmarketscience.jackcess/jackcess-encrypt -->
<dependency>
<groupId>com.healthmarketscience.jackcess</groupId>
<artifactId>jackcess-encrypt</artifactId>
<version>3.0.0</version>
</dependency>
to try java code :
public static void tryit() throws SQLException, ClassNotFoundException {
try {
Connection conn = DriverManager
.getConnection("jdbc:ucanaccess://C:\\Users\\aag\\Desktop\\F_B_1.mdb;jackcessOpener=com.floridatrading.mobile_app.mobileapp.manage.CryptCodecOpener");
System.out.println("Successfully");
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Upvotes: 0
Reputation: 123399
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.
Upvotes: 2