Access Microsoft Access Database From Java Using JDBC-ODBC Bridge - Sample Code

By Angsuman Chakraborty, Gaea News Network
Tuesday, March 28, 2006

Previously I had provided the gist of how to access MS Access database from JDBC using JDBC-ODBC bridge. Today I will elaborate on that and provide you with two sample classes. The first - AccessJDBCUtil.java defines a method which provides you with a connection to the ms access database. The second - AccessJDBCTest.java uses the first to execute any given query (DDL & DML supported) on any ms access database and displays the result on console.

AccessJDBCUtil.java can be used as a utlity class for connecting to Access databases. The connection provides read/write access to the database. In other words you can execute queries as well as DDL supported by MS Access. It simplifies creation of the strange connection parameters.

The rest should be clearly explained by the self-explanatory code included below:

Discussion

Neeraj
July 8, 2010: 6:18 am

plz give example where u have used any of the following classes to help ur existing code:
AccessJDBCUtil.java
AccessJDBCTest.java

plz reply me on my email id if u can.

thankyou


Ani
June 27, 2010: 9:17 am

I need the steps that is how to connect jsp in access dadabase and sql database.


ashish
April 17, 2010: 2:47 am

how to store data in ms access using servlet please send me the code


VipinJeet Tripathi
April 14, 2010: 9:46 am

I have a problem while creating runtime DSN for SQL Server in JSP.Please suggest me.


Balaji
March 20, 2010: 12:39 am

I am using front end as jsp with html as presentation logic and using ms acess as my back end accepting the data from the acess it is working give me some examples programs regarding this application


deiveegan
March 19, 2010: 1:43 am

amoll try this code it will work properly
————————————–
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class NorthwindServlet extends HttpServlet
{
public static void main (String[] args)
{
System.out.println(doQuery());

}
public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
PrintWriter out=res.getWriter();
out.println(doQuery());

}
public static String doQuery()
{
StringBuffer buf=new StringBuffer();

try {
Class.forName(”sun.jdbc.odbc.JdbcOdbcDriver”);
Connection con=DriverManager.getConnection(”jdbc:odbc:Northwind”,” “,” “);
Statement st=con.createStatement();
String query=”SELECT * FROM Employees”;
ResultSet rs=st.executeQuery(query);
buf.append(”Empoyee Database”+ “\n” +”\n”);
while (rs.next())
{
buf.append(rs.getString(1)+” “+rs.getString(2)+” “+rs.getString(3)+” “+rs.getString(4)+” “+rs.getString(5)+”\n”);
}

con.close();
}
catch (ClassNotFoundException cnfe) {
buf.append(”Couldn’t find class file”+cnfe);
}
catch(SQLException sqle){
buf.append(”SQL Exception”+sqle);

}
return buf.toString();
}
}


adedeji balogun
March 16, 2010: 10:07 am

i need a code to search through a database(SQL Server) and display the results in a table.The string word will be searched in a separate frame.


Priyadharshini.V
February 19, 2010: 6:00 am

I need to know the sql command to count the number of attributes in Ms access database in java(using jdbc connection)

February 11, 2010: 4:32 am

how to use the SEO tool in java?or how to import SEO tool to Java?


Vishnu
December 8, 2009: 6:32 am

Hi i want to get the 750th record from the database using simple jdbc code


Amit
December 2, 2009: 5:29 am

How we can create MDB file through java programming.

November 10, 2009: 3:55 pm

HOW DO I INSERT AND DELETE DATE FROM DATABASE USING STATEMNET RS=ST.EXECUTEUPDATE()IN JAVA DEVELOPMENT


c gowtham
November 7, 2009: 1:36 am

I am doing a project in networks .. I want to know how
to connect microsoft access using java code..
can you tell me the code for connection establishment.


doc
October 21, 2009: 3:49 am

i am doing an assignment and i am required to import a database file to java and im nt sure of the code to use.can u plz help me out wth a simple program doing that plz.


rakesh patil
September 29, 2009: 6:33 am

i m not geting an ms access data base connection when i write a java program usig jdbc odbc bridge
dsn that will be created not found


senthil
September 14, 2009: 4:22 am

sir i need a code immed…..

to write a java program using applet swings. front end java back end ms access. i will give a input(usre name admin and password admin) to the applet wimdow then open to the another page. that page contain simple 3 buttons.


souvik pramanik
August 21, 2009: 9:39 am

I have a jsp form, from where i’ve taken a value like
String val1=request.getParameter(”fieldname”);
now I’ve converted string to integer
like
int val = Integer.parseInt(val1);
but the problem is I can not insert this int value to ms access database where the field is number type!

urgent…plz help me!
Thanks in advance


vinay
July 30, 2009: 11:01 am

I am trying to connect ms access database using servlet(with Tomcat Server) but getting problem in connection.
I got the Exception as below:

java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

What I do? Where I keep my database?

Code:

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;

public class check extends HttpServlet

