MySQLでJavaを使用して、現在の日付に数か月追加しますか?
以下は、Java-MySQLでINTERVALを使用して月を追加するための構文です。
String query; query = "insert into yourTableName values(curdate()+interval howManyNumberOfMonths month)";
以下は現在の日付です-
mysql> select curdate(); +------------+ | curdate() | +------------+ | 2020-10-25 | +------------+ 1 row in set (0.00 sec)
テーブルを作成しましょう-
mysql> create table demo12 −> ( −> created_date date −> ); Query OK, 0 rows affected (1.84 sec)
ここでは、Javaから現在の日付に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;
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();
}
}
} これにより、次の出力が生成されます-
これで、すべてのレコードを表示することにより、データがテーブルに挿入されているかどうかを確認できます。以下はクエリです-
-
MySQLクエリを使用して、現在の日付が特定の日付範囲内にあるかどうかを確認します
まずテーブルを作成しましょう- mysql> create table DemoTable1448 -> ( -> StartDate date, -> EndDate date -> ); Query OK, 0 rows affected (0.46 sec) 挿入コマンド-を使用して、テーブルにいくつかのレコードを挿入します mysql> insert into DemoTable1448 values('2019-01-21',&
-
MySQLで現在の日付に11日を追加
まずテーブルを作成しましょう- mysql> create table DemoTable1994 ( ArrivalDate date ); Query OK, 0 rows affected (5.33 sec) 挿入コマンド-を使用して、テーブルにいくつかのレコードを挿入します mysql> insert into DemoTable1994 values('2019-12-18'); Query OK, 1 row affected (0.47 sec) mysql> insert into DemoTable1994 va