File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "database" Watch "database" New topic


vani geetha

Joined: Jan 18, 2008
Posts: 2
how to connect MS-Access database in java. I have windows_xp.but jdbc odbc driver didnot connect properly in my system please give the suggestion in my problem.
Jan Cumps

Joined: Dec 20, 2006
Posts: 2565

jdbc odbc driver didnot connect properly
It should work. Can you please tell us what you tried, and how it fails?

Regards, Jan

OCUP UML fundamental and ITIL foundation
Lukasz Bajzel

Joined: Dec 03, 2007
Posts: 26
Creating a database connection using JDBC involves 3 main steps.
1. Loading the database driver
2. Getting the connection using the driver manager
3. Using the connection to access the underlying database.

The sample code below explains the steps in detail.

import java.sql.*;

Connection conn = null;
Statement stmt = null;
ResultSet rs = null;

//Load the database driver class
Class.forName("your databse driver name");
//Example, mySql driver is com.mysql.jdbc.Driver

//Get the connection
conn = DriverManager.getConnection( "dburl","user","password") ;

//Create a Statement object.
stmt = conn.createStatement();

//Get a ResultSet

rs = stmt.executeQuery("your query here");

//Loop through the results
//get to each column in a row.
//for example to get a value from a column named 'user'
String userName = rs.getString("user");
}catch( Throwable t){
//handle error here

//note the sequence of closing the objects. rs first followed
// by stmt and connection objects
if(rs != null) rs.close();
}catch(Throwable t){t.printStackTrace();}

if(stmt != null) stmt.close();
}catch(Throwable t){t.printStackTrace();}

if(con != null) con.close();
}catch(Throwable t){t.printStackTrace();}

Hope this helps!

Your friends at - The one stop resource for all your Java questions and answers.
Pankaja Shinde
Ranch Hand

Joined: Sep 15, 2006
Posts: 87
I will tell you step by step procedure of how to create table in MS Access and example of JDBC using java

First create table in Access as

Microsoft Access-File-New-Blank database-Enter Filename as mydb1-Select path to store-OK-Double Click Create table in design view-Enter EmpID as field name and data type as number & enter EmpName as field name and data type as text-Right click first row and click primary key-Save as EmpTable-OK - close

Ereation of EmpDSN
Control panel-Administrative tools-Data sources (ODBC)-click MS Access DataBase-Add-select microsoft access driver (*.mdb)-Finish-Enter data Source name as EmpDSN-Select mydb1.mdb from location where you stored - OK.

Now check this java file to access database

// file name

import java.sql.*;

public class myEmployee
public static void main(String args[]) throws SQLException
Connection con;
Statement stmt;
String query = "select * from EmpTable";
String updateString = "INSERT INTO EmpTable VALUES (4,'pankaj')";
catch (ClassNotFoundException e)
System.out.print("ClassNotFoundException: " + e);
con = DriverManager.getConnection("jdbcdbc:EmpDSN");
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
System.out.println("Employee Details : ");
while (
int empID = rs.getInt(1);
String empName = rs.getString(2);
System.out.println(empID + " " + empName);

Now compile and run this file. And check EmpTable in database Mydb1. You will see values are inserted in table.
Remember when you run this example second time first delete inserted record from EmpTable, and run example.
For details check tutorial on JDBC.

Pankaj Shinde

[ January 19, 2008: Message edited by: Pankaja Shinde ]
[ January 19, 2008: Message edited by: Pankaja Shinde ]
I agree. Here's the link:
subject: database
jQuery in Action, 3rd edition