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
Release: 2024-11-16 09:00:04
Original
454 people have browsed it

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();
            }
        }
    }
}
Copy after login

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)
Copy after login

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>
Copy after login

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
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template