0

So i followed all the steps listed in other posts to add mysql-connector-java-5.1.36-bin to my class path in intellij 12 and when i'm writing code intellij clearly sees it and lets me import it, but when I run the application inside of tomcat I get a ClassNotFoundException

Below are screen shots of my set ups and my code.enter image description here

enter image description here

 public void runLookUp(String s){
    String url = "jdbc:mysql://localhost:3316/test";
    String username = "java";
    String password = "password";
    System.out.println("Connecting database...");

    System.out.println("Loading driver...");

    try {
        Class.forName("com.mysql.jdbc.Driver");
        System.out.println("Driver loaded!");
    } catch (ClassNotFoundException e) {
        throw new IllegalStateException("Cannot find the driver in the classpath!", e);
    }
    try  {
        Connection connection = (Connection) DriverManager.getConnection(url, username, password);
        System.out.println("Database connected!");
    } catch (SQLException e) {
        throw new IllegalStateException("Cannot connect the database!", e);
    }
}

I am aware that doing it in global libraries instead of my lib directory is bad form I did it originally there and moved it to global as an attempted fix to this issue and just haven't moved it back yet. Given that this seems to work for everyone else I assume its something in the way I have my tomcat deploying from intellij, any thoughts?

lvoelk
  • 2,390
  • 1
  • 12
  • 16

1 Answers1

0

This has nothing at all to do with IntelliJ.

You need to put JDBC driver JARs in the Tomcat /lib folder, not in any project WEB-INF/lib.

Your code as written is still far from optimal:

  1. Hard coded URL, driver class, and credentials make it difficult to change.
  2. Username and password in plain text.
  3. No JNDI lookup.
  4. No connection pooling.
duffymo
  • 305,152
  • 44
  • 369
  • 561
  • Thank you for marking the other question. I put it in the WEB-INF/lib as the other answers recommended and it works properly now. In terms of code this is just sample code my actually database accessing code is scattered across several files as it does involve properties files, pooling and query validation, as such it is not suitable for posting on stack overflow and I personally feel that you should always post the simplest code possible to show your issue on stack overflow as it will make it pertain to the most people. – lvoelk Sep 27 '15 at 16:41
  • No, don't put it in WEB-INF/lib. That used to be the right answer, but it's not anymore for Tomcat 7.x and higher: https://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html – duffymo Sep 27 '15 at 17:10