0

I am working on a webapp using JavaServer Pages and a MySQL database.

I moved my webapp into Apache Tomcat's .../webapps/ROOT folder to run the application on local host. However, I no longer have my terminal to compile and run with an absolute path to the database driver jar files found in mysql-connector-java-8.0.17. I have found a post that says to put the mysql-connector-java-8.0.17.jar file into WEB-INF/lib, but this has not fixed my problem.

How can I give my JSP file a classpath to the MySQL database driver so I can connect to it? If there is a different, better way of doing this, please let me know!

My code to connect is this:

String url = "jdbc:mysql://localhost:3306/***";
        ResultSet rs = null;
        try {
          Connection conn = DriverManager.getConnection(url, "***", "***");
...

I am getting this exception:

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/***.

If there is more information to include, please let me know!

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • I have rolled back you last edit. Please don't add things like "SOLVED" to a title. The correct way to do this is by accepting the answer. You should not do anything other than that. If you managed to solve your own problem, then post an answer and accept that after the timeout. – Mark Rotteveel Aug 11 '19 at 05:23
  • In a web application you shouldn't use `DriverManager` to connect. It is inefficient, instead use a `javax.sql.DataSource` that provides connection pooling. – Mark Rotteveel Aug 11 '19 at 05:24

1 Answers1

1

Since WEB-INF/lib is automatically included into classpath, the problem is probably that you forgot specifying the JDBC driver's package path before connecting:

Class.forName("com.mysql.jdbc.Driver");  
mangusta
  • 3,470
  • 5
  • 24
  • 47
  • Okay, I tried that and it seemed to have done something but also created a different exception: ```java.lang.ClassNotFoundException: com.mysql.jdbc.Driver``` – Chris Turgeon Aug 11 '19 at 03:06
  • I also found this: When you do ```Class.forName("com.mysql.jdbc.Driver")``` to load and register this driver class, the class loader in JVM search for this class inside all JAR files available in CLASSPATH. If ```mysql-connector-java-5.1.25-bin.jar```, which contains this class is not available in CLASSPATH then JVM will throw ```java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at run-time.``` – Chris Turgeon Aug 11 '19 at 03:07
  • This must mean that my program cannot find the .jar within the classpath – Chris Turgeon Aug 11 '19 at 03:08
  • 1
    I had to restart Apache Tomcat, and it worked. Thank you! – Chris Turgeon Aug 11 '19 at 03:27