Connecting to Access database in JAVA
underfloor heating systems
Hi there,
I started a project for school that I must develop in JAVA. It’s a library system that uses Access databases for data and settings storage. I’m not going into any more details about the analysis of the problem but it’s obvious that I needed to setup a connection to the database.
In this small post I thought I’ll show you how I did that.
Below is just the snippet that does the connection:
try { Class.forName("sun.jdbc.odbc.JodbcOdbcDriver"); String filename = "filepath/to/the/database.mdb"; String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="; database += filename.trim() + ";DriverID=22;READONLY=false"; Connection con = DriverManager.getConnection(database, "", ""); } catch (ClassNotFoundException cnfex) { JOptionPane.showMessageDialog(null, cnfex); System.exit(0); } catch (SQLException sqlex) { JOptionPane.showMessageDialog(null, sqlex); System.exit(0); }
I used this way because it allows me to set the filepath of the mdb file and I don’t need to set it in the Data Sources (ODBC) in Windows.
If you like to set the database in the Data Sources in windows, just use this try instead of the one above:
try { Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection ("jdbc:odbc:DBName"); }
Hope you find this useful.
Links
- floor paints
- Online Schuhe von Snipe im Shop bestellen



One Response
to “Connecting to Access database in JAVA”




[...] a previous post, Connecting to Access database in JAVA, I’ve shared a little snippet of code with you that showed how to connect to an Access [...]