Error connecting to Mysql

Asked

Viewed 354 times

1

I took an example of 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 that mistake 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"/>

inserir a descrição da imagem aqui

  • Unzip this . zip to stay . jar.

1 answer

2


The error occurs because the mysql driver is not in your classpath. To resolve, you can download it manually via the link https://dev.mysql.com/downloads/connector/j/
Or you can add the dependency on Maven if you use it by adding it to your pom.xml file:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>6.0.2</version>
</dependency>
  • Hello, I already include the driver and keep happening the error(picture in question).

  • Check if the driver appears in the classpath.

  • I’ve already arranged it, thank you.

  • What was the problem?

  • @Lucascarezia if my answer was convincing, mark it as Reply Acceptance please.

  • I just forgot to put the Connector.jar inside the JAVA folder

  • Got it, good that your problem has been solved. Thank you :)

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.