1

I have some trouble connecting my remote mysql database from my rest web service running on Tomcat. I know this is common issue but I tried every way that I found on the internet. Error is: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver in here: Class.forName("com.mysql.jdbc.Driver");

I am using Intellij, what I tried is, I added "mysql-connector-java-5.1.46-bin.jar" and "mysql-connector-java-5.1.46.jar" to my project as library in project structure tab of IntelliJ and I also put these files to resources folder which I marked as resources file from project structure tab. I can see these to jar file in external libraries. I added these to jar file to .classpath file and in the artifacts tab of project structure I added them as library and also as extracted directory.

But no result, same error again. Where am I doing wrong?

Edit: I am adding whole related code. I realized that if I try to run this code below in an other class that not related with rest web service and tomcat, it works. But when I try it on tomcat, It does not work.

try{
        Class.forName("com.mysql.jdbc.Driver");
        connection = DriverManager.getConnection(
                "jdbc:mysql://(ip):3306/DatabaseName",
                "(username)","password");
        statement = connection.createStatement();

    }catch (SQLException e){
        e.printStackTrace();
    }catch (ClassNotFoundException e){
        e.printStackTrace();
    }
JollyRoger
  • 737
  • 1
  • 12
  • 38
  • Have you tried turning your IDE (or computer) off and on again? :) But seriously adding it as library should be enough. It is hard to help you without seeing your project structure and/or its settings. – Pshemo Mar 31 '18 at 08:28
  • I tryed it :). What do you need to know? I am not sure what is related with this. What settings should I have. By the way, It is running on tomcat. @Pshemo – JollyRoger Mar 31 '18 at 08:30
  • I added whole related code @Pshemo – JollyRoger Mar 31 '18 at 08:33
  • @Pshemo It works on a single main class but not on tomcat server. Any idea? – JollyRoger Mar 31 '18 at 08:38
  • 1
    does your war Lib directory contains the connector jar ? – Alpesh Jikadra Mar 31 '18 at 08:39
  • Since it works on non-server environment it suggests that project sees this library, but at the same time it is not deployed on server. Work around would be placing that JAR on Tomcat libraries (`tomcat/lib`) directory but proper one would be configuring project to include it while deployment. – Pshemo Mar 31 '18 at 08:48
  • @AlpeshJikadra That solved my problem, thank you. I thought IntelliJ automatically does that. – JollyRoger Mar 31 '18 at 08:48
  • Possibly related: [How to configure Tomcat to connect with MySQL](https://stackoverflow.com/q/3485177) – Pshemo Mar 31 '18 at 08:48

1 Answers1

0

Just add mysql-connector-java-5.1.46.jar in the Project Structure -> Libraries. Make sure it appears like below screenshot.

enter image description here

Shailesh Pratapwar
  • 4,054
  • 3
  • 35
  • 46