Set Date by using the Prepared Statement

This JDBC tutorial helps us for setting date in the database table by using the PreparedStatement interface of java.sql package.

Set Date by using the Prepared Statement

Set Date by using the Prepared Statement

     

This JDBC tutorial helps us for setting date in the database table by using the PreparedStatement interface of java.sql package. Sometimes, if you want to insert the data into database table with date this will provide a setDate method to set the date in table. Here, we are going to provide an example with code for adding the date in table. See brief description below:

Description of program:

In this program we are going to insert the date in database table (Records) with the help of PreparedStatement interface. For this, we establish the connection firstly. After establishing the connection you need to insert an appropriate data according to Records table (Stu_roll, Stu_name and Admin_date) in prepareStatement method that returns the PreparedStatement object. Here we add date by using the setDate method. Finally we will get all data that are inserted into the database table (Records) and it will display a message "row(s) affected" otherwise it shows "SQL statement is not executed!". 

Description of code:

setDate(int index, Date d):
This method sets an arguments to java.sql.Date value. The JDBC driver converts java.sql.Date into SQL DATE value in the database.

Here is the code of progam:

import java.sql.*;

public class SetDate{
  public static void main(String[] args) {
  System.out.println("Prepared statement set date example!");
  Connection con = null;
  Date date = new Date(0000-00-00);
  
  try{
  Class.forName("com.mysql.jdbc.Driver");
  con = DriverManager.getConnection("jdbc:mysql:
//localhost:3306/jdbctutorial","root","root");
  try{
  PreparedStatement prest = con.prepareStatement("INSERT Records VALUES(?,?,?)");
  prest.setInt(1,001);
  prest.setString(2,"Raj kumar");
  prest.setDate(3,date.valueOf("1998-1-17"));
  int row = prest.executeUpdate();
  System.out.println(row + " row(s) affected");
  }
  catch (SQLException s){
  System.out.println("SQL stattement is not executed!");
  }
  }
  catch (Exception e){
  e.printStackTrace();
  }
  }
} 

Download this example.

Database Table: Records

Stu_roll Stu_name Admin_date
1 Raj kumar 1998-01-17
2 Santosh kashyap 1984-12-20
2 Rahul raj 1974-08-27

Output of program:

C:\vinod\jdbc\jdbc\PreparedStatement>javac SetDate.java

C:\vinod\jdbc\jdbc\PreparedStatement>java SetDate
Prepared statement set date example!
1 row(s) affected

After executing the program:

Database Table: Records

Stu_roll Stu_name Admin_date
1 Raj kumar 1998-01-17
2 Santosh kashyap 1984-12-20
2 Rahul raj 1974-08-27
3 vinod kumar 1984-01-12

Tutorials

  1. Mapping MySQL Data Types in Java
  2. Connecting to a MySQL Database in Java
  3. Creating a Database in MySQL
  4. Creating a Database Table
  5. Creating a MySQL Database Table to store Java Types
  6. Description of Database Table
  7. Deleting a Table from Database
  8. Retrieving Tables from a Database
  9. Inserting values in MySQL database table
  10. Retrieving All Rows from a Database Table
  11. Adding a New Column Name in Database Table
  12. Change Column Name of a Table
  13. Make Unique Column in Database Table
  14. Remove Unique Column in Database Table
  15. Arrange a Column of Database Table
  16. Arrange a Column of Database Table
  17. Sum of Column in a Database Table
  18. Deleting All Rows from a Database Table
  19. Delete a Specific Row from a Database Table
  20. Delete a Column from a Database Table
  21. Join tables in the specific database
  22. Join tables with the NATURAL LEFT JOIN operation
  23. Join tables with the NATURAL RIGHT JOIN operation
  24. Cross Join Tables in a Specific Database
  25. Prepared Statement Set Object
  26. Statement Batch Update
  27. Prepared Statement With Batch Update
  28. Select Records Using Prepared Statement
  29. Update Records using Prepared Statement
  30. Inserting Records using the Prepared Statement
  31. Count Records using the Prepared Statement
  32. Deleting Records using the Prepared Statement
  33. Using the Prepared Statement Twice
  34. Set Data Types by using Prepared Statement
  35. Set byte, short and long data types by using the Prepared Statement
  36. Prepared Statement Set Big Decimal
  37. Set Date by using the Prepared Statement
  38. Set Time by using the Prepared Statement
  39. Set Timestamp by using the Prepared Statement
  40. Copy Table in a MySQL Database