Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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

                         

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

                         

» View all related tutorials
Related Tags: c database query ide table data io column numbers number sum using this id simple tab student university if ie

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

2 comments so far (
post your own) View All Comments Latest 10 Comments:

Hi i am using jdbc & mysql, i want to how to compare the date datatype between the columns.
Thanks & Regards
Sundaravadivel.K

Posted by sundaravadivel on Wednesday, 06.18.08 @ 20:21pm | #63749

ok its fine when we entered date manually,if we have to take data from database,EX: sysdate from database(oracle),how can we read it using ResultSet rs,rs.getDate or anything else give an example if ucan

Posted by satya srinivas on Sunday, 03.2.08 @ 00:47am | #50888

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.