
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to insert DATE into a MySQL column value using Java?
For this, you can use PreparedStatement from Java. Let us first create a table wherein one of the columns is ArrivalDate with DATE type −
mysql> create table DemoTable( PassengerId int, PassengerName varchar(40), ArrivalDate date ); Query OK, 0 rows affected (0.82 sec)
The JAVA code is as follows to insert date −
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; public class InsertDateFromJava { public static void main(String[] args) { Connection con = null; PreparedStatement ps = null; try { java.util.Date javaDate = new java.util.Date(); java.sql.Date mySQLDate = new java.sql.Date(javaDate.getTime()); con = DriverManager.getConnection("jdbc :mysql ://localhost :3306/web?useSSL=false", "root","123456"); String query = "insert into DemoTable(PassengerId,PassengerName,ArrivalDate) values(?,?,?) "; ps = con.prepareStatement(query); ps.setInt(1, 101); ps.setString(2, "Adam"); ps.setDate(3, mySQLDate); ps.executeUpdate(); System.out.println("Record is inserted with current Date......"); } catch (Exception e) { e.printStackTrace(); } } }
This will produce the following output of Java −
Record is inserted with current Date......
Following is the screenshot of the output −
Let us check the records of the table again −
mysql> select * from DemoTable;
This will produce the following output −
+-------------+---------------+-------------+ | PassengerId | PassengerName | ArrivalDate | +-------------+---------------+-------------+ | 101 | Adam | 2019-09-07 | +-------------+---------------+-------------+ 1 row in set (0.00 sec)
The screenshot is as follows −
- Related Articles
- Insert NULL value into INT column in MySQL?
- How to insert only a single column into a MySQL table with Java?
- Java application to insert null value into a MySQL database?
- How to insert Date value into table in JDBC?
- How to Insert custom date into MySQL timestamp field?
- How can we use INSERT() function to insert a new string into the value of a column of MySQL table?
- How to insert own values into auto_increment column in MySQL?
- How to insert date object in MySQL using Python?
- How to insert data into a MySQL database with Java?
- MySQL insert a value to specific row and column
- Insert default into not null column if value is null in MySQL?
- How to insert into two tables using a single MySQL query?
- How to insert a document into a MongoDB collection using Java?
- How can we insert current date automatically in a column of MySQL table?
- How to insert multiple document into a MongoDB collection using Java?

Advertisements