ClassNotFoundException com.mysql.jdbc.Driver fix in IntelliJ IDEA

When you encounter a java.lang.ClassNotFoundException com.mysql.jdbc.Driver error using a JDBC driver to connect to MySQL in IntelliJ IDEA, here’s your fix. If you want to cut straight to the chase, here’s the answer on StackOverflow. If you want pictures and a bit of elaboration, read on.

Here is the error in the IntelliJ Run Window.

We created a database source, so when we instantiate our JDBC Driver class with

Class.forName(“com.mysql.jdbc.Driver”);

we might think we should be good to go, but alas that is not the case.

The problem is that we haven’t yet added the java.jdbc.Driver class module as a project dependency. We start with File -> Project Structure.

Next we Attach Files or Directories. In the Linux Mint version of IntelliJ IDEA we do that with the “+” icon.

Add the MySQL Connector jar.

Our Java source code now is referencing the JDBC Connector module class…

…which means only one thing: Ja-zowwie!