Home Search Contact us About us
Title Access a database from Java.
Summary Use JDBC to access ODBC data held in a MS Access database. This is done using the sun.jdbc.odbc.JdbcOdbcDriver. It demonstrates Read/Write and add operations.
Contributor John McTainsh
Published 23-Dec-2000
Last updated 23-Dec-2000
Page rating   87% for 22 votes Useless Brilliant

Description.

There are two phases to the development of a database connection in Java. The first is to set up a DSN and the second is to write some code. I will cover both these steps. Note that this is written for the only operating system I know Microsoft Windows. If any one has another way to do this for better cross platform control, I wold be happy to add your ideas to this article.

Setup the DSN

The following steps will setup a System DSN to a Microsoft Access database.

  1. Open the Windows Control Panel.
  2. If running Windows 2000 open the Administrative Tools item.
  3. Open the Data Sources (ODBC) item.
  4. Select the System DSN Tab.
  5. Select Add...
  6. Double click on Microsoft Access Driver (*.mdb).
  7. Enter a name for the data source, VisualCAccess.
  8. Press Select....
  9. Browse for the database to open.
  10. Press OK. That is it.

Write the code

Each of the following examples shows how to append, read and modify the data. To use each of these steps it is necessary to include the followinf import.

import java.sql.*;

Add a record

Adding a record is done using the SQL INSERT command.

try {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    //OR Class.forName("com.ms.jdbc.odbc.JdbcOdbcDriver");
            
    Connection conn = DriverManager.getConnection(
        "JDBC:ODBC:Driver={Microsoft Access Driver};"+
        "dsn=VisualCAccess;UID=;PWD=;" );
    
    Statement st = conn.createStatement();
    java.util.Date dNow = new java.util.Date();
    java.text.DateFormat dFormat = java.text.DateFormat.getDateTimeInstance();
    String sCommand = "INSERT INTO Hit " +
        "(Path, IPAddress, BrowserString, HitTime) " +
        "VALUES('AAA','10.1.1.1','Mosaic',"
        "'" + dFormat.format( dNow ) + "' );";
    st.executeUpdate( sCommand );
    st.close();
    conn.close();
} 
catch( Exception ex ) {
    String sErr = ex.toString();
}

Modifying a record

Changing a record is very simular using the SQL UPDATE command. Here record ID 11500 has its Wxyz set to 57, in the Hit table

try {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    //OR Class.forName("com.ms.jdbc.odbc.JdbcOdbcDriver");
            
    Connection conn = DriverManager.getConnection(
        "JDBC:ODBC:Driver={Microsoft Access Driver};"+
        "dsn=VisualCAccess;UID=;PWD=;" );
    
    Statement st = conn.createStatement();
    String sCommand = "UPDATE Hit SET Wxyz = 57 WHERE ID = 11500";
    st.executeUpdate( sCommand );
    st.close();
    conn.close();
} 
catch( Exception ex ) {
    String sErr = ex.toString();
}

Reading a record

Reading is a slightly different task. Here all records from the Hit database will be read and displayed.

try {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    //OR Class.forName("com.ms.jdbc.odbc.JdbcOdbcDriver");
            
    Connection conn = DriverManager.getConnection(
        "JDBC:ODBC:Driver={Microsoft Access Driver};"+
        "dsn=VisualCAccess;UID=;PWD=;" );

    Statement st = conn.createStatement();
    ResultSet rs = st.executeQuery( "SELECT * FROM Hit" );
    while( rs.next() ){
        String sPath = rs.getString( "Path" );
        String sIP   = rs.getString( "IPAddress" );
        String sBS   = rs.getString( "BrowserString" );
        String sHT   = rs.getString( "HitTime" );
        long nUserID = rs.getLong  ( "MemberID" );
        long nID     = rs.getLong  ( "ID" );
    }

   rs.close();
   st.close();
   conn.close();
} 
catch( Exception ex ) {
    String sErr = ex.toString();
}
Comments Date
20-Feb-2004 Saurabh
about Ms-access 20-Feb-2004 Saurabh
hello ,read ur note is good but i need to conncet java without dsn.
The best 100% 22-Sep-2004 Shimon
cannot make it 23-Oct-2004 mumbra99
sorry, im really cant make it, and still dont know why.
theres no error while compiling but theres no data inserted into the table.
13/2/2004 13-Feb-2005 mohamed
when i put name of any coulmn that is not in the table there is no error but when that is all right there is problem in the class JdbcOdbcDriver
(sun.jdbc.odbc.JdbcOdbcDriver)
i don`t have these class
Thank You! 18-Feb-2005 Robert Stout
I have been searching for this all day. You rock!
cannot work 23-Feb-2005 jyothi
sorry to say, i use the code written by U. There is no errors at compile and run time but data is not inserted in to the database. why i don`t know. Thank U
conncet java without dsn. 13-Apr-2005 conncet java without dsn.
try {
Class.forName(`sun.jdbc.odbc.JdbcOdbcDriver`);
//System.out.println(`----------1`);
connect =
DriverManager.getConnection(
`jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=c:\\downloads\\myDb.mdb`,
``,
``)
stmnt =
getConnection().createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
} catch (Exception eee) {
System.out.println(`Error in initiate DBconnection class: ` + eee);
};
Home Search Contact us About us