Menu

Installation and use of UCanAccess

Help
2015-10-07
2015-10-13
  • Franco Meazza

    Franco Meazza - 2015-10-07

    I am learning how to manage information I saved a long time ago in Access 2003 databases (and how to rewrite in Java user interfaces I prepared and programs I wrote at that time). I was satisfied of my results when, unfortunately, I updated my jdk from 7 to 8 edition. And I was no more able to connect my databases.
    I looked for pure Java JDBC Driver and found UCanAccess.
    I downloaded it and unzipped it in a directory named Programmi\UcanAccess-3.0.1-bin.
    I added the directory to system variable PATH as C:\Program Files\UCanAccess-3.0.1-bin.
    I tried to open database by means of these instructions (that, as far as I remember, I found at UCanAccess Web Site):
    String driver = "net.ucanaccess.jdbc.UcanaccessDriver";
    Class.forName(driver);
    but I got the ClassNotFoundException.

    1st quetion) I suppose I have to add something after having imported java.sql.; but what I have to import?
    2nd question) Have I to add some other directory to PATH?

    I know these are questions of a beginner and, in such a matter, I am a beginner. I would appreciiate if anyone can help me a little bit.

     
    • Gord Thompson

      Gord Thompson - 2015-10-07
       
  • Franco Meazza

    Franco Meazza - 2015-10-07

    I have already had a look at that site. Expecially at "Option 2: Manually adding the JARs to your project". But there is no answer to my question. At least no answer I can understand.
    Anyway thaks for your propmt reply.

     
  • Franco Meazza

    Franco Meazza - 2015-10-08

    Iread at the site the following (and I put in capital letters my questions):
    "Option 2: Manually adding the JARs to your project
    As mentioned above, UCanAccess requires Jackcess and HSQLDB. Jackcess in turn has its own dependencies. So to use UCanAccess you will need to include (WHERE?) the following components:

    UCanAccess (ucanaccess-2.x.x.jar)
    HSQLDB (hsqldb.jar, version 2.2.5 or newer)
    Jackcess (jackcess-2.x.x.jar)
    commons-lang (commons-lang-2.4.jar, or newer)
    commons-logging (commons-logging-1.0.4.jar, or newer)

    Fortunately, UCanAccess includes all of the required JAR files in its distribution file. When you unzip it you will see something like

    ucanaccess-2.0.4.jar
    lib/commons-lang-2.6.jar
    lib/commons-logging-1.1.3.jar
    lib/hsqldb.jar
    lib/jackcess-2.0.3.jar

    All you need to do is add all five (5) JARs to your project. (I DO NOT KNOW WHAT EXACTLY MEANS "TO ADD THE FILES TO MY PROJECT. I have just the attached class ArticoliVers0Java8.java):

     
  • Franco Meazza

    Franco Meazza - 2015-10-08

    Sorry. Here the class ArticoliVers0Java8.java

    import java.sql.*;
    
    // With next line the compiler gives an error: "package net.ucanaccess.jdbc does not exist
    import net.ucanaccess.jdbc.*;
    
    public class ArticoliVers0Java8 {
        public static void main (String args[]) {
            Connection con = null;
            Statement st = null;
            ResultSet rs = null;
            try {
                // Carichiamo un driver di tipo 1 (bridge jdbc-odbc)
                String driver = "net.ucanaccess.jdbc.UcanaccessDriver";
                Class.forName(driver); // ClassNotFoundException
                // Creiamo la stringa di connessione al database Articoli
                String url = "jdbc:ucanaccess://C:/Users/FrancoW/Java/ApplicazioniMie/Articoli/ProveParziali/Articoli.mdb";
                con = DriverManager.getConnection (url, "", "");
                // Creiamo un oggetto Statement per interrogare il db
                st = con.createStatement ();
                // Eseguiamo una query e immagazziniamone i risultati
                // in un oggetto ResultSet
                String qry = "SELECT * FROM tblArchivi";
                rs = st.executeQuery(qry);
                // Stampiamone i risultati riga per riga
                while (rs.next()) {
                    System.out.println(rs.getString("N")+ " " + rs.getString("Descrizione")+" ID = " + rs.getString("IDArchivio"));
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } finally {
                if (rs!=null) {
                    try {
                        rs.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                     }
                 }
                rs = null;
                if (st!=null) {
                    try {
                        st.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                     }
                 }
                st = null;
                if (con!=null) {
                    try {
                        con.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                     }
                 }
                con = null;
            }
        }
    }
    
     
  • Marco Amadei

    Marco Amadei - 2015-10-08

    Your issue doesn't seem to be ucanaccess related, but you should to understand how to add jars to a project classpath using your IDE or your startup script(.bat or .sh). We can obviously explain and teach this, but it's out of this forum's scope. Are you using an IDE or not? What's your IDE? Eclipse? NetBeans? Gord's article shows clearly how to do with both.

     

Log in to post a comment.