Mysql Does Not Connect by getConnection() Netbeans Java

Asked

Viewed 911 times

2

I’ve been trying for a while to get my Mysql database connected by getConnection(). I’ve tried several different ways, to put the name of the bank, I’ve seen several tutorials including tutorials that are from the Mysql site itself, videos, posts on the internet and none of the solutions there views solved my problem!

I am trying to use the format that does not use the driver manually since in the most current versions of java this is done automatically.

Follow a print of the code I ran next to the connected database.

descrição

The error is as follows:

Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:mysql:@localhost:3306/Java
    at java.sql.DriverManager.getConnection(DriverManager.java:689)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at acessobanco.AcessoBanco.main(AcessoBanco.java:14)
/Users/Alecell/Library/Caches/NetBeans/8.1/executor-snippets/run.xml:53: Java returned: 1
FALHA NA CONSTRUÇÃO (tempo total: 0 segundos)

I use Netbeans on Mac with MAMP. Initially the port of Mysql was 8889, I came to think that would be the problem and modified to 3306 even so did not work. I did a check of whether the database is working on the new port (3306) with Mysqlworkbanch and is actually working, I just can’t make the connection via java code.

The JDBC was installed in several different ways and the mysql-Connector-java-5.0.8-bin. jar in fact it is in the library of the project.

  • Hi Alexandre, could you please post the code in text format instead of an image? Hugs.

1 answer

3


Try adding this to your code at the very beginning of main:

try {
    Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
    throw new AssertionError(e);
}

If that solves the problem, then great. On the other hand, if it AssertionError having as its cause the ClassNotFoundException, then there is definitely something wrong with your/classpath libraries. This shouldn’t be necessary, but is harmless and will serve to report some classpath problem, if any.

Also, I recommend using a newer version of the Nector, to avoid having a headache with some bugs or with the lack of some Eature. Currently the latest versions are 5.1.39 and 6.0.3 (Milestone 2).

Finally, avoid putting code questions only in images, always prefer text because it is easier for those who answer test their code. The images only serve for when the text is not enough.

  • Hi, it worked with the code you gave me, I even got scared when it spun. But I was left with the doubt of why it wasn’t working without this code snippet, would you tell me?

  • 1

    Ultimately the problem was in fact the JDBC version of Mysql! Thank you so much for the help!

Browser other questions tagged

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