MsAccess Database Connectivity in Java
This blog will help you to connect your java program with Ms Access Database.
Step 1: First we required drives which we can use to connect our database with Java. So in this example we will use ucanaccess (Download - http://ucanaccess.sourceforge.net/site.html) driver for database connectivity.
Step 2: As you can see in above figure you should find all files in the folder which you have downloaded from the url which is given in Step1. Path is(UCanAccess-2.0.9.5-bin\UCanAccess-2.0.9.5-bin\lib)
Step 3: Now go to Netbeans.
File->New Project->Java->Java Application
Step 4: Now we need to add JAR file to our newly created project.
Go to Libraries->Add JAR/Folder and select all the files exists in lib and ucanaccess-2.0.9.5.
Step 5: Next steps is to create one database in your local drive. As i have created in D:\\D Drive\\data/Player.accdb.
Now add following code in Java_DB_MsAccess.java file
package java_db_msaccess;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Java_DB_MsAccess {
public static void main(String[] args) {
// variables
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
// Step 1: Loading or registering Oracle JDBC driver class
try {
Class.forName("net.ucanaccess.jdbc."+ "UcanaccessDriver");
}
catch(ClassNotFoundException cnfex) {
System.out.println("Problem in loading or "+ "registering MS Access JDBC driver");
cnfex.printStackTrace();
}
// Step 2: Opening database connection
try {
String msAccDB = "D:\\D Drive\\data/Player.accdb";
String dbURL = "jdbc:ucanaccess://" + msAccDB;
// Step 2.A: Create and get connection using DriverManager class
connection = DriverManager.getConnection(dbURL);
// Step 2.B: Creating JDBC Statement
statement = connection.createStatement();
// Step 2.C: Executing SQL & retrieve data into ResultSet
resultSet = statement.executeQuery("SELECT * FROM "+ "PLAYER");
System.out.println("ID\tName\t\t\tAge\tMatches");
System.out.println("==\t================\t===\t=======");
// processing returned data and printing into console
while(resultSet.next()) {
System.out.println(resultSet.getInt(1) + "\t" +
resultSet.getString(2) + "\t" +
resultSet.getString(3) + "\t" +
resultSet.getString(4));
}
}
catch(SQLException sqlex){
sqlex.printStackTrace();
}
finally {
// Step 3: Closing database connection
try {
if(null != connection) {
// cleanup resources, once after processing
resultSet.close();
statement.close();
// and then finally close connection
connection.close();
}
}
catch (SQLException sqlex) {
sqlex.printStackTrace();
}
}
}
}
Step 6: Now Run the Program
Step 1: First we required drives which we can use to connect our database with Java. So in this example we will use ucanaccess (Download - http://ucanaccess.sourceforge.net/site.html) driver for database connectivity.
Step 2: As you can see in above figure you should find all files in the folder which you have downloaded from the url which is given in Step1. Path is(UCanAccess-2.0.9.5-bin\UCanAccess-2.0.9.5-bin\lib)
Step 3: Now go to Netbeans.
File->New Project->Java->Java Application
Step 4: Now we need to add JAR file to our newly created project.
Go to Libraries->Add JAR/Folder and select all the files exists in lib and ucanaccess-2.0.9.5.
Step 5: Next steps is to create one database in your local drive. As i have created in D:\\D Drive\\data/Player.accdb.
Now add following code in Java_DB_MsAccess.java file
package java_db_msaccess;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Java_DB_MsAccess {
public static void main(String[] args) {
// variables
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
// Step 1: Loading or registering Oracle JDBC driver class
try {
Class.forName("net.ucanaccess.jdbc."+ "UcanaccessDriver");
}
catch(ClassNotFoundException cnfex) {
System.out.println("Problem in loading or "+ "registering MS Access JDBC driver");
cnfex.printStackTrace();
}
// Step 2: Opening database connection
try {
String msAccDB = "D:\\D Drive\\data/Player.accdb";
String dbURL = "jdbc:ucanaccess://" + msAccDB;
// Step 2.A: Create and get connection using DriverManager class
connection = DriverManager.getConnection(dbURL);
// Step 2.B: Creating JDBC Statement
statement = connection.createStatement();
// Step 2.C: Executing SQL & retrieve data into ResultSet
resultSet = statement.executeQuery("SELECT * FROM "+ "PLAYER");
System.out.println("ID\tName\t\t\tAge\tMatches");
System.out.println("==\t================\t===\t=======");
// processing returned data and printing into console
while(resultSet.next()) {
System.out.println(resultSet.getInt(1) + "\t" +
resultSet.getString(2) + "\t" +
resultSet.getString(3) + "\t" +
resultSet.getString(4));
}
}
catch(SQLException sqlex){
sqlex.printStackTrace();
}
finally {
// Step 3: Closing database connection
try {
if(null != connection) {
// cleanup resources, once after processing
resultSet.close();
statement.close();
// and then finally close connection
connection.close();
}
}
catch (SQLException sqlex) {
sqlex.printStackTrace();
}
}
}
}
Step 6: Now Run the Program
Have a Happy Learning
Regards
Digvijaysinh Virpura
Comments
Post a Comment