Home >Database >Mysql Tutorial >Why am I getting a NullPointerException when connecting to a MySQL database from Java?

Why am I getting a NullPointerException when connecting to a MySQL database from Java?

Susan Sarandon
Susan SarandonOriginal
2024-11-16 09:00:04445browse

Why am I getting a NullPointerException when connecting to a MySQL database from Java?

Troubleshooting MySQL Connection Failure in Java

Problem: Connecting to a MySQL database from a Java program results in a NullPointerException error when invoking DriverManager.getConnection().

Code Example:

import java.sql.*;

public class Squirrel {
    public static void main(String[] args) {
        String user;
        String password;
        Connection connection;
        Statement statement;
        try {
            Class.forName("com.mysql.jdbc.Driver");

            connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306", user, password);

            statement = connection.createStatement();

            // Other code
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (statement != null) {
                    statement.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

Error Message:

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
...
Caused by: java.lang.NullPointerException
    at com.mysql.jdbc.ConnectionImpl.getServerCharset(ConnectionImpl.java:2997)

Solution:

The NullPointerException error indicates an issue with the MySQL driver initialization. The most common cause is using an outdated version of the MySQL JDBC driver.

Update the MySQL JDBC Driver:

  • Navigate to https://mvnrepository.com/artifact/mysql/mysql-connector-java
  • Download the latest version (currently 8.0.12)
  • Add the corresponding dependency to your Java project's pom.xml file:
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.12</version>
</dependency>

Additional Fix (for MySQL 8.0.1 ):

If the issue persists, it may be related to changes introduced in MySQL 8.0.1. To resolve this:

  • Update to MySQL JDBC Driver 5.1.41 or later
  • Alternatively, you can use the following optimization for MySQL 8.0.0 in your connection URL:
jdbc:mysql://localhost:3306/?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC

The above is the detailed content of Why am I getting a NullPointerException when connecting to a MySQL database from Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn