Question posted 2011 · +6 upvotes
I’m building a desktop app that needs to communicate with a MS Access database. Now, unless I want to register the DSN for the database on every computer that’s going to use the desktop app, I need a way to connect to the database in a DSN-less fashion.
I’ve searched alot and found some useful links on how to create connection strings and based on that I tried modifying my program based on that but without success. The code below fails. If i switch the string in the getConnection to “jdbc:odbc:sampleDB” it works, but that’s using DSN and not what I want to achieve.
How do I write and use a connection string in java to make a DSN-less connection to a MS Access database?
private Connection setupConnection() throws ClassNotFoundException,
SQLException {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("Driver={Microsoft Access Driver (*.mdb)} &_ Dbq=c:\as\sampleDB.mdb");
return con;
}
Addition: I’d also like to point out that if anyone has an idea of a way to achieve what I asked for WITH a DSN-connection I’ll gladly listen to it!
Accepted answer +5 upvotes
JDBC connection string shouls start with jdbc: like:
jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\Nwind.mdb
so try with:
Connection con = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=c:\as\sampleDB.mdb");
If you configure DSN then you can connect to it using simplier connect string: jdbc:odbc:[alias], example:
jdbc:odbc:northwind
3 code variants in this answer
- Variant 1 — 1 lines, starts with
jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\… - Variant 2 — 1 lines, starts with
Connection con = DriverManager.getConnection("jdbc:odbc:Dri… - Variant 3 — 1 lines, starts with
jdbc:odbc:northwind
Top ms-access Q&A (6)
- How can I modify a saved Microsoft Access 2007 or 2010 Import Specification? +31 (2008)
- OleDbCommand parameters order and priority +28 (2009)
- Is there an equivalent to the SUBSTRING function in MS Access SQL? +26 (2009)
- What do I need to read Microsoft Access databases using Python? +25 (2009)
- MS Access library for python +24 (2009)
- is there any replacement of Access? +21 (2009)
ms-access solutions on this site
.