1
votes

I running Java REST project (IDEA with Maven framework support), deploy successfully, but when I call service, I´m getting a strange SQLException: "SQL exception occured: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/hh_parser"

I have dependency in pom.xml:

<dependency>
     <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>8.0.7-dmr</version>
</dependency>

and mysql-connector-java added in WEB-INF/lib

and mysql-connector-java-8.0.7.jar added in e:\tomcat\lib\

but it does not help me!

If I comment on the use of the DB, the REST service works fine.

Who knows what to do? This is a dead end! Here are full of the same questions without answers.

2
Where do you deploy your application?? Does it work fine in your local env? - Jeeppp
I deploy it in IDEA+tomcat+mysql. Yes, everything works fine, except for connect to DB. - Anatoly Belov
How do you access the data source? Via JNDI lookup or do you create it directly? - fhossfel
I think directly: con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hh_parser","rootpwd", "123"); If I use this connect from public static void main() - it working! - Anatoly Belov

2 Answers

1
votes

This error occurs when you don't register your Driver with your application.

If you have written like this

con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hh_‌​parser","rootpwd", "123");

try the below

Class.forName("path.to.your.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hh_‌​parser","rootpwd", "123");

or

DriverManager.registerDriver(new com.mysql.jdbc.Driver ());
0
votes

Considering that you are using the correct version of the MySQL Connector/J jar, you need to add the jar both to your CLASSPATH environment variable and also to TOMCAT_HOME\lib folder.

As per the documentation available here, you need to have these settings done so as to work with MySQL JDBC connectivity.

You can set the CLASSPATH environment variable under Unix, Linux, or OS X either locally for a user within the user's .profile, .login or other login file, or you can also set it globally by editing the global /etc/profile file.

For example, add the Connector/J driver to your CLASSPATH using one of the following forms, depending on your command shell:

  # Bourne-compatible shell (sh, ksh, bash, zsh):
    shell> export CLASSPATH=/path/mysql-connector-java-ver-bin.jar:$CLASSPATH

  # C shell (csh, tcsh):
    shell> setenv CLASSPATH /path/mysql-connector-java-ver-bin.jar:$CLASSPATH

For Windows platforms, you set the environment variable through the System Control Panel. To use MySQL Connector/J with an application server such as GlassFish, Tomcat, or JBoss, read your vendor's documentation for more information on how to configure third-party class libraries, as most application servers ignore the CLASSPATH environment variable.

If you are developing servlets or JSPs, and your application server is J2EE-compliant, you can put the driver's .jar file in the WEB-INF/lib subdirectory of your webapp, as this is a standard location for third party class libraries in J2EE web applications.