
- 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
Add some months to current date using Java with MySQL?
Following is the syntax to add months using INTERVAL with Java − MySQL.
String query; query = "insert into yourTableName values(curdate()+interval howManyNumberOfMonths month)";
Following is the current date −
mysql> select curdate(); +------------+ | curdate() | +------------+ | 2020-10-25 | +------------+ 1 row in set (0.00 sec)
Let us create a table −
mysql> create table demo12 −> ( −> created_date date −> ); Query OK, 0 rows affected (1.84 sec)
Here, I am going to add 2 months to the current date from Java. Now, date will be inserted into the table 2020−12−25.
The Java code is as follows
Example
import java.sql.Connection; import java.sql.DriverManager; import com.mysql.jdbc.Statement; public class AddSomeMonthDemo { public static void main(String[] args) { Connection con = null; Statement statement = null; try { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sampledatabase", "root", "123456"); statement = (Statement) con.createStatement(); String query; query = "insert into demo12 values(curdate()+interval 2 month)"; int status = statement.executeUpdate(query); if (status > 0) System.out.println("Record inserted succesfully..Look into the database"); else System.out.println("Error not inserted"); } catch (Exception e) { e.printStackTrace(); } } }
This will produce the following output −
Now you can verify whether the data inserted in the table by displaying all the records. Following is the query −
- Related Articles
- Add months to current date using Calendar.add() method in Java
- Java Program to subtract months from current date using Calendar.add() method
- Java Program to add days to current date using Java Calendar
- Select dates between current date and 3 months from the current date in MySQL?
- How can it be possible to add 3 months interval in a MySQL date without using the word ‘Months’ with interval?
- Add week to current date using Calendar.add() method in Java
- Add seconds to current date using Calendar.add() method in Java
- Java Program to add year to current date using Calendar.add method
- Add 11 days to current date in MySQL
- How to add a number of months to a date using JavaScript?
- How to add current date to an existing MySQL table?
- How to add months to a date in JavaScript?
- MySQL date column auto fill with current date?
- How to add new worksheet with current date in Excel?
- How do I calculate the date six months from the current date using the datetime Python module?

Advertisements