dcsimg
Use JDBC for loading database without using DNS
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Sweet_Angel
Posted On:   Thursday, May 16, 2002 08:50 AM


I want to use a database with JDBC API. This database is located on my computer directory c:mydb.mdb
The problem is that i dont want to create ODBC source for this database and i want to load it as an ordinary file. I know its easier if i just create a datasource but the problem is that i have to configure all of the computers in my network and i dont want to. since a separate copy of database is residing on each computer , i just need to load it right off the disk and use it without even bothering to go to the control panel. is there any way to do this in java !!!(we could do the same thing in Visual basic)

You don't need to create a DSN for MS Access

Posted By:   Roger_Hand  
Posted On:   Friday, May 17, 2002 01:04 AM

Here's the Java code for making a JDBC DSN-less connection to an MS Access database using the JDBC-ODBC driver. Just change (or pass in) the db name:


public static Connection SetupConn_Access() {

// JDBC driver name
final String driverClassName = "sun.jdbc.odbc.JdbcOdbcDriver";

// JDBC subprotocol
final String subprotocol = "odbc";

final String driverName = "{Microsoft Access Driver (*.mdb)}";
String filenameMSAccessDB = "Test.mdb";
String pathMSAccessDB = "C:/path/to/database/";
String fullPathMSAccessDB = pathMSAccessDB + filenameMSAccessDB;

Connection Conn = null;
String strConnectURL = "jdbc:" + subprotocol
+ ":Driver=" + driverName + ";DBQ=" + fullPathMSAccessDB;

try {
Class.forName(driverClassName);

Conn = DriverManager.getConnection(strConnectURL);
}
catch (Exception e) { // This catches all load errors beside just not finding the driver class.
System.out.println("Cannot load driver "+driverName+" exception:"+e.toString());
return null; // No point in trying to connect now..
}

return Conn;
}
About | Sitemap | Contact