Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Jdbc Jdbc-mysql Set Date by using the Prepared Statement

Related Tutorials


 
 

Share on Google+Share on Google+

Set Date by using the Prepared Statement

Advertisement
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

     

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
Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 13, 2007

Related Tutorials

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
DMCA.com