I'm creating a web app using servlets and Tomcat in IntelliJ. I want to connect my application to a MySQL database with JDBC. I downloaded MySQL Workbench, connected to it, and created a database.
Now, in my IntelliJ project I followed this tutorial to add database connection, and my properties look like this:
My mysql driver version is 5.1.35, so it should be compatible with JDBC 4.0. As found out (mainly here) to connect with my database I should use just:
Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
However,when I try to connect I get No suitable driver
exception. Even when I add
Class.forName(DB_DRIVER).newInstance();
I end up with the same. It only works when I add manually jar to this driver in my libraries and artifacts:
Is it really the only way? If IntelliJ downloaded the jar automatically, shouldn't it also load it automatically when I try to connect? In DriverManager
documentation there is something like this: As part of its initialization, the DriverManager class will attempt to load the driver classes referenced in the "jdbc.drivers" system property.
Do I have to configure this property somehow?
Configure a JDBC driver from the existing connection ) that is in the lower-right part of the window. ) and select Custom JARs…. In the file browser, navigate to the JAR file of the JDBC driver, select it, and click OK. In the Class field, specify the value that you want to use for the driver .
IntelliJ IDEA uses JDBC drivers to interact with a database. You can download and use a driver from the IntelliJ IDEA driver repository or specify the driver that you store on your computer. To download and use the latest driver version, click the Download ver. N link.
Short answer - Yes, project driver jars need to be mentioned explicitly.
When you configure DB for Intellij (using its database panel), Intellij can download respective driver based on database type. This is similar to configuring MySQL Workbench. Its for development purposes.
When you use JDBC connection within your project, you need to tell Intellij somehow that you need corresponding driver. You can do it the following ways:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With