How to Connect Ms Access Database in Java Using UCanAccess

Welcome Readers, If you are trying really hard to connect your Java application with the MS Access database using the Eclipse and NetBeans IDE and still after numerous attempts you are failing to connect, then you have come to the right place. In this tutorial, we are going to learn about How to Connect MS Access Database in Java or Java 8 using ucanaccess But before moving ahead make sure that you have the latest version of Java in your system and if not then you can download it through this link.

To make it easier for you I have divided this ucanaccess tutorial into several sections and feel free to jump in the division as per your interest.

If you are also interested in Connecting MySQL database to Java, then you can refer to this article How to Connect MySQL Database in Java Using Eclipse.

Without wasting time let’s quickly dive into our tutorial “How to Connect MS Access Database in Java using the Eclipse and NetBeans IDE“.

How to Connect MS Access Database in Java

Creating an MS Access Database

Although you are free to use any of the Microsoft Access version that is convenient for you, I would recommend you to try out the latest one. To get the latest version of the Microsoft Office, you can head for the Official website of Microsft.

As of now let’s look through the steps involved in creating MS Access database.

  • Open Microsoft office Access (I am using Microsoft office access 2007).
  •  Create a New Blank Database as shown in the figure below.
How to connect MS access database in java-fig-1
How to connect MS access database in java-fig-1
  • Give it a name (For example StudentDatabase.accdb) and specify a location where you want to save it.
  • Now click on Create button to create the database.
How to connect MS access database in java-fig-2
How to connect MS access database in java-fig-2
  • As soon as you click Create button, Microsoft Access creates a default table named “Table1” with its default field “ID“.
  • Right-click on the Table and select Design View.
How to connect MS access database in java-fig-3
  • A Save As dialog box will appear, and it will ask you for to give it a name.
How to connect MS access database in java-fig-4
  • Provide a suitable name for your table and click OK button.
  • Now you can create fields for your table with their associated data types.(I am going to create two fields Username and City with their data types as Text).
How to connect MS access database in java-fig-5

Downloading UCanAccess JAR file

To connect our Java application with Microsoft Access Database, we need to add some JAR files to our program, and you can download them by clicking on the download link below.

 

Connecting MS Access Database in Java Using Eclipse

Step #1 -> Creating a Java Project in Eclipse

  • Open Eclipse IDE and click on the Java Project under the new section of File Menu (File>>New>>Java Project).
fig – 6
  • Now give a name to your project (AccessConnect in this example) and click on “Finish”.
fig – 7
  • Now right click on the project and create a new Java class (New>>Class).
fig – 8
  • Now give a name to your class(AccessConnectivity in this Example), tick mark on the public static void main(String[] args), and then click on the Finish button as shown in the figure below.
fig – 9

 

Step #2 -> Adding UCanAccess Jar Files to Java Program in Eclipse

  • To connect your java program with the MS Access database in Eclipse IDE, you need to include UCanAccess Jar files to the Eclipse.
  • I have given the download link of the zip file in the above.
  • Extract the zip archive and you will get the Jar files.
  • Right-click on the project, go to the properties section, select Java Build Path, and click on the Add External JARs button.
fig – 10
  • After clicking on the button, a pop-up window will appear to select and open the Jar files.
fig – 11
  • You can see the added Jar files as shown in the figure below. Now click on the Apply and Close button.
fig – 12

Step #3 -> Connecting Java Program with the MS Access Database

Since we have already finished adding Jar files, now we are ready to connect our Java program with MS Access Database. Let’s quickly take a look at the steps.

  • Load the UCanAccess Driver using forname method of class Class.
  • Now we have to establish a connection using DriverManager.getConnection(String URL) and it returns a Connection reference.
  • In String URL parameter you have to write like this “jdbc:ucanaccess://”+”Full path of the database”; 
  • SQL Exception might occur while connecting to the database, So you need to surround it with the try-catch block.
 

Connecting MS Access Database in Java Using NetBeans

Step #1 -> Creating a Java Project in NetBeans

  • Open NetBeans IDE and click on the New Project under the File Menu (File>>New Project).
fig – 13
  • Now select “Java” under the categories section and “Java Application” under the Projects section and then click on the Next button.
fig – 14
  • Now give a name to your project(AccessConnect in this Example), tick mark on the Create Main Class, and then click on the Finish button as shown in the figure below.
fig – 15

 

Step #2 -> Adding UCanAccess Jar Files to Java Program in NetBeans

  • To connect your Java program with the MS Access database in NetBeans IDE, you need to include UCanAccess Jar files to the NetBeans.
  • I have given the download link of the zip file in the above.
  • Extract the zip archive and you will get the Jar files.
  • Right click on the Libraries folder then select Add Jar/Folder.
fig – 16
  • After clicking on that, a pop-up window will appear to select and open the Jar files.
fig – 17
  • You can see the added Jar files inside the Libraries folder, as shown in the figure below.
fig – 18

Step #3 -> Connecting Java Program with the MS Access Database

Since we have already finished adding jar files, now we are ready to connect our Java program with MS Access Database. Let’s quickly take a look at the steps.

  • Load the UCanAccess Driver using forname method of class Class.
  • Now we have to establish a connection using DriverManager.getConnection(String URL) and it returns a Connection reference.
  • In String URL parameter you have to write like this “jdbc:ucanaccess://”+”Full path of the database”; 
  • SQL Exception might occur while connecting to the database, So you need to surround it with the try-catch block.

Inserting Data into Table

Right away, we saw how to connect the MS Access Database in Java Using NetBeans and Eclipse IDE, now its time to insert some data into our table. If you are familiar with the Prepared Statement in Java and about the SQL INSERT query, it will be a lot easier to insert data to your table.

  • Create a PreparedStatement object and use SQL INSERT query to add data to the table.
  • Specify the values for each parameter through the PreparedStatement object.
  • Execute query through the PreparedStatement object.

Note ->  I am using the coding example of Eclipse IDE for Inserting, Updating, Retrieving, and Deleting data from the table. You can use the same code in NetBeans too.

Updating Data in the Table

  • To update data in the table, we will use SQL UPDATE query.

Retrieving Data from Table

  • To retrieve data from the table, we will use SQL SELECT query.
  • In order to get the results, we will use Java ResultSet object.

Deleting Data from the Table

  • To delete data from the table, we will use SQL DELETE query.

Conclusion

Finally, in this tutorial of the UcanAccess example, we have concluded that it is a lot easier to connect Java with MS Access Database using the UcanAccess JAR files than applying the traditional methods.

So, guys, I am wrapping up this tutorial about the Java MS Access Database connection example. Feel free to drop us a comment if you find something difficult to understand.

People are also Reading…..

10 thoughts on “How to Connect Ms Access Database in Java Using UCanAccess”

    • Notepad se connect karne ke liye aapko jitne bhi jar files hain unko extract karke us folder mein daalna hoga jismein aapka java class file hai.

      Reply

Leave a Comment