I am trying to connect access database with eclipse but it is not working.

I first created data source in "ODBC Data Source Administrator(32-bits)" for windows
than I add the jar file

But I am getting an error:

ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver
SQLException: No suitable driver found for jdbc:odbc:J2EE_Database







public class ex01 {
    public static void main(String[] args) {
        String url = "jdbc:odbc:J2EE_Database";

        Connection con;

        Statement stmt;

        String query = "Select * from user";

        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        } catch (java.lang.ClassNotFoundException e) {
            System.err.print("ClassNotFoundException: ");
            System.err.println(e.getMessage());
        }

         try
          {
           con = DriverManager.getConnection(url, "", "");

           stmt = con.createStatement();

           // Returns a ResultSet that contains the data produced by the query; never null
           ResultSet rs = stmt.executeQuery(query);


           System.out.println("Course Data:");
           System.out.println("Course\tTitle\tCredits");

           while (rs.next())
           {
            String course = rs.getString("COURSE");
            String description = rs.getString("DESCRIPTION");
            int credits = rs.getInt("CREDITS");
            System.out.println(course + "\t" + description + "\t" + credits);
           }

           stmt.close();

           con.close();
          } 
          catch(SQLException ex) 
          {
           System.err.println("SQLException: " + ex.getMessage());
          }
    }
}

Edited 1 Year Ago by pritaeas: Moved to Java

Are you sure you are adding the .jar file to a directory where Eclipse can find it? Have you tried using the precise location in the Eclipse configuration for the ODCB file?

which jar file did you add?
Are you using Java 8? as of Java 8, the JDBC-ODBC bridge is not longer part of Java, which could lead to that error.

Yes, there's no ODBC in current Java. Fortunately there's an open source JDBC Access driver http://ucanaccess.sourceforge.net/site.html that seems to have a good reputation.

Ps: That call to Class.forName (line 21) (and the problems it can spawn) has been obsolete for many years now. Don't use it.
It just shows how out of date so many tutorials are! The only ones you can rely on are Oracle's own: https://docs.oracle.com/javase/tutorial/jdbc/basics/connecting.html

Edited 1 Year Ago by JamesCherrill

I used the link and download some jar files. Take a look at attached image for my jar path. I just trying to use Access database with my java project.
Also I am using Java 1.8.0_40

ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver
java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at ex01.main(ex01.java:34)
Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class net.ucanaccess.jdbc.UcanaccessDriver
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at java.sql.DriverManager.isDriverAllowed(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at ex01.main(ex01.java:43)

Untitled.png

Also I am using the following Java version: 1.8.0_40

Edited 1 Year Ago by hwoarang69

sigh... READ and COMPREHEND, the bridge driver is gone, history, finito, and good riddance.
Use another database engine instead, and the driver associated with that. Which means you have to change your code.

I am trying but I dont know what you mean by bridge driver? I though JDBC was the online database and ODBC was Access database.

I just want to connect my Access Database(software) with Java. Could you please send me tutorial link, that explain how to set up driver and sample code to connect to MS Access database? I tried to find but I was confouse.

JDBC was the online database ?? what online database? It's easier to use 'tools' if you know what they are and what they (are supposed to) do.

You want a good tutorial link? James already posted them.

Could you please send me tutorial link, that explain how to set up driver and sample code to connect to MS Access database? I tried to find but I was confouse.

You need to stop, think, understand the error message and then proceed. Your code doesn't work even after using the "ucanaccess" driver. What can be problem? The error message says "Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class net.ucanaccess.jdbc.UcanaccessDriver". Did you search around the internet to understand what "NoClassDefFoundError" stands for?

This article has been dead for over six months. Start a new discussion instead.