The first layer we will set to tackle is the bottom-most, the JDBC Drivers. These are third-party files that allow a specific database to extend JDBC classes and interfaces so that they work with that database. While SQL is the industry standard for relational database communication, its implementation is not standardized across all databases. Some databases only support certain SQL statements, some format their SQL differently, and some still may rely on SQL and some other programming language to implement their features. This variation among database providers means that each database product will have its own drivers, similar to each printer manufacturer having its own drivers. JDBC drivers are typically maintained in a repository service like Maven.

Once downloaded, the driver needs to be added to the classpath of your program. The classpath is the location where the JVM looks for .class files for your application when the program is run. Many IDEs have a process to do this automatically, but we will stick to adding the classpath variable through the command line interface manually.

We’ll also be using SQLite and the SQLite JDBC Driver as our backend database. Once it is downloaded, the next step is to register the driver with the DriverManager class. This class is responsible for managing all the connected JDBC drivers and ensuring the necessary files are available for a successful connection. Since the 2006 release of JDBC 4.0, any driver that is placed as a classpath variable is automatically registered but since we are adding the classpath manually, it will be good practice to run through the steps to verify the driver was loaded properly. We do this by checking to see if the class exists in the program build (this occurs at runtime, not compile time) using the Class.forName("Class Name") method.

Let’s go ahead and get started! In the IDE on the right, you will see that we have already uploaded a copy of the latest SQLite driver (the .jar file) and have created a project folder that contains the file ConnectionTest.java.



Inside ConnectionTest.java, you will see an empty class, and if you open the file navigator, you can see the .jar file of the SQLite Driver and the project folder where ConnectionTest.java lives. Inside ConnectionTest, create an empty main method.


Inside the main method, call the static method, .forName(), of the Class class. This method takes a String argument, the name of the class we are looking for. In this case, we are looking for a class inside the .jar called "org.sqlite.JDBC".


Since there is a chance this method doesn’t find the class, it throws a ClassNotFoundException, for this reason, wrap the method in a try-catch block:

  • Put the .forName() method call in the try block.
  • Catch the ClassNotFoundException as e in the catch block.
    • Inside the catch block, call the .printStackTrace() method of the error.

Inside the try block, after the .forName() method, add a .println() statement that says, "The driver was successfully loaded.". In the catch block, before the stack trace, add a .println() statement for when the driver isn’t located, "The driver class was not found in the program files at runtime.". This will execute when we have either forgotten to include the classpath variable or accidentally pointed it to the wrong location.


After you print the stack trace in the catch block, make a call to System.exit(1). There is no point in continuing the program if it can’t connect to the database.


Compile and run your program. Remember, you will need to pass in the classpath variable through the command line that points to your driver. You also need to point it to your current folder. We’ll provide the syntax since this is new, but you can read more in the Java Documentation.

  1. Navigate, using the terminal, to the project folder. Use the command cd to change directories in the terminal.
  2. javac ConnectionTest.java
  3. java -classpath .:../sqlite-jdbc- ConnectionTest

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?