CONNECT MS-ACCESS DATABASE FROM JAVA

If you have any queries please leave a message here
Your Message
×


  1. Install Windows Operating System

  2. Install MS-Access

    NOTES - If you use 64 bit Operating System then you must install MS-Access 64 bit version.

  3. Install Java and Set Environment Variable

  4. Database Create Process -

    Control Panel → Administrative Tools → Data Sources (ODBC)

    In the ODBC Data Source Administrator dialog box select MS Access Database and then click on Add button.


    In the Create New Data Source dialog box select Microsoft Access Driver (*.mdb, *.accdb) and then click on Finish button.


    Enter the Data Source Name and Description and then click on Create button.


    Enter the database name you want to create in Database Name field.

    Select Drive and Directory where you want to store the created database and then click on Ok button.

    Click on Ok button in all the dialog box.


Example 1

// Write a program to create employee table
import java.sql.*;
import java.io.*;
import sun.jdbc.odbc.*;
public class CreateEmployeeTable
{
public void createOperation()
{
try
{
// MS-Access Database Connection
String url="jdbc:odbc:OfficeDatabase";
String user="";
String password="";
new JdbcOdbcDriver();
Connection con=DriverManager.getConnection(url,user,password);

// Query Execution
String query="create table employee (id char(10), name char(50), salary long, primary key (id))";
Statement stmt=con.createStatement();
stmt.executeUpdate(query);

// Close The Connection
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

NOTES - sun.jdbc.odbc.* package not present in JDK 8 and its above version.


Example 2

// Write a program to insert details of an employee
import java.sql.*;
import java.io.*;
import sun.jdbc.odbc.*;
public class InsertEmployeeDetails
{
public void insertOperation()throws IOException
{
// Input employee id, name and salary from the user
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the employee id ...");
String id=br.readLine();
System.out.println("Enter the employee name ...");
String name=br.readLine();
System.out.println("Enter the employee salary ...");
long salary=Long.parseLong(br.readLine());

try
{
String url="jdbc:odbc:OfficeDatabase";
String user="";
String password="";
new JdbcOdbcDriver();
Connection con=DriverManager.getConnection(url,user,password);
String query="insert into employee values('"+id+"','"+name+"',"+salary+")";
Statement stmt=con.createStatement();
stmt.executeUpdate(query);
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

Example 3

// Write a program to check whether an employee record present in the database or not
import java.sql.*;
import java.io.*;
import sun.jdbc.odbc.*;
public class ViewEmployeeDetails
{
public void selectOperation()throws IOException
{
// Input employee id from the user
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the employee id ...");
String id=br.readLine();
try
{
String url="jdbc:odbc:OfficeDatabase";
String user="";
String password="";
new JdbcOdbcDriver();
Connection con=DriverManager.getConnection(url,user,password);
String query="select * from employee";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(query);
int count=0;

// Check whether the given employee id present in the database or not
while(rs.next())
{
if(rs.getString("id").equals(id))
{
System.out.println(rs.getString("id"));
System.out.println(rs.getString("name"));
System.out.println(rs.getString("salary"));
count=1;
}
}
con.close();

if(count==0)
System.out.println("Employee details not present in the database");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}


ABOUT US

QuestionSolves.com is an educational website that helps worldwide students in solving computer education related queries.

Also, different software like Visual Studio, SQL Server, Oracle etc. are available to download in different versions.

Moreover, QuestionSolves.com provides solutions to your questions and assignments also.


MORE TOPIC


Windows Command

UNIX Command

IGNOU Assignment Solution

IGNOU Question Paper Solution

Solutions of Different Questions


WHAT WE DO


Website Devlopment

Training

Home Learning

Provide BCA, MCA Projects

Provide Assignment & Question Paper Solution


CONTACT US


Follow Us