By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,695 Members | 1,338 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,695 IT Pros & Developers. It's quick & easy.

how to connect to MS Access database with Java 8?

P: 10
How to connect to MS Acess Database with Java 8?

Here is the error I am getting:

Expand|Select|Wrap|Line Numbers
  1. SQLException: No suitable driver found for jdbc:ucanacess:Database11
  2.  


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

Expand|Select|Wrap|Line Numbers
  1. 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.

Expand|Select|Wrap|Line Numbers
  1. public class ex01 {
  2.     public static void main(String[] args) {
  3.         String url = "jdbc:ucanacess:Database11";
  4.  
  5.         Connection con;
  6.  
  7.         Statement stmt;
  8.  
  9.         String query = "Select * from user";
  10.  
  11.  
  12.          try
  13.           {
  14.              Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
  15.  
  16.            con = DriverManager.getConnection(url, "", "");
  17.  
  18.            stmt = con.createStatement();
  19.  
  20.            // Returns a ResultSet that contains the data produced by the query; never null
  21.            ResultSet rs = stmt.executeQuery(query);
  22.  
  23.  
  24.            System.out.println("User Data:");
  25.            System.out.println("FirstName\tLastName\tAge");
  26.  
  27.            while (rs.next())
  28.            {
  29.                String fName = rs.getString("FirstName");
  30.                String lName = rs.getString("LastName");
  31.                int age = rs.getInt("age");
  32.                System.out.println(fName + "\t" + lName + "\t" + age);
  33.            }
  34.  
  35.            stmt.close();
  36.  
  37.            con.close();
  38.           } 
  39.           catch(SQLException ex) 
  40.           {
  41.            System.err.println("SQLException: " + ex.getMessage());
  42.           }
  43.     }
  44. }
  45.  
Attached Images
File Type: png tesss.png (8.7 KB, 401 views)
Aug 2 '15 #1
Share this Question
Share on Google+
3 Replies


Expert 100+
P: 1,035
The error is correct,
You have a typing error in 'ucanaccess'
Aug 2 '15 #2

P: 10
Thank you so much for getting back to me so fast!

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

Expand|Select|Wrap|Line Numbers
  1. String url = "jdbc:ucanaccess://C:/Users/dave/My_WorkSpace/Eclipse_Workspaces/workspace-jsp/Database11.accdb";
  2.  
and now I get the following error:
Expand|Select|Wrap|Line Numbers
  1. Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang/builder/CompareToBuilder
  2.     at com.healthmarketscience.jackcess.impl.RowIdImpl.compareTo(RowIdImpl.java:106)
  3.     at com.healthmarketscience.jackcess.impl.IndexData$Entry.compareTo(IndexData.java:2039)
  4.     at com.healthmarketscience.jackcess.impl.IndexData$Entry.compareTo(IndexData.java:1847)
  5.     at java.util.Collections.indexedBinarySearch(Unknown Source)
  6.     at java.util.Collections.binarySearch(Unknown Source)
  7.     at com.healthmarketscience.jackcess.impl.IndexData$DataPage.findEntry(IndexData.java:2570)
  8.     at com.healthmarketscience.jackcess.impl.IndexData.findEntryPosition(IndexData.java:844)
  9.     at com.healthmarketscience.jackcess.impl.IndexData.access$3700(IndexData.java:47)
  10.     at com.healthmarketscience.jackcess.impl.IndexData$EntryCursor.updatePosition(IndexData.java:2335)
  11.     at com.healthmarketscience.jackcess.impl.IndexData$EntryCursor.restorePosition(IndexData.java:2273)
  12.     at com.healthmarketscience.jackcess.impl.IndexData$EntryCursor.restorePosition(IndexData.java:2256)
  13.     at com.healthmarketscience.jackcess.impl.IndexData$EntryCursor.beforeEntry(IndexData.java:2218)
  14.     at com.healthmarketscience.jackcess.impl.IndexCursorImpl.findPotentialRow(IndexCursorImpl.java:376)
  15.     at com.healthmarketscience.jackcess.impl.IndexCursorImpl.findFirstRowByEntryImpl(IndexCursorImpl.java:282)
  16.     at com.healthmarketscience.jackcess.impl.IndexCursorImpl.findFirstRowByEntry(IndexCursorImpl.java:153)
  17.     at com.healthmarketscience.jackcess.impl.DatabaseImpl$DefaultTableFinder.findRow(DatabaseImpl.java:2074)
  18.     at com.healthmarketscience.jackcess.impl.DatabaseImpl$TableFinder.findObjectId(DatabaseImpl.java:1953)
  19.     at com.healthmarketscience.jackcess.impl.DatabaseImpl.readSystemCatalog(DatabaseImpl.java:858)
  20.     at com.healthmarketscience.jackcess.impl.DatabaseImpl.<init>(DatabaseImpl.java:518)
  21.     at com.healthmarketscience.jackcess.impl.DatabaseImpl.open(DatabaseImpl.java:389)
  22.     at com.healthmarketscience.jackcess.DatabaseBuilder.open(DatabaseBuilder.java:248)
  23.     at net.ucanaccess.jdbc.DefaultJackcessOpener.open(DefaultJackcessOpener.java:38)
  24.     at net.ucanaccess.jdbc.DBReference.<init>(DBReference.java:158)
  25.     at net.ucanaccess.jdbc.DBReferenceSingleton.loadReference(DBReferenceSingleton.java:57)
  26.     at net.ucanaccess.jdbc.UcanaccessDriver.connect(UcanaccessDriver.java:103)
  27.     at java.sql.DriverManager.getConnection(Unknown Source)
  28.     at java.sql.DriverManager.getConnection(Unknown Source)
  29.     at ex01.main(ex01.java:37)
  30. Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang.builder.CompareToBuilder
  31.     at java.net.URLClassLoader$1.run(Unknown Source)
  32.     at java.net.URLClassLoader$1.run(Unknown Source)
  33.     at java.security.AccessController.doPrivileged(Native Method)
  34.     at java.net.URLClassLoader.findClass(Unknown Source)
  35.     at java.lang.ClassLoader.loadClass(Unknown Source)
  36.     at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
  37.     at java.lang.ClassLoader.loadClass(Unknown Source)
  38.     ... 28 more
  39.  
but if you take a look at the attached image. you can see I have all the jar files.
Aug 2 '15 #3

Expert 100+
P: 1,035
sorry to say, but my Java skills fail at this point

But, I think, you should improve your code
change line #9 to:
Expand|Select|Wrap|Line Numbers
  1.          String query = "Select FirstName, LastName from user";
  2.  
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 '*'
Aug 3 '15 #4

Post your reply

Sign in to post your reply or Sign up for a free account.