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.

Ads

Tutorials   
Change Column Name in MySQL Java Mysql Connection Example JDBC Example with MySQL Jdbc Mysql Connection String Jdbc Mysql Connection Url Getting Column Names from a database table in Java Count Rows from a Database Table JDBC Example with MySQL Write Text File to Table Inserting Image in Database Table Insert Data in Table Using Stored Procedure Copy One Database Table to Another Copy Table in a MySQL Database Set Timestamp by using the Prepared Statement Set Time by using the Prepared Statement Set Date by using the Prepared Statement Prepared Statement Set Big Decimal Set byte, short and long data types by using the Prepared Statement Set Data Types by using Prepared Statement Using the Prepared Statement Twice Deleting Records using the Prepared Statement Count Records using the Prepared Statement Inserting Records using the Prepared Statement Update Records using Prepared Statement Select Records Using Prepared Statement Prepared Statement With Batch Update Statement Batch Update Prepared Statement Set Object Cross Join Tables in a Specific Database Join tables with the NATURAL RIGHT JOIN operation Join tables with the NATURAL LEFT JOIN operation Join tables in the specific database Delete a Column from a Database Table Delete a Specific Row from a Database Table Deleting All Rows from a Database Table Sum of Column in a Database Table Arrange a Column of Database Table Arrange a Column of Database Table Remove Unique Column in Database Table Make Unique Column in Database Table Change Column Name of a Table Adding a New Column Name in Database Table Retrieving All Rows from a Database Table Inserting values in MySQL database table Retrieving Tables from a Database Deleting a Table from Database Description of Database Table Creating a MySQL Database Table to store Java Types Creating a Database Table Creating a Database in MySQL
Ads

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

Advertisements

Ads
Share on Google+Share on Google+

Set Date by using the Prepared Statement

Posted on: April 13, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Set Date by using the Prepared Statement   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads