0

I want to connect Java 1.8 with Access but the error occurs and I don't know how to fix it. I have written this code:

import java.sql.*;
class db1
{
    public static void main(String args[])
    {
        //Load a driver
        try
        {
            String url = "jdbc:odbc:sample1";
            Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
            Connection con = DriverManager.getConnection("jdbc:ucanaccess://F:\\java\\project\\samp.accdb");
            //Connection con = DriverManager.getConnection("Jdbc:Odbc:sample1");
            Statement st = con.createStatement();
            String Sql = "Select * from tbsamp";
            ResultSet rs = st.executeQuery(Sql);
            //ResultSet rt = st.executeQuery(Sql);
            while (rs.next())
            {
                System.out.println(rs.getString("movie") + " \t " + rs.getString("price") + "\n");
            }
            rs.close();
            //rt.close();
            st.close();
            con.close();
        }
        catch(Exception e)
        {
            System.out.println("error " + e);
        }
    }
}

and on running it the following exception arises

[error java.lang.ClassNotFoundException: net.ucanaccess.jdbc.UcanaccessDriver][1]
Dan Lowe
  • 51,713
  • 20
  • 123
  • 112
  • 1
    You don't have the UCanAccess driver or one of its dependencies on the classpath of your application. – Mark Rotteveel Nov 01 '15 at 10:30
  • Have a look at the answer [here](http://stackoverflow.com/a/21955257/2144390). If you still have problems then [edit] your question with more details. (For example, are you using Eclipse? ...NetBeans? ...something else?) – Gord Thompson Nov 01 '15 at 13:24
  • 1
    To add to Mark and Gord, if you are not using an IDE but simple text code editor and command line, all UCanAccess .jar files (driver and lib) need to be added to your classpath environment variable. – Parfait Nov 01 '15 at 15:04
  • I have added the .jar files. The code is working now. Thank you. – anushka bajpai Nov 02 '15 at 10:50

0 Answers0