Error connecting to MySQL

1

I took an example from the internet to connect with MySQL

private static final String USUARIO = "root";
private static final String SENHA = "123456";
private static final String URL = "jdbc:mysql://127.0.0.1:3306/aulas";
private static final String DRIVER = "com.mysql.jdbc.Driver";

public static Connection abrir() throws Exception
{   
    Class.forName(DRIVER);

    Connection conn = DriverManager.getConnection(URL, USUARIO, SENHA);

    System.out.println("Conectado");

    return conn;
}

And this error happens

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at Default.Main.abrir(Main.java:52)
at Default.Main.<init>(Main.java:64)
at Default.Main.main(Main.java:36)

How can I fix this?

EDIT

<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="lib" path="mysql-connector-java-5.1.39.zip"/>
<classpathentry kind="output" path="bin"/>

    
asked by anonymous 17.05.2016 / 15:59

1 answer

2

The error occurs because the mysql driver is not in your classpath. To resolve, you can download it manually through the link link. Or you can add the dependency on maven if you use it by adding to your pom.xml file:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>6.0.2</version>
</dependency>
    
17.05.2016 / 16:03