To connect MS Access database using java we should use JDBC drivers.
We should follow below given steps to connect MS Access database using JDBC.
Step #1: Create MS Access database.
Open Microsoft Office Access from the All programs. Open MSAccess then select 'Blank Database',enter the file name as "AccessDB" then click Create button.
Step #2: Create a table in the database File.
Here we are not retrieving data using our program but we should have a Access database to run our program. Create a table named 'Employee' and save it in "F:\JDBC " folder.
Step #3: Create a Data Source Name for MSAccess.
Here we are using JDBC Type I driver(sun.jdbc.odbc.JdbcOdbcDriver). Here we given Driver Connection as "jdbc:odbc:AccessDSN" to connect MS Access Database. AccessDSN is the Data Source Name as shown above.
Output:
we have a package here com.javabynataraj so you should use the below command while compiling and running the program.
We should follow below given steps to connect MS Access database using JDBC.
- Create MS Access database file
- Create a table in the database
- Create a Data Source Name using Microsot Access Driver
- Write a java program to connect MS Access database
Step #1: Create MS Access database.
Open Microsoft Office Access from the All programs. Open MSAccess then select 'Blank Database',enter the file name as "AccessDB" then click Create button.
Step #2: Create a table in the database File.
Here we are not retrieving data using our program but we should have a Access database to run our program. Create a table named 'Employee' and save it in "F:\JDBC " folder.
Step #3: Create a Data Source Name for MSAccess.
Open Control Panel and select Administrative tools then select Data Sources (ODBC).
Click on Add button to select Microsoft Access Driver
Select *.mdb, *.accdb driver for our datasource
Enter the Data Source Name(dsn) as 'AccessDSN ', then select Database file 'AccessDB' from F:\JDBC folder. We should select a database to Access driver.
then click on ok button.
Step: #4: Write a java program to connect MS Access database.Here we are using JDBC Type I driver(sun.jdbc.odbc.JdbcOdbcDriver). Here we given Driver Connection as "jdbc:odbc:AccessDSN" to connect MS Access Database. AccessDSN is the Data Source Name as shown above.
package com.javabynataraj; import java.sql.Connection; import java.sql.DriverManager; //javabynataraj.blogspot.com public class MSAccessConnTest { public static void main(String[] args) { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //Load Driver System.out.println("---Driver Loaded---"); Connection con = DriverManager.getConnection("jdbc:odbc:AccessDSN"); //Create Connection with Data Source Name : AccessDSN System.out.println("---Connection Established---"); } catch (Exception e) { System.out.println("Exception : "+e); } } }
Output:
we have a package here com.javabynataraj so you should use the below command while compiling and running the program.
Successfully we have connected to MSAccess database using JDBC driver.
Reference Books: