Can't connect to MySQL from Java: NullPointerException inside MySQL driver connection logic

前端 未结 3 1510
感情败类
感情败类 2020-11-30 12:54

I\'m trying to connect to a database I created with MySQL in my Java program, but it always fails.

For the sake of example, here is my code:

import j         


        
相关标签:
3条回答
  • 2020-11-30 12:56

    It might be because you're using an older version of the MySQL driver. You should try using the newest version.

    To get the newest version, you can check https://mvnrepository.com/artifact/mysql/mysql-connector-java

    As of right now, the newest version is 8.0.11. You can download it here or add this to your pom.xml:

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.11</version>
    </dependency>
    

    Update

    Upon further investigation, it seems that it's because of a change that was introduced in MySQL 8.0.1:

    The issue you reported is related to the changes introduced in MySQL 8.0.1 wrt the character sets and collations support, with the addition of now being 'utf8mb4' the default char set. Such changes broke the way Connector/J initializes connections.

    As you know this was fixed in Connector/J 5.1.41 and I'm sure you already updated your library.

    reference

    Like mentionned above, an alternative fix to your problem would have been to use the 5.1.41 instead of 5.1.40.

    0 讨论(0)
  • 2020-11-30 13:03

    If you call the IP address 127.0.0.1/localhost then you are communicating with the localhost – in principle, with your own computer.This issue also appears when you don't have localhost configured

    For Linux systems

    • Add/Edit "127.0.0.1 localhost" under /etc/hosts if its missing.

    For Windows system

    • Add/Edit under C:windows/system32/drivers/etc/hosts if its missing.

    For more details on localhost

    0 讨论(0)
  • 2020-11-30 13:11

    Sounds like a potential version mismatch or outdated client. When you run it outside the IDE you may be pulling in the wrong version. I'd make sure the client is on the latest version or similar to the version used by the server.

    0 讨论(0)
提交回复
热议问题