1

I've been trying to set up my own rest api for a school project. I decided to use MySQL as a database and i want to connect it with my webservice, but apparently i always get this error message: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/userdatenbank

When i look at the error message, it also tells me that this code can't be executed:

public UserService() {
    try {
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/userdatenbank", "root", "adminadmin123"); 
    } catch (SQLException e) {
        e.printStackTrace();
    }  
} 

I don't know why this keeps happening... The MySQL Java Connector JAR-File is located in "Referenced Libraries". By the way I use Tomcat 9, MySQL 8, JDK 13 and Maven (Jersey Servlet).

Quantum
  • 17
  • 1
  • 1
  • 5

2 Answers2

3

Before get connection you have to load your driver with:

Class.forName("com.mysql.jdbc.Driver");

And the corresponding JAR must be in your classpath (in the lib of your server or packaged in your WAR file).

Anyway I suggest you to use a connection's pool instead of DriverManager

The main benefits to connection pooling are:

  1. Reduced connection creation time.

Although this is not usually an issue with the quick connection setup that MySQL offers compared to other databases, creating new JDBC connections still incurs networking and JDBC driver overhead that will be avoided if connections are recycled.

  1. Simplified programming model.

When using connection pooling, each individual thread can act as though it has created its own JDBC connection, allowing you to use straightforward JDBC programming techniques.

  1. Controlled resource usage.

If you create a new connection every time a thread needs one rather than using connection pooling, your application's resource usage can be wasteful, and it could lead to unpredictable behaviors for your application when it is under a heavy load.

Community
  • 1
  • 1
Renato
  • 2,077
  • 1
  • 11
  • 22
  • The Class.forName() method doesn't change anything. Still an error. But this time i also have a java.lang.ClassNotFoundException: com.mysql.jdbc.Driver error. – Quantum Feb 05 '20 at 21:18
  • If you have a ClassNotFound instead of SQLException, something is changed! As I wrote you must have a JAR containing the driver implementation in your class path – Renato Feb 05 '20 at 21:20
  • Do u mean the Java Connector jar-file? This one: mysql-connector-java-8.0.19.jar? – Quantum Feb 05 '20 at 21:21
  • Yes, I do! You must have mysql-connector-java-8.0.19.jar in the lib of your server – Renato Feb 05 '20 at 21:23
  • Now i get this error: java.sql.SQLException: The server time zone value 'Mitteleurop?ische Zeit' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the 'serverTimezone' configuration property) to use a more specific time zone value. And also this error: org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet [Jersey Web Application] in context with path [/gfos-inno-award-2020] threw exception [java.lang.NullPointerException] with root cause java.lang.NullPointerException – Quantum Feb 05 '20 at 21:27
  • Take a look at this https://stackoverflow.com/a/27276523/4210091 – Renato Feb 05 '20 at 21:28
  • @Quantum, I am glad you found the solution useful. In these cases you can accept the solution by clicking on the check mark beside the answer to toggle it from greyed out to filled in. Thank you! – Renato Feb 05 '20 at 21:43
1

Since you are using maven just add the dependency in your pom.xml file.

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>

Just go to the maven repo and search for a version that works for you. or If you have downloaded the jar file then add it to your project classpath.

edit -> Class.forName() is not needed since JDBC 4.0.

Silverfang
  • 349
  • 1
  • 8