Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Using MS Access with JDBC-ODBC but without DSN

843854Apr 5 2004 — edited Apr 5 2004
I like to use a JDBC-ODBC bridge to access a MS Access file without having to define the bridge with a DSN entry in the Windows registry. I've found a page that give instructions on how to do this (http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=2691&lngWId=2#SECTION1), but I can't get it to work and the page even says it doesn't for some Windows machines. The error I get is:

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

My connection code is listed below.

If I add a DSN entry to my local machine and use the following line instead of the definition of the "database" variable below, it works. Problem is that my software can't require the user to setup a DSN entry by hand. I'd need to do it automatically. Is there a way to do it automatically?
String database = "jdbc:odbc:SoilProjectTablesV97new";

Is there something wrong with my code below?
Also, is this possible to do this in a reliable way?

-Leslie

=====================
Connection Code:
=====================

try {
System.setProperty("jdbc.drivers","sun.jdbc.odbc.JdbcOdbcDriver");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

DriverManager.setLogStream(System.out);
// set this to a MS Access DB you have on your machine
String filename =
"H:/codebase/spDatabaseV2000.mdb";
String database = "jdbc:odbc:Driver={Microsoft Access Driver(*.mdb)};DBQ=";
database+= filename.trim() + ";DriverID=22;READONLY=true"; //add on to the end

// now we can get the connection from the DriverManager
con = DriverManager.getConnection(database);

//Check for, and display and warnings generated
// by the connect.
checkForWarning (con.getWarnings ());

// Get the DatabaseMetaData object and display

// some information about the connection

DatabaseMetaData dma = con.getMetaData ();

System.out.println("\nConnected to " + dma.getURL());
System.out.println("Driver " + dma.getDriverName());
System.out.println("Version " + dma.getDriverVersion());
System.out.println("");


} catch (Exception e){
e.printStackTrace();
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 3 2004
Added on Apr 5 2004
4 comments
342 views