Home >Java >javaTutorial >How to Correctly Use `setDate` in a Java `PreparedStatement` to Avoid Invalid Date Errors?

How to Correctly Use `setDate` in a Java `PreparedStatement` to Avoid Invalid Date Errors?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-06 05:51:10833browse

How to Correctly Use `setDate` in a Java `PreparedStatement` to Avoid Invalid Date Errors?

Using setDate in PreparedStatement

In your code, you are attempting to set the value of a DATE column in the database using the setDate() method. However, you are passing in a java.sql.Date object that has been initialized with the value 0000-00-00, which is an invalid date.

The correct way to set the value of a DATE column using setDate() is to pass in a java.util.Date object that contains the correct date value. You can create a java.util.Date object using the new java.util.Date() constructor or by using one of the many static factory methods available in the java.util.Calendar class.

For example, the following code shows how to set the value of a DATE column using a java.util.Date object that contains the current date:

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;

public class SetDateExample {

    public static void main(String[] args) throws SQLException {
        // Create a connection to the database
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "user", "password");

        // Create a prepared statement to insert a new row into the table
        String sql = "INSERT INTO my_table (name, date_column) VALUES (?,?)";
        PreparedStatement pstmt = conn.prepareStatement(sql);

        // Set the name and date_column values for the new row
        pstmt.setString(1, "John Doe");
        pstmt.setDate(2, new Date(Calendar.getInstance().getTime().getTime()));

        // Execute the prepared statement
        pstmt.executeUpdate();

        // Close the prepared statement and connection
        pstmt.close();
        conn.close();
    }
}

You can also use the setString() method to set the value of a DATE column, but you must first convert the java.util.Date object to a string using the toString() method. For example, the following code shows how to set the value of a DATE column using the setString() method:

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;

public class SetStringExample {

    public static void main(String[] args) throws SQLException {
        // Create a connection to the database
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "user", "password");

        // Create a prepared statement to insert a new row into the table
        String sql = "INSERT INTO my_table (name, date_column) VALUES (?,?)";
        PreparedStatement pstmt = conn.prepareStatement(sql);

        // Set the name and date_column values for the new row
        pstmt.setString(1, "John Doe");
        pstmt.setString(2, new Date(Calendar.getInstance().getTime().getTime()).toString());

        // Execute the prepared statement
        pstmt.executeUpdate();

        // Close the prepared statement and connection
        pstmt.close();
        conn.close();
    }
}

The above is the detailed content of How to Correctly Use `setDate` in a Java `PreparedStatement` to Avoid Invalid Date Errors?. 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