0

I've already checked all the other answers and I've not find a correct answer.

I've a servlet that wants to insert data into a MySQL DB through a Model (InterazioneDB.java), so in the constructor I put this. On the PC I've downloaded MySQL Workbench for creating the databases

InterazioneDB(){
          try {
             //Creea il collegamento con il DB
              Connection conn = DriverManager.getConnection(
                      "jdbc:mysql://localhost:3306/sitofoto?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC",
                      "root", "password"
                );    // for MySQL only
          } catch(SQLException ex) {
             ex.printStackTrace();
          }
}

When I create the object I get the error: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/sitofoto?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC

I have the driver (mysql-connector-java-8.0.19.jar) in lib folder inside WEB-INF I've already tried to remove, reinstall it again and with Class.forName("com.mysql.jdbc.Driver"); even though it's deprecated.

I did that yesterday (and I didn't change that part of code) and it worked, so I don't know what to do.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 1
    In a servlet environment you shouldn't use `DriverManager` directly; it is recommend to use a `javax.sql.DataSource` backed by a connection pool. Also, if the driver is in `WEB-INF`, then loading the class with `Class.forName` is necessary, because automatic driverloading is only supported for drivers on the initial classpath (in this case the class path of your application server). For MySQL Connector/J 8.0.x you should use `Class.forName("com.mysql.cj.jdbc.Driver")`. – Mark Rotteveel Apr 02 '20 at 11:58
  • I did that and it works, but nw I've another problem. When I exceute a query it shows nothing (empty) – Alessio Balderi Apr 02 '20 at 19:27

0 Answers0