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.
- 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.
- 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.
- A Save As dialog box will appear, and it will ask you for to give it a name.
- 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).
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).
- Now give a name to your project (AccessConnect in this example) and click on “Finish”.
- Now right click on the project and create a new Java class (New>>Class).
- 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.
1 2 3 4 5 6 7 8 9 10 |
//Creating Class public class AccessConnectivity { //Creating main method public static void main(String[] args) { } } |
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.
- After clicking on the button, a pop-up window will appear to select and open the Jar files.
- You can see the added Jar files as shown in the figure below. Now click on the Apply and Close button.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.sql.Connection; import java.sql.DriverManager; public class AccessConnectivity { public static void main(String[] args){ try{ Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");//Loading Driver Connection connection= DriverManager.getConnection("jdbc:ucanaccess://E:\\StudentDatabase.accdb");//Establishing Connection System.out.println("Connected Successfully"); }catch(Exception e){ System.out.println("Error in connection"); } } } |
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).
- Now select “Java” under the categories section and “Java Application” under the Projects section and then click on the Next button.
- 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.
1 2 3 4 5 6 7 8 9 10 11 12 |
package accessconnect; //Creating Class public class AccessConnect { //Creating main method public static void main(String[] args) { } } |
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.
- After clicking on that, a pop-up window will appear to select and open the Jar files.
- You can see the added Jar files inside the Libraries folder, as shown in the figure below.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package accessconnect; import java.sql.Connection; import java.sql.DriverManager; public class AccessConnect { public static void main(String[] args) { try{ Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");//Loading Driver Connection connection= DriverManager.getConnection("jdbc:ucanaccess://E:\\StudentDatabase.accdb");//Establishing Connection System.out.println("Connected Successfully"); }catch(Exception e){ System.out.println("Error in connection"); } } } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; public class AccessConnectivity { public static void main(String[] args){ try{ Class.forName("net.ucanaccess.jdbc.UcanaccessDriver"); Connection connection= DriverManager.getConnection("jdbc:ucanaccess://E:\\StudentDatabase.accdb"); System.out.println("Connected Successfully"); //Crating PreparedStatement object PreparedStatement preparedStatement=connection.prepareStatement("insert into Student values(?,?)"); //Setting values for Each Parameter preparedStatement.setString(1,"Mehtab"); preparedStatement.setString(2,"Ranchi"); //Executing Query preparedStatement.executeUpdate(); System.out.println("data inserted successfully"); }catch(Exception e){ System.out.println("Error in connection"); } } } |
- You could also use Java Statement object to insert data into your table.
Updating Data in the Table
- To update data in the table, we will use SQL UPDATE query.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; public class AccessConnectivity { public static void main(String[] args){ try{ Class.forName("net.ucanaccess.jdbc.UcanaccessDriver"); Connection connection= DriverManager.getConnection("jdbc:ucanaccess://E:\\StudentDatabase.accdb"); System.out.println("Connected Successfully"); //Using SQL UPDATE query to update data in the table PreparedStatement preparedStatement=connection.prepareStatement("update Student set City=? where Username=?"); preparedStatement.setString(1,"Kanke"); preparedStatement.setString(2,"Mehtab"); preparedStatement.executeUpdate(); System.out.println("data updated successfully"); }catch(Exception e){ System.out.println("Error in connection"); } } } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; public class AccessConnectivity { public static void main(String[] args){ try{ Class.forName("net.ucanaccess.jdbc.UcanaccessDriver"); Connection connection= DriverManager.getConnection("jdbc:ucanaccess://E:\\StudentDatabase.accdb"); System.out.println("Connected Successfully"); //Using SQL SELECT Query PreparedStatement preparedStatement=connection.prepareStatement("select * from Student"); //Creaing Java ResultSet object ResultSet resultSet=preparedStatement.executeQuery(); while(resultSet.next()){ String Username=resultSet.getString("Username"); String City=resultSet.getString("City"); //Printing Results System.out.println(Username+" "+City); } }catch(Exception e){ System.out.println("Error in connection"); } } } |
Deleting Data from the Table
- To delete data from the table, we will use SQL DELETE query.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; public class AccessConnectivity { public static void main(String[] args){ try{ Class.forName("net.ucanaccess.jdbc.UcanaccessDriver"); Connection connection= DriverManager.getConnection("jdbc:ucanaccess://E:\\StudentDatabase.accdb"); System.out.println("Connected Successfully"); //Using SQL DELETE query PreparedStatement preparedStatement=connection.prepareStatement("delete from Student where Username=?"); preparedStatement.setString(1,"Mehtab"); preparedStatement.executeUpdate(); System.out.println("Data deleted Successfully"); }catch(Exception e){ System.out.println("Error in connection"); } } } |
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…..
- Menu Driven Program in Java Using Switch Case
- How to Create Calculator in Java Swing
- How to Create Tic Tac Toe Game in Java
- How to Create Login Form in Java Swing
- Registration Form In Java with Database Connectivity
- How to Create Splash Screen In Java
- How to Create Mp3 Player in Java
- How to Connect MySQL Database in Java Using NetBeans
- 11 Best site to Learn Java Online for Free
Thanks. Good one to learn java topics.
i downloaded the zip file but it does not contain any jar file just have one folder that’s all
Sorry for your inconvenience, You can check the post again.
i am getting error in connection? Please help
aur clearly btaye sir. notepad me kaise krnge
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.
Sir, JAR files are not available.
Sorry for your inconvenience, You can check the post again.
Very good and educational.
Thank You So much