0

I cannot connect to my access database using jdbc:ucanaccess driver. Here the code:

public void open_conn()
{
    try
    {
        Class.forName("net.ucanaccess.jdbc.UcanaccessDriver"); 
        String url = "jdbc:ucanaccess://C:\\AnalysisLab\\dbanal.accdb";
        conn = DriverManager.getConnection(url, "username", "password");
        stmt = conn.createStatement();
    }
    catch (Exception e)
    {
        JOptionPane.showMessageDialog(null,"Error: "+e.getLocalizedMessage()); e.printStackTrace();
    }
}

The error reported : "Error: net.ucanaccess.jdbc.UcanaccessDriver" I tried including org-netbeans-modules-db-mysql.jar file in the jar directory, but it doesn't work.

  • The error indicates that the driver wasn't loaded, which probably means that you don't have it on your classpath. Refer to the duplicate for instructions. Otherwise you will need to provide a lot more information about your setup, the classpath, how you run your application, and show the actual stacktrace of the exception, not just the error message that you created yourself. – Mark Rotteveel Feb 14 '17 at 17:25

1 Answers1

0

This documentation says that your URL should be in the following format:

Connection conn=DriverManager.getConnection("jdbc:ucanaccess://",user, password);

// for example: Connection conn=DriverManager.getConnection("jdbc:ucanaccess://c:/pippo.mdb");

So your URL is going to change to:

String url = "jdbc:ucanaccess://C:\\AnalysisLab\\dbanal.accdb";

And add the following jar files to your CLASSPATH as all of these are needed by the actual JDBC driver itself:

  • ucanaccess-3.0.3.jar
  • commons-lang-2.6.jar
  • commons-logging-1.1.1.jar
  • hsqldb.jar
  • jackcess-2.1.3.jar

As per your question, I assume that you are on Netbeans and hence you need to all these 5 jars there in the following manner:

Expand the tree view for your project, right-click the "Libraries" folder and choose "Add JAR/Folder...", then browse to the JAR file.

A very detailed explanation of a similar question can be found here.

Community
  • 1
  • 1
N00b Pr0grammer
  • 3,930
  • 4
  • 28
  • 40