TechBookReport logo

JDBC-ODBC Bridge - A Quick Introduction



ODBC (Open Database Connectivity) is an API that provides programmatic access to data using SQL. ODBC adopts a layered approach, an application written to ODBC can talk to any underlying data source that has an ODBC driver. In the case of Microsoft Access ODBC support has been there almost from the beginning. In the Java world a similar architecture exists called JDBC (Java Database Connectivity), in which Java applications written to the API can talk to any data source that has a JDBC driver. Put these together in the form of a JDBC-ODBC bridge and you have a platform-independent mechanism for Java to talk to any DBMS that has JDBC or ODBC drivers.

Note that while ODBC was originally developed by Microsoft (based on earlier work by other organisations involved in Call Level Interfaces standards), it has been adopted by many other organisations and is no longer limited purely to the Windows platform. ODBC drivers are available for Oracle, DB2, Sybase, MySQL, Firebird, SQLite, PostgreSQL, Ingres and other DBMS products, as well as Microsoft Access and SQL Server.

If you are on a Windows platform you can use an ODBC Data Source to get into your DBMS. In the case of Access the first step to opening the database to ODBC is to create an ODBC Data Source Name for your database. The simplest method is to load the Data Source Administrator from within the Windows Administrator tools (via the Control Panel if you're not running a server version of Windows). A User DSN creates and registers a DSN for a single user, a System DSN is accessible to all users and a File DSN stores the DSN information to a file which can be shared among different users. In each case the DSN associates a user-defined label with a database driver (Microsoft Access Driver *.mdb) and a specific instance of the database. Once the DSN has been created it can be used by any program that uses the ODBC API to talk to the underlying database.

Alternatively you can make an explicit call in your code, with a link to the path of the database file (and .MDB file for Access). Though in this case you have to be aware of hard-coding file paths, so in practice you would seek to decouple your code by using a property (or a command-line argument for a console application).

In the case of Java the normal method for talking to a database is to use JDBC. First the database driver is registered, then a specific database URL is used to establish a connection which can be used to create the statement object. Once this has been done it's possible to interact with the database using SQL, stored procedures etc.

In the following code snippet we are going to connect to an Access database which has a DSN of MyAccessDB using Sun's JDBC-ODBC bridge driver

import java.sql.*;

public class AccessDAO {

	private Connection con;
	private Statement st;
    private static final String url="jdbc:odbc:MyAccessDB";
	private static final String className="sun.jdbc.odbc.JdbcOdbcDriver";
	private static final String user="";
	private static final String pass="";

	AccessDAO()throws Exception {


	Class.forName(className);
		con = DriverManager.getConnection(url, user, pass);
		st = con.createStatement(); 

		//do whatever database processing is required
	}
}

In the case where you do not use a DSN but want to embed the path, you use a url of the form:

String url = "jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=c:/path/to/MyAccessDB.mdb"

It's not just the user data that can be queried using the JDBC-ODBC bridge, the database metadata can also be accessed. The following program will report all of the SQL data types supported by the version of Access used by the database contained in the MyAccessDB DSN:

import java.sql.*;

public class AccessDAO {

	private Connection con;
	private Statement st;
	private static final String url="jdbc:odbc:MyAccessDB";
	private static final String className="sun.jdbc.odbc.JdbcOdbcDriver";
	private static final String user="";
	private static final String pass="";
		
	public AccessDAO()throws Exception {

		Class.forName(className);
		con = DriverManager.getConnection(url, user, pass);
 		DatabaseMetaData dbMetaData  = con.getMetaData();
		ResultSet dbTypes = dbMetaData.getTypeInfo();
		String typeName;
		System.out.println("Supported Database Types:");
		while (dbTypes.next()){
			typeName = dbTypes.getString("TYPE_NAME");
			System.out.println(typeName);
		}
		
	}
	
	public static void main(String[] args) throws Exception{
		new AccessDAO();
	}
	
}

Obviously once you have established a connection you are able to use the full power of JDBC to execute queries, make data updates, create prepared statements and much more. To find out more about what you can do with Java and JDBC take a look at some of the recommended books from our Java book recommendations.

Return to home page

Contents copyright of Pan Pantziarka. If you like it link it, don't lift it. No copying for commercial use allowed. Site © 2007.