Hello fellas! In this article, I’ll explain what’s JDBC and why it’s used. Why sometimes JDBC fails to connect with MS Access (.accdb). And how UCanAccess solves this problem. Last because least important, how to successfully compile and run the program from the terminal.
What’s JDBC?
Java Database Connectivity(JDBC) is a standard API used in Java to connect and interact with databases. It allows Java programs to execute SQL queries, retrieve data, and update databases regardless of the database type _ as long as a suitable driver is available.
So, JDBC provides: –
- Database connections
- Query execution
- Result processing
Typical JDBC workflow: –
- Load database driver
- Establish a connection
- Execute SQL queries
- Process results
- Close the connection
Why JDBC Needs a Driver?
JDBC doesn’t know how to talk to databases like MySQL, PostgreSQL, MS Access, and Oracle etc. Each database requires a driver which acts as a translator between the Java and the database engine.
Now, here comes the real problem with the accurate solution. 🙂
Why MS Access (.accdb) Is a Problem?
MS Access databases primarily exists in two formats: .mdb and .accdb. But the real issue isn’t the database file formats, but rather the Java version being used.
Earlier versions (up to Java 7) had a built-in component called JDBC-ODBC Bridge. This bridge allowed the Java applications to communicate with databases through ODBC drivers. Since, Microsoft provides ODBC drivers for MS Access, connecting Java to .mdb and .accdb wasn’t an issue. However, this JDBC-ODBC driver was outright removed for some performance and security concerns later.
That’s why Java 8 or later versions were failing to use ODBC-based connectivity, even if the database and ODBC were correctly installed.
Therefore, we need a 3rd party solution and this is UCanAccess driver.
Why UCanAccess?
UCanAccess is an open-source JDBC driver that allows Java to connect to .mdband .accdb files.
Advantages: –
- No ODBC configuration required
- Works with modern Java versions
- Easy setup
- Actively maintained
This is the reason that makes it suitable option for MS Access with Java. Make sure you download this driver and extract the file contents. At the moment, I’m writing this, I had the file named UCanAccess-5.0.1.bin downloaded.
I’ve the following hierarchy in my VS code: –
JavaAccessTemplate
├── bin
├── lib
│ ├── ucanaccess-5.0.1.jar
│ ├── commons-lang3-3.8.1.jar
│ ├── commons-logging-1.2.jar
│ ├── hsqldb-2.5.0.jar
│ └── jackcess-3.0.1.jar
├── src
│ └── com
│ └── myproject
│ └── JdbcEx.java
└── .vscode
└── settings.json
binfolder will have.classbytecode files that.javafile will create. It’s for managerial purposes to keep working place tidy.libfolder = All the*.jarfiles extracted fromUCanAccess-5.0.1.bin.- Created
srcfolder havingcomandmyprojectsub-folders. And placed java file inside thismyproject. - Then
.vscodewith the filesettings.json.
you can have .java directly inside src. For sake of learning, it’s done this way. So that we can know if our hierarchy for .java says: –
`├── src
│ └── com
│ └── myproject
│ └── JdbcEx.java
Then, never forget to add package com.myproject at the top of .java program. Otherwise, VS-code won’t know where to look for .java file if you’re running command inside JavaAccessTerminal directory.
Add the following code inside .vscode/settings.json: –
{
"java.project.sourcePaths": ["src"],
"java.project.outputPath": "bin",
"java.project.referencedLibraries": [
"lib/*/.jar"
]
}
Above steps allow: –
- Correct source folder detection.
- Compiled
.classfiles go intobin. - External libraries load correctly.
Compiling and Running from Terminal
Normal commands for running java program from inside G:\codes\Java\JavaAccessTemplate: –
javac src/com/myproject/JdbcEx.java
java src.com.myproject.JdbcEx
But for database and above setup we made: –
javac -d bin -cp "lib/*" src/com/myproject/JdbcEx.java
java -cp "bin;lib/*" com.myproject.JdbcEx
Conclusion: –
Using MS Access with Java is absolutely possible — but it requires:
- The correct driver (UCanAccess)
- Proper folder structure
- Correct classpath usage
- Exact column naming
