Defining DSN on the fly in java

September 10, 2007 at 1:20 pm (Java, languages)

Talking about the traditional way of connecting to database involves.. Defining a driver making a dsn in the administrator console etc. Here is a short way in which you don’t need to worry about setting a dsn everytime project location gets shifted.

You can use the JDBC-ODBC but define your DSN “on the fly” by passing all ODBC configuration params inside the connect string:

// example for db test1.mdb, user dba, pw sql
con = DriverManager.getConnection(
"jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};
DBQ=C:/data/Access/test1.mdb","dba","sql");

Look out: 1 blank before (*.mdb) !!!
Don’t forget the Class.forName() before!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: