0

We have a database on a server for our project.
When I run the program, it works completely fine. I can log in, and register another account.
But when my classmate runs the code, he gets an exception: No suitable driver found for jdbc:mysql://ServerIp//DBName

My code looks like this:

String hostURL = "jdbc:mysql://ServerIp:3306/Database"; //There's an actual ip of course
Connection con = DriverManager.getConnection(hostURL, "username", "password");
System.out.println("Connected successfully");

We've already added mysql-connector to the dependencies (IntelliJ), but it didn't help at all.
Also, I didn't even need to add it, the project worked just fine for me without doing anything at all. The project is in Java Enterprise -> Web Application

Nowhere Man
  • 19,170
  • 9
  • 17
  • 42
Olcsa22
  • 27
  • 1
  • 4

1 Answers1

2

This error occurs if JDBC is not able to find a suitable driver for the URL format passed to the getConnection() method e.g."jdbc:mysql://" in our case.

In order to solve this error, you need the MySQL JDBC driver like "mysql-connector-java-5.1.36.jar" in your classpath.

Or because you forgot to register your mysql jdbc driver with the java application.

Connection con = null;
try {
    //registering the jdbc driver here, your string to use 
    //here depends on what driver you are using.
    Class.forName("something.jdbc.driver.yourdriver");   
    con = DriverManager.getConnection(hostURL, ...);
} catch (SQLException e) {
    throw new RuntimeException(e);
}
Moïse Gui
  • 67
  • 2
  • In modern Java, JDBC drivers are automatically registered via the Java [*Service Provider Interface (SPI)*](https://en.m.wikipedia.org/wiki/Service_provider_interface) facility. No need for `Class.forName`. – Basil Bourque Nov 15 '21 at 23:12