{

Connection con;

Statement stmt;

ResultSet rst;

PrintWriter out;

public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException

{

try
{
out=res.getWriter();

Class.forName(”sun.jdbc.odbc.JdbcOdbcDriver”);
con=DriverManager.getConnection(”jdbc:odbc:logdata”);
stmt=con.createStatement();
rst=stmt.executeQuery(”select * from Employee”);
}
catch(Exception ae)
{
out.println(”Exception : “+ae);
}

try

{

out.println(”Servlet with jdbc”);
out.println(”Hello”);
while(rst.next())

{
String str1=(String)rst.getString(1);

String str2=(String)rst.getString(2);

out.println(”");

out.println(”"+str1+”");

out.println(”"+str2+”");

out.println(”");

}

out.println(”Bye”);
}

catch(Exception e)
{

out.println(”Exception : “+e);
}

}

}


regz
July 4, 2009: 1:44 pm

hi sir i’m doing a simple program on how to use the MS acces as a database but the interface is using a java,
can you give me a sample program how to connect access to java?


sharan
June 17, 2009: 10:01 am

hi..i am doing a project in J2ME…. how to connect to msaccess…. can anyone send me a sample code…


sri
March 30, 2009: 10:45 am

this is my program
import java.io.*;
import java.sql.*;
public class odbcaccessconnection
{
public static void main(String[] args)
{
Connection con = null;
try
{
Class.forName(”sun.jdbc.odbc.JdbcOdbcDriver”) ;

// Connect with a url string
con = DriverManager.getConnection(”jdbc:odbc:hy_access”);
System.out.println(”Connection ok.”);
con.close();

}
catch (Exception e)
{
System.err.println(”Exception: “+e.getMessage());
}
}
}
i have error that exception :no data found please clear it


rom
March 23, 2009: 3:05 pm

i just want to know, how to insert, delete, edit and search in java programming using DataBase via MS ACCESS


Stephane Duplessis
March 17, 2009: 4:16 pm

Connection issues between JSP and MS Access

Anybody know why I would be getting the following error: [Microsoft][ODBC Microsoft Access Driver] System resource exceeded.

The connection will work for a while … then we will start getting the above error. Rebooting the server hosting the JSP & MS Access DB will fix the issue for while.

Any suggestions welcome…

Here’s what we have tried

Attempt 1
***************

Attempt 2
**********************
Changed:
rs.close();
st.close();
cn.close();

To:
if (rs != null) rs.close();
if (st != null) st.close();
if (cn != null) cn.close();

March 16, 2009: 10:16 am

Are you using the JDBC-ODBC bridge driver?


nag
March 16, 2009: 9:10 am

hi sir, i have some problem in using the ms access databse with swings.
it throws driver not found exception.
can u come out with any solution.


sathish reddy
March 3, 2009: 5:00 pm

i m doing a project is (online shopping) using
java ..
how to create MS access database, table using java and how to insert data got from the user into the access table. and how to retrive the data into database pls help me…thanks in advance.

regards
sathishreddy
satsri.masri@gmail.com
9010465523

February 22, 2009: 8:11 pm

hi…

your site is very useful for newbiz thanks for providing a good reference material for students

thanks


manoj
February 22, 2009: 6:37 am

how to connect between java with MSAccess database with some simple example


Amol
February 20, 2009: 8:43 am

Hi i ve problem with this code when i run this servlet then it will display following err msage
Couldn’t find class filejava.lang.ClassNotFoundException: sun.jdbc.odbc.jdbcodbcDriver
so anyone can suggest me whats the problem?
……………………………………………..
i ve given code below
……………………………………………..
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class NorthwindServlet extends HttpServlet
{
public static void main (String[] args)
{
System.out.println(doQuery());

}
public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
PrintWriter out=res.getWriter();
out.println(doQuery());

}
public static String doQuery()
{
StringBuffer buf=new StringBuffer();

try {
Class.forName(”sun.jdbc.odbc.jdbcodbcDriver”);
Connection con=DriverManager.getConnection(”jdbc:odbc:Northwind”,” “,” “);
Statement st=con.createStatement();
String query=”SELECT * FROM Employee”;
ResultSet rs=st.executeQuery(query);
buf.append(”Empoyee Database”+ “\n” +”\n”);
while (rs.next())
{
buf.append(rs.getString(1)+” “+rs.getString(2)+” “+rs.getString(3)+” “+rs.getString(4)+” “+rs.getString(5)+”\n”);
}

con.close();
}
catch (ClassNotFoundException cnfe) {
buf.append(”Couldn’t find class file”+cnfe);
}
catch(SQLException sqle){
buf.append(”SQL Exception”+sqle);

}
return buf.toString();
}
}


hari
February 1, 2009: 7:42 am

hi sir

how to create a table in MS-ACCESS throw jdbc bridge driver

YOUR VIEW POINT
NAME : (REQUIRED)
MAIL : (REQUIRED)
will not be displayed
WEBSITE : (OPTIONAL)
YOUR
COMMENT :