how to connect to MS Access database with Java 8?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gamer2d
    New Member
    • Jul 2014
    • 10

    how to connect to MS Access database with Java 8?

    How to connect to MS Acess Database with Java 8?

    Here is the error I am getting:

    Code:
    SQLException: No suitable driver found for jdbc:ucanacess:Database11


    Here is my path for my MS Access Database that I am trying to connect:

    Code:
    C:\Users\dave\My_WorkSpace\Eclipse_Workspaces\workspace-jsp\Database11.accdb

    Take A look at the attached image. It will show you what jar files I have added to my project.



    and here is the code I am using to connect to database.

    Code:
    public class ex01 {
    	public static void main(String[] args) {
    		String url = "jdbc:ucanacess:Database11";
    
    		Connection con;
    
    		Statement stmt;
    
    		String query = "Select * from user";
    
    		
    		 try
    		  {
    			 Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
    			 
    		   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("User Data:");
    		   System.out.println("FirstName\tLastName\tAge");
    
    		   while (rs.next())
    		   {
    			   String fName = rs.getString("FirstName");
    			   String lName = rs.getString("LastName");
    			   int age = rs.getInt("age");
    			   System.out.println(fName + "\t" + lName + "\t" + age);
    		   }
    
    		   stmt.close();
    		   
    		   con.close();
    		  } 
    		  catch(SQLException ex) 
    		  {
    		   System.err.println("SQLException: " + ex.getMessage());
    		  }
    	}
    }
    Attached Files
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    #2
    The error is correct,
    You have a typing error in 'ucanaccess'

    Comment

    • gamer2d
      New Member
      • Jul 2014
      • 10

      #3
      Thank you so much for getting back to me so fast!

      I just have fix that typo by change that line to:

      Code:
      String url = "jdbc:ucanaccess://C:/Users/dave/My_WorkSpace/Eclipse_Workspaces/workspace-jsp/Database11.accdb";
      and now I get the following error:
      Code:
      Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang/builder/CompareToBuilder
      	at com.healthmarketscience.jackcess.impl.RowIdImpl.compareTo(RowIdImpl.java:106)
      	at com.healthmarketscience.jackcess.impl.IndexData$Entry.compareTo(IndexData.java:2039)
      	at com.healthmarketscience.jackcess.impl.IndexData$Entry.compareTo(IndexData.java:1847)
      	at java.util.Collections.indexedBinarySearch(Unknown Source)
      	at java.util.Collections.binarySearch(Unknown Source)
      	at com.healthmarketscience.jackcess.impl.IndexData$DataPage.findEntry(IndexData.java:2570)
      	at com.healthmarketscience.jackcess.impl.IndexData.findEntryPosition(IndexData.java:844)
      	at com.healthmarketscience.jackcess.impl.IndexData.access$3700(IndexData.java:47)
      	at com.healthmarketscience.jackcess.impl.IndexData$EntryCursor.updatePosition(IndexData.java:2335)
      	at com.healthmarketscience.jackcess.impl.IndexData$EntryCursor.restorePosition(IndexData.java:2273)
      	at com.healthmarketscience.jackcess.impl.IndexData$EntryCursor.restorePosition(IndexData.java:2256)
      	at com.healthmarketscience.jackcess.impl.IndexData$EntryCursor.beforeEntry(IndexData.java:2218)
      	at com.healthmarketscience.jackcess.impl.IndexCursorImpl.findPotentialRow(IndexCursorImpl.java:376)
      	at com.healthmarketscience.jackcess.impl.IndexCursorImpl.findFirstRowByEntryImpl(IndexCursorImpl.java:282)
      	at com.healthmarketscience.jackcess.impl.IndexCursorImpl.findFirstRowByEntry(IndexCursorImpl.java:153)
      	at com.healthmarketscience.jackcess.impl.DatabaseImpl$DefaultTableFinder.findRow(DatabaseImpl.java:2074)
      	at com.healthmarketscience.jackcess.impl.DatabaseImpl$TableFinder.findObjectId(DatabaseImpl.java:1953)
      	at com.healthmarketscience.jackcess.impl.DatabaseImpl.readSystemCatalog(DatabaseImpl.java:858)
      	at com.healthmarketscience.jackcess.impl.DatabaseImpl.<init>(DatabaseImpl.java:518)
      	at com.healthmarketscience.jackcess.impl.DatabaseImpl.open(DatabaseImpl.java:389)
      	at com.healthmarketscience.jackcess.DatabaseBuilder.open(DatabaseBuilder.java:248)
      	at net.ucanaccess.jdbc.DefaultJackcessOpener.open(DefaultJackcessOpener.java:38)
      	at net.ucanaccess.jdbc.DBReference.<init>(DBReference.java:158)
      	at net.ucanaccess.jdbc.DBReferenceSingleton.loadReference(DBReferenceSingleton.java:57)
      	at net.ucanaccess.jdbc.UcanaccessDriver.connect(UcanaccessDriver.java:103)
      	at java.sql.DriverManager.getConnection(Unknown Source)
      	at java.sql.DriverManager.getConnection(Unknown Source)
      	at ex01.main(ex01.java:37)
      Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang.builder.CompareToBuilder
      	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)
      	... 28 more
      but if you take a look at the attached image. you can see I have all the jar files.

      Comment

      • Luuk
        Recognized Expert Top Contributor
        • Mar 2012
        • 1043

        #4
        sorry to say, but my Java skills fail at this point

        But, I think, you should improve your code
        change line #9 to:
        Code:
                 String query = "Select FirstName, LastName from user";
        Whenever you change your database, and i.e. drop the column 'FirstName' your query should give an error. This will not happen if you do 'select * from....', that's why I don't like the '*'

        Comment

        Working...