mysql> desc my_uat.students;
+-----------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-------------+------+-----+---------+----------------+
| student_id | int | NO | PRI | NULL | auto_increment |
| student_name | varchar(45) | NO | | NULL | |
| student_dob | datetime | NO | PRI | NULL | |
| student_address | varchar(45) | NO | | NULL | |
+-----------------+-------------+------+-----+---------+----------------+
As you can see in the above students table I have a filed student_dob declared as datetime, in this example, we will see how to insert a timestamp into this field using JDBC PreparedStatement,
Code Example:
String url ="jdbc:mysql://localhost:3306/my_db";
String userName="root";
String password ="root123";
String insertQuery ="insert into students values(?,?,?,?)";
Connection connection = DriverManager.getConnection(url,userName,password);
PreparedStatement preparedStatement = connection.prepareStatement(insertQuery);
preparedStatement.setString(1, null);
preparedStatement.setString(2, "Huge");
preparedStatement.setObject(3,Instant.now());
preparedStatement.setString(4, "Ohio");
int result = preparedStatement.executeUpdate();
As you can see we have made use of the Java 8 Instance (Date and Time API) class in order to insert a timestamp into the Database using PreparedStatement setObject method.

-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- Convert Java Map Collection Object to JSON String using Jackson
- Java Stream flatmap() Examples
- [Fix] Instant java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years
- How to run Java Unit Test cases with Apache Maven?
- How to check if Java main thread is alive
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Parsing CSV file using Java code example (Comma Separated File)
- Unhandled exception type InterruptedException : Java Threads
- Native getClass() method from java.lang.Object Class Explained with examples.
- Java Jackson ObjectMapper Class with Examples
- Java 8 Streams map() with examples
- Java 8 - Convert List to Map Examples
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- Java Stream with Multiple Filters Example
- How to Clear StringJoiner in Java 8
- Spring 5 IoC Example with application Context XML (ClassPathXmlApplicationContext) and Gradle.
- How to get end of line (EOL) or new line character \r \n in Java
- Spring Boot CRUD Examples using JDBCTemplate
- Delete a File in Java with Examples
- Implementing Insertion Sort Algorithm in Java Program
- Java JDBC Batch Update Example with PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to fix Java HTTP java.net.UnknownHostException
- Java 8 Display time in 12 hour AM PM format
More Posts:
- Types of brackets used in Programming Languages - Codes - 2022
- How to sort a List using Lambda in Python - Python
- Setting Expire Time - EX EXAT PX PXAT and KEEPTTL with Spring Boot + Redis - Java
- JSON Datatypes : Tutorial - Json-Tutorial
- How to disable SharePoint subsite creation option for owners - SharePoint
- [Fix] Microsoft Windows Store error 0x00000005 (Windows 8/10/11) - Windows
- Installing vue.js in Laravel 8 - PHP
- Steps to Delete or Deactivate Instagram Account - HowTos