You might have noticed the preparedStatement.setDate method of Java JDBC users the older Date object, if you are wondering how to insert the new Java 8+ Date and Time API date objects to the Database using JDBC then you should be using preparedStatement.setObject
Example 1: with LocalDate
Connection connection = DriverManager.getConnection(url,userName,password);
PreparedStatement preparedStatement = connection.prepareStatement(insertQuery);
preparedStatement.setString(1, null);
preparedStatement.setString(2, "Andrew");
preparedStatement.setObject(3, LocalDate.of(1999, 07, 21));
preparedStatement.setString(4, "Ohio");
int result = preparedStatement.executeUpdate();
mysql> select * from my_uat.students;
+------------+--------------+---------------------+-----------------+
| student_id | student_name | student_dob | student_address |
+------------+--------------+---------------------+-----------------+
| 1 | Mike | 2001-07-22 00:00:00 | New York City |
| 2 | Alex | 2002-08-13 00:00:00 | Chicago |
| 3 | Sam | 2022-01-25 00:00:00 | Ohio |
| 4 | Andy | 2001-10-10 00:00:00 | Japan |
| 5 | Andrew | 1999-07-21 00:00:00 | Ohio |
+------------+--------------+---------------------+-----------------+
Example 2: with LocalDateTime
preparedStatement.setObject(3, LocalDateTime.of(1998, 11, 18,10,20,10));

-
Have Questions? Post them here!
More Posts related to Java,
- Add two numbers using Java Generics
- Convert Java List to Json String using Jackson
- Convert Java Object to JSON using Jackson Library
- Java SE JDBC: Insert with PreparedStatement Example
- [Program] How to read three different values using Scanner in Java
- Java JDBC Batch Update Example with PreparedStatement
- Java Stream flatmap() Examples
- Save Java Object as JSON file using Jackson Library
- Java get day of the week as an int using DayOfWeek
- Create Nested Directories using Java Code
- Java JDBC Delete a Record in Database Table using PreparedStatement
- List of jars required for Struts2 project
- Convert Java Object to XML using Jackson Library
- Struts2 : java.lang.ClassNotFoundException: org.apache.commons.fileupload.RequestContext
- Java JDBC Get Id of the Inserted Record with AutoIncrement
- How to list all tables using Java JDBC
- Java Jackson ObjectMapper Class with Examples
- Fix: Maven - Failed to execute goal - Compilation failure - Source/Target option 5 is no longer supported. Use 7 or later
- Eclipse : The type java.lang.CharSequence cannot be resolved. Indirectly referenced from required .class files
- Formatting Double in Java [Examples]
- How to run Java Unit Test cases with Apache Maven?
- [fix] NullPointerException Cannot Invoke findById because Repository is null - Java Spring
- [Fix] java: integer number too large compilation error
- [Java] Read a File with UTF-8 Encoding
- How to detect Operating System using Java code
More Posts:
- Maven Unsupported major.minor version 51.0 - Java
- Error: Unable to access jarfile jarFileName.jar file [Windows] - Java
- SharePoint - The URL is invalid. It may refer to a nonexistent file or folder, or refer to a valid file or folder that is not in the current Web. - SharePoint
- [Fix] Docker Run unknown shorthand flag: 'r' in -rm - Docker
- How to install Node using Brew on Mac - MacOS
- How to clear MySQL Console Screen - MySQL
- Android xml error Attribute is missing the Android namespace prefix [Solution] - Android
- How to share SharePoint site or document with all users in organization - SharePoint