1

I've ran into some problems while trying to connect my app to the SQL and I've got this error:

java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:377)
    at DB.DB.getDbConnection(DB.java:19)
    at DB.DB.isConnected(DB.java:26)
    at com.urlshort.Main.main(Main.java:13)

Does anyone know what can I do to stop this error?

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Dylan
  • 23
  • 4
  • 4
    Did you add the required jdbc driver to your classpath? – Federico klez Culloca Dec 05 '20 at 11:59
  • 1
    You do not need `Class.forName()` anymore for loading the JDBC driver, but it still has to be in the classpath. – Progman Dec 05 '20 at 12:00
  • @Progman: I hate that this is still in all the JDBC tutorials when it hasn't been necessary for over 10 years now (since JDBC 4 / Java **1.6**). I guess it doesn't usually cause explicit problems, so there's no pressure to remove it. – Joachim Sauer Dec 05 '20 at 12:05
  • @JoachimSauer Using `Class.forName` is still necessary when the driver is not on the initial classpath, but on a secondary classpath, like for example if the driver is in the `WEB-INF/lib` of a WAR. And of course, it is the ideal check if the driver is actually on the classpath (contrary to the "No suitable driver found", which might also indicate a typo or otherwise wrong URL). – Mark Rotteveel Dec 05 '20 at 12:16

0 Answers0