6

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: enter image description here

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: enter image description here enter image description here

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?

Community
  • 1
  • 1
Mohru
  • 725
  • 1
  • 7
  • 17
  • Possible duplicate of [How to fix: "No suitable driver found for jdbc:mysql://localhost/dbname" error when using pools?](http://stackoverflow.com/questions/5556664/how-to-fix-no-suitable-driver-found-for-jdbcmysql-localhost-dbname-error-w) – N00b Pr0grammer Jan 16 '17 at 06:44
  • I don't think this is a duplicate. Firstly, I use different environment where configuration is slightly different. Secondly, I knew the solution proposed in that question, and asked if there was any other way to accomplish that. – Mohru Jan 16 '17 at 08:31
  • There is difference between configuring IntelliJ to be able to connect to MySQL, and using a database driver in your project. – Mark Rotteveel Jan 16 '17 at 12:48

1 Answers1

6

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:

  1. Manually add driver jar as library to the project (like you did)
  2. In code, use the driver class. Use Intellisense to import the JAR (screenshot below). 'add to classpath'.
  3. Same as above but for maven based project (screenshot below). 'Add maven dependency'
  4. Use Spring-boot framework, let it automatically figure out the driver for you.

enter image description here enter image description here

DeepakV
  • 2,420
  • 1
  • 16
  • 20
  • thanks for your answer. Does any of your solutions uses this loading drivers during DriverManager initialization that I've mentioned in my question? – Mohru Jan 16 '17 at 23:08
  • Doing DriverManager initialisation in code requires Class.forName("class_name_string"). Intellij will not load class because its a string. Thus, just for import, write the driver class name (with package) anywhere in your code, and then use intellij suggestions to import the JAR. Once imported, remove that class name code. – DeepakV Jan 17 '17 at 03:06