33

I have created an MS Access database and assigned a DSN to it. I want to access it through my Java application.

This is what I am doing:

public class AccessDbConnection {

    public static void main(String[] args) {
        System.out.println("**ACCESS DB CONNECTION**");

        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // for MS Access ... MS access driver loading

            String     conURL    = "jdbc:odbc:sampleDNS";
            Connection con       = DriverManager.getConnection(conURL);
            Statement  statement = con.createStatement();
            String     qry       = "SELECT * FROM Table1";
            ResultSet  rs        = statement.executeQuery(qry);

            while(rs.next()) {
                String id    = rs.getString("ID") ;
                String fname = rs.getString("First_Name");
                String lname = rs.getString("Last_Name");
                System.out.println(id + fname + lname);
            }
        } catch (ClassNotFoundException ex) {
            System.out.println("Classforname Exception!!");
            Logger.getLogger(AccessDbConnection.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            System.out.println("DriverManager Exception!!");
            Logger.getLogger(AccessDbConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

I am getting the exception at the first line of try block. That is class.forname("..");. Why am I having this Exception?

TallTed
  • 8,292
  • 2
  • 18
  • 33
hina abbasi
  • 399
  • 1
  • 3
  • 13

5 Answers5

29

For Java 7 you can simply omit the Class.forName() statement as it is not really required.

For Java 8 you cannot use the JDBC-ODBC Bridge because it has been removed. You will need to use something like UCanAccess instead. For more information, see

Manipulating an Access database from Java without ODBC

Community
  • 1
  • 1
Gord Thompson
  • 98,607
  • 26
  • 164
  • 342
  • 1
    that is jackcess .. but jackcess is also not helping me. again some exceptions are occuring. i am using Database db = DatabaseBuilder.open(new File("db.mdb")); – hina abbasi Apr 11 '14 at 03:54
  • Database db; db = DatabaseBuilder.open(new File("E://PersonsDB/personsinfo.mdb")); – hina abbasi Apr 12 '14 at 09:37
  • 1
    @hinaabbasi If you have a question about using UCanAccess or Jackcess please [ask a new question](http://stackoverflow.com/questions/ask) that includes the relevant code and the actual error message(s) you are receiving. – Gord Thompson Apr 12 '14 at 16:08
  • Does it work for Visual FoxPro database? I just tried and did not work. Since it does not have `.accdb` file extension. Thanks – Yubaraj Jun 17 '16 at 11:07
  • @Yubaraj - No, UCanAccess cannot work with VFP databases. – Gord Thompson Jun 17 '16 at 12:21
  • @GordThompson please have a look at this http://stackoverflow.com/questions/42275749/netbeans-8-2-java-lang-classnotfoundexception-sun-jdbc-odbc-jdbcodbcdriver-exce – Pranav MS Feb 16 '17 at 14:07
5

in JDK 8, jdbc odbc bridge is no longer used and thus removed fro the JDK. to use Microsoft Access database in JAVA, you need 5 extra JAR libraries.

1- hsqldb.jar

2- jackcess 2.0.4.jar

3- commons-lang-2.6.jar

4- commons-logging-1.1.1.jar

5- ucanaccess-2.0.8.jar

add these libraries to your java project and start with following lines.

Connection conn=DriverManager.getConnection("jdbc:ucanaccess://<Path to your database i.e. MS Access DB>");
Statement s = conn.createStatement();

path could be like E:/Project/JAVA/DBApp

and then your query to be executed. Like

ResultSet rs = s.executeQuery("SELECT * FROM Course");
while(rs.next())
    System.out.println(rs.getString("Title") + " " + rs.getString("Code") + " " + rs.getString("Credits"));

certain imports to be used. try catch block must be used and some necessary things no to be forgotten.

Remember, no need of bridging drivers like jdbc odbc or any stuff.

Umair Bhatti
  • 87
  • 1
  • 4
3

Setup:

My OS windows 8 64bit
Eclipse version Standard/SDK Kepler Service Release 2
My JDK is jdk-8u5-windows-i586
My JRE is jre-8u5-windows-i586

This how I overcome my error.

At the very first my Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") also didn't work. Then I login to this website and downloaded the UCanAccess 2.0.8 zip (as Mr.Gord Thompson said) file and unzip it.

Then you will also able to find these *.jar files in that unzip folder:

ucanaccess-2.0.8.jar
commons-lang-2.6.jar
commons-logging-1.1.1.jar
hsqldb.jar
jackcess-2.0.4.jar

Then what I did was I copied all these 5 files and paste them in these 2 locations:

C:\Program Files (x86)\eclipse\lib
C:\Program Files (x86)\eclipse\lib\ext

(I did that funny thing becoz I was unable to import these libraries to my project)

Then I reopen the eclipse with my project.then I see all that *.jar files in my project's JRE System Library folder.

Finally my code works.

public static void main(String[] args) 
{

    try
    {

        Connection conn=DriverManager.getConnection("jdbc:ucanaccess://C:\\Users\\Hasith\\Documents\\JavaDatabase1.mdb");
        Statement stment = conn.createStatement();
        String qry = "SELECT * FROM Table1";

        ResultSet rs = stment.executeQuery(qry);
        while(rs.next())
        {
            String id    = rs.getString("ID") ;
            String fname = rs.getString("Nama");

            System.out.println(id + fname);
        }
    }
    catch(Exception err)
    {
        System.out.println(err);
    }


    //System.out.println("Hasith Sithila");

}
Garrett Hyde
  • 4,815
  • 7
  • 46
  • 50
0

add these dependecies to your .pom file:

<dependency>
  <groupId>org.hsqldb</groupId>
  <artifactId>hsqldb</artifactId>
  <version>2.5.0</version>
  <scope>test</scope>
</dependency>

<dependency>
  <groupId>com.healthmarketscience.jackcess</groupId>
  <artifactId>jackcess-encrypt</artifactId>
  <version>3.0.0</version>
</dependency>

<dependency>
  <groupId>net.sf.ucanaccess</groupId>
  <artifactId>ucanaccess</artifactId>
  <version>5.0.0</version>
</dependency>

<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-lang3</artifactId>
  <version>3.9</version>
</dependency>

<dependency>
  <groupId>commons-logging</groupId>
  <artifactId>commons-logging</artifactId>
  <version>1.2</version>
</dependency>

and add to your code to call a driver:

Connection conn = DriverManager.getConnection("jdbc:ucanaccess://{file_location}/{accessdb_file_name.mdb};memory=false");
0

Make sure you have closed your MSAccess file before running the java program.