1
votes

This is my pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>id</groupId>
    <artifactId>jbdc_project</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                </configuration>
            </plugin>
        </plugins>

    </build>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.20</version>
        </dependency>
    </dependencies>
</project>

I tried to connect to MySQL database:

public class Main {
    public static void main(String[] args) {
        String url = "jbdc:mysql://localhost:3306/mydb?autoReconnect=true&useSSL=false";
        String user = "root";
        String password = "root";

        try{
            Connection myConn = DriverManager.getConnection(url,user,password);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

And i get this error:

java.sql.SQLException: No suitable driver found for jbdc:mysql://localhost:3306/mydb?autoReconnect=true&useSSL=false

Any ideas on how to fix it?

1
Load the class beforehand. 'Class.forName("com.mysql.jdbc.Driver");'Nipun Thathsara
Btw, did you try executing this from the IDE or running a standalone JAR file? If you are trying to execute the built JAR, you might want to package the dependencies with Maven assembly plugin.Nipun Thathsara
'Class.forName("com.mysql.jdbc.Driver");' doesn't fix my error. I'm not using a JAR, i thought the whole point of importing a dependency was not having to use the physical jar file right?Yan Lanna Alexandre

1 Answers

0
votes

Got it.

There's a typo in your JDBC URL. It should be 'jdbc' instead of 'jbdc'.

String url = "jdbc:mysql://localhost:3306/mydb?autoReconnect=true&useSSL=false";