2

I want to connect Java 8 with Access but the following error occurs and I don't know how to fix it. I always get this error:

java.sql.SQLException: No suitable driver found for jdbc:ucanaccess://C:/Users/Ghazi/workspace/java w access/login.accdb

I added 4 libraries:

  • hsqldb.jar
  • jackcess-2.0.7.jar
  • org.apache.commons.lang-2.6-source.jar
  • org.apache.commons.loggin-1.1.1-source.jar

This is my code

import java.sql.*;
public class DbConnection {
    Connection con;
    Statement st;
    DbConnection(){
        dbconnect();
        }
    //-----------------------
    public void dbconnect(){
        try
        {
     Connection conn=DriverManager.getConnection("jdbc:ucanaccess://C:/Users/Ghazi/workspace/java w access/login.accdb");
      Statement stment = conn.createStatement();
        }
        catch(Exception err)
        {
            System.out.println(err);
        }
    }
    //--------------------------
        public static void main(String[]args){
            DbConnection ob=new DbConnection();
            }//end main
    }
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
user3000000
  • 35
  • 1
  • 1
  • 7

2 Answers2

0

Try adding "Class.forName():

   public void dbconnect(){
     try  {
        Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
        Connection conn=DriverManager.getConnection("jdbc:ucanaccess://C:/Users/Ghazi/workspace/java w access/login.accdb");
        Statement stment = conn.createStatement();
     }
     catch(Exception err) {
       System.out.println(err);
     }
   }

The basic problem is that earlier versions of Java/JDBC used ODBC to connect to MS-Access ... and the ODBC driver has been removed from Java 8.

Two alternatives are to use:

1) UCanAccess: http://ucanaccess.sourceforge.net/site.html

... or ...

2) Jackcess (Jackcess 2.0: New crunchy outside, same yummy filling!): http://jackcess.sourceforge.net/

If that doesn't work:

3) Please specify what what IDE you're using (Eclipse, etc - if applicable)

4) Make sure your jackcess-2.0.7.jar is explicitly included in the CLASSPATH (how to do this depends on your IDE)

5) Consider using a directory without spaces in the name

FoggyDay
  • 11,962
  • 4
  • 34
  • 48
  • thanks bro, but still doesn't work, I use eclipse I added jackcess-2.0.7.jar and 3 other library but not fixed my problem – user3000000 Dec 20 '14 at 05:20
  • You only need *one* library (jackcess-2.0.7.jar) and you need it in the Run/Debug CLASSPATH (not just build path). ALSO: did "forName()" help? Removing the space from the file path? ADDITIONAL SUGGESTION: try [DriverManager.getDrivers()](http://docs.oracle.com/javase/6/docs/api/java/sql/DriverManager.html) and list all the JDBC drivers before you call getConnection() - see if our friend "ucanaccess" is registered. It sounds like that's the problem. – FoggyDay Dec 20 '14 at 06:13
  • 1
    It looks like you didn't add the ucanaccess.jar, it isn't in your list. It's in the root of the ucanaccess distribution, at the end you need five jars. – jamadei Dec 20 '14 at 10:06
  • 2
    `Class.forName()` is definitely *not* required. – Gord Thompson Dec 20 '14 at 14:51
  • @GordThompson, FoggyDay probably suggested trying Class.forName() to glean information about the driver availability. Certainly not required, but not harmful, and maybe enlightening. – Dale Jun 08 '21 at 18:56
  • 1
    Adding `Class.forName()` definitely caused Eclipse IDE's "Export... > Runnable JAR file" function to do something good; without it, I get `No suitable driver found`, with it, I get database access. – Dale Jun 09 '21 at 19:31
0

I added 4 libraries

You need five (5) libraries. You forgot to add the "ucanaccess-x.y.z.jar" file itself.

For detailed instructions, see

Manipulating an Access database from Java without ODBC

Community
  • 1
  • 1
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418