To connect to a database in Java using JDBC, drivers for JDBC need to be installed. Installation is not as straightforward as the C# ODBC counterpart so this guide will show you how to add JDBC drivers to your Java project and tell your application to use them.
The JDBC driver is a .jar file that must be bundled into your Java application library. Java also requires that the driver in the library is called by name. This article will go into detail showing you how to do this exactly, as the driver name is not always the same as it’s named in your files.
How to find the driver name that Java is looking for
Navigate to where your driver, that you would like to use, is located. This guide will use one from Connect Bridge:
Navigate here via command prompt. This can easily be done by typing cmd into the navigation bar whilst in the folder and hit enter
Now you should have cmd open to the folder structure that holds your driver .jar
You can now enter this command to list the names Java recognizes for the file:
jar tf *NAME OF YOUR DRIVER.jar* | findstr Driver
After clicking enter, you will see a list of results with possible driver names for that driver
Based on the results above, we can deduce that the name of our driver is
com.cnsconnect.mgw.jdbc.MgDriver
We can deduce this based on the typical naming conventions and by eliminating the unlikely classes such as MgDriver$1, MgDriver$KeepAliveTask as they either contain dollar signs suggesting inner classes or auxiliary tasks or have names suggesting other purposes like test.class and TrustedConnectionExample.class
Adding the driver to a Java project
Now that we have our name for the driver, we can add it to our project and point Java to it. In your IDE (Eclipse is used in this example) navigate to your project > right click it > click on properties
You will now see that the driver has been added to the project library
And just like that, your Java application will now use the driver to communicate to your database. Theoretically speaking, if you followed this short guide and you were making use of our Connect Bridge middleware, you would at this point be ready to start programmatically accessing data from countless Microsoft products, Salesforce, Quickbooks, SAP and many, many more.
More on Software Integration using Java
About the Author
“As a Solution Architect at Connecting Software, it's important to know all of the little intricacies of the tools that you use on a day to day basis. It helps me do what I do better and I thought, why not share some of these uncommon tidbits of information with all of you. If this ends up helping at least a single person, I reckon it was worth the writeup."