सिंटैक्स
String query = "INSERT INTO yourTableName VALUES (CURDATE() + INTERVAL howManyMonths MONTH)";
उदाहरण
वर्तमान तारीख: 2020-10-25
2 महीने जोड़ने के बाद: 2020-12-25
Java कोड
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 = "INSERT INTO demo12 VALUES (CURDATE() + INTERVAL 2 MONTH)";
int status = statement.executeUpdate(query);
if (status > 0)
System.out.println("Record inserted successfully..Look into the database");
else
System.out.println("Error not inserted");
} catch (Exception e) {
e.printStackTrace();
}
}
}
नोट: MySQL का INTERVAL कीवर्ड तारीख अंकगणित को सरल बनाता है।