0

I've got a servlet and a filter connected to it. In this filter I'd like to do some database things but I've got well known error "No suitable driver found for jdbc:mysql". I've been using the jdbc in the past and those codes still work. But when I copy the connection lines from them to my filter, I've got the error. I change the database name in the line.

Here's my filte:

System.out.println("Filter 2 works");

        try (Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/filters?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC", "root", "password");
             Statement stat = conn.createStatement()) {

        } catch (SQLException e) {
            e.printStackTrace();
        }

In "try" line I have the error. I tried without "useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC" but it still didn't work. Here's a screen of the module settings -> Libraries. The JDBC is added there: https://i.stack.imgur.com/KzhqV.png

That's my first project when I'm using Tomcat and JDBC together. Should I connect them somehow before trying anything?

  • Hey, You forgot to register the driver with `Class.forName("com.mysql.jdbc.Driver")` above your getConnection line. – KnockingHeads Nov 23 '20 at 13:25
  • @Ashish I've never done that before and my code was fine. Now I've got try...catch block with Class.forName("com.mysql.jdbc.Driver") above and now it gives me the same error + ClassNotFoundException in the Class.forName("com.mysql.jdbc.Driver") line – Kacper Zięba Nov 23 '20 at 15:18
  • 1
    That means that you don't have the MySQL Connector/J JDBC driver on the classpath. As an aside, using `DriverManager` in a web application is usually a bad idea. For better performance, use a connection pool (eg the one built-in in Tomcat, or a third-party library like HikariCP). – Mark Rotteveel Nov 23 '20 at 16:01
  • @Ashish - `Class.forName()` is [not needed for JDBC 4.0 drivers](https://stackoverflow.com/a/8053125/12567365). mysql-connector-java-8.0.20.jar is one such driver (it's actually JDBC 4.2, I believe). Most drivers you are likely to encounter these days are JDBC 4.x. – andrewJames Nov 23 '20 at 17:11

0 Answers0