Reputation:
Im building a simple Web application, I have configured Mysql to connect to my program by:
Adding maven dependency, this is in my pom.xml file
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
I set up the connection inside Intellij, and testing it gives me valid connection Connection test I can even run SQL queries through the query console: Running query
Just to be extra sure, we can see that the MYSQL-connector jar file has been downloaded by maven and added to the project library. jar file
Making sure the JBDC driver is built into the artifact during deployment: Driver in artifact deployment
So to test my connection I have the following code
package id.owl.owl;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.json.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
@Path("/hello-world"){
public class HelloResource {
@GET
@Produces("text/plain")
public String hello() {
String jdbcUrl = "jdbc:mysql://localhost:3306/test";
String username = "root";
String password = "root1234";
String value ="hello world";
try{
System.out.println("Connecting to database");
Connection connection = DriverManager.getConnection(jdbcUrl, username, password);
}
catch (Exception error){
value = error.toString();
}
return value;
}
What I get back is java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/test
I really have no idea what else Im doing wrong here, I looked through a lot of the stackoverflow posts on this error and none of their solutions have worked. The driver is imported, i can even run queries through my IDE, but not through my code for some reason.
Upvotes: 0
Views: 579
Reputation:
So heres how to solve it, you dont need to add Class.forname or anything. Intellij doesnt actually import the jdbc driver for you even though it says it does, you need to manually download the driver for whatever database you are using, in my case MYSQL, then go to project structure > modules > add new library and add the downloaded jdbc driver. Recompile and it should run now.
Upvotes: 1