When you write a database application using Java, Most of the times your first choice is MS-Access. There is a reason for it. That is the easiness of handling the database. But when you connect to an Access database, there's something which bother you every time. That is setting up the ODBC data source in the windows control panel. There could be times that you tried to write you own installer for you java database application, but couldn’t automate the entire process, due to the problem of manually configuring ODBC source.
But here, I’ll show you how to automate it.
When you connect to a database using Java, you call the following statement, which returns a connection to the specified database.
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
DriverManager.getConnection(DATABASE, USERNAME, PASSWORD);
Old Technique
If your database is an Access database, you parameter for DATABASE is something like this.
"jdbc:odbc:test";
Here, you have to go to control panel and set an ODBC data source called "test".
New Technique
In the way I’m going to tell you, you don’t have to set any ODBC data source. Just pass the following string as your DATABASE.
"jdbc:odbc:;DRIVER=Microsoft Access Driver (*.mdb);DBQ=C:\\testing folder\\test.mdb";
If the database is in your working directory, simply call following.
"jdbc:odbc:;DRIVER=Microsoft Access Driver (*.mdb);DBQ=test.mdb";
In this way, you can directly connect to the Access database you specified, without bothering about configuring ODBC.
That's how I do it.
/Cheers,
-MadGuy