MySQL does not connect through getConnection () Netbeans Java

2

I'm already trying to make my MySQL database connect at getConnection() . I have tried several different ways, to put the name of the bank, I have seen several tutorials including the tutorials that are from the MySQL website itself, videos, internet postings and none of the solutions there have solved my problem!

I'm trying to use the format that I do not go to the driver manually since in the most current versions of java this is done automatically.

Follows a print of the code I ran next to the connected bank.

Theerrorisasfollows:

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 the Mac with MAMP. Initially the port of MySQL was 8889, I came to think that this would be the problem and I modified it to 3306 even though it did not work. I made a check of whether the database is working on the new port (3306) with MySQLWorkbanch and is actually working, I just can not make the connection via java code.

JDBC has been installed in many different ways and mysql-connector-java-5.0.8-bin.jar is actually in the project library.

    
asked by anonymous 20.07.2016 / 04:23

1 answer

3

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

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

If this solves the problem, then great. On the other hand, if it pops AssertionError from ClassNotFoundException , then there's something wrong with your classpaths. This should not be necessary, but it is harmless and will serve to report some classpath problem, if any.

In addition, I recommend using a newer version of the connector, to avoid having a headache with some bugs or lack of any features. At this time the latest versions are 5.1.39 and 6.0.3 (milestone 2).

Finally, avoid asking questions in the code only in images, always prefer text because it is easier for anyone to respond to test your code. Images are only used when text is not sufficient.

    
20.07.2016 / 11:05