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

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Set Time by using the Prepared Statement 
 

In this section you will learn about setting the time in database table by using the PreparedStatement interface of java.sql package.

 

Set Time by using the Prepared Statement

                         

In this section you will learn about setting the time in database table by using the PreparedStatement interface of java.sql package. Suppose you want to insert the actual time in a table, here you will provide a setTime method for setting the time. See below for an example with code that insert the time in JDBC table.

Description of program:

This example helps us for setting the time in database table (child) by using the PreparedStatement interface. First of all we need to establish the connection with MySQL database through the help of JDBC deriver. After establishing the connection, we will insert our query in the prepareStatement method that returns the PreparedStatement object. With the help of this object we insert all data with time. Here, we will only insert the child name and born time by using the setString and setTime methods. Finally you will get all data that are added in the database table (child) and it will show "row(s) affected" otherwise it will display a message "SQL statement is not executed!".

Description of code:

setTime(int index, Time t);
Above method takes integer type value and time type value. This method sets the arguments to java.sql.Time value that is converted into SQL TIME value.

Here is the code program:

import java.sql.*;

public class SetTime{
  public static void main(String[] args) {
    System.out.println("Prepared Statement Set Time example!");
    Connection con = null;
    Time time = new Time(00-00-00);
    try{
      Class.forName("com.mysql.jdbc.Driver");
      con = DriverManager.getConnection("jdbc:mysql:
//localhost:3306/jdbctutorial"
,"root","root");
      try{
        String sql = "INSERT child VALUES(?,?)";
        PreparedStatement prest = con.prepareStatement(sql);
        prest.setString(1,"vinod");
        prest.setTime(2,time.valueOf("1:60:60"));
        int row = prest.executeUpdate();
        System.out.println(row + " row(s) affectec)");
      }
      catch (SQLException e){
        System.out.println("SQL statement is not executed!");
      }
    }
    catch (Exception e){
      e.printStackTrace();
    }
  }
}

Download this example.

Database Table: child

ch_name b_time
Santosh 12:08:36
vinod 02:01:00

Output of program:

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

C:\vinod\jdbc\jdbc\PreparedStatement>java SetTime
Prepared Statement Set Time example!
1 row(s) affectec)

After executing the program:

Database Table: child

ch_name b_time
Santosh 12:08:36
vinod 02:01:00
Deepak 09:45:57

                         

» 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:

please tell us to call stored procedure from mysql.for rest of the tutorial i just really hats off to you.
thanks a lot
rose india





raghu pradeep

Posted by raghu on Wednesday, 03.28.07 @ 13:16pm | #12832

hello,
i really thank u first, i was in search for
a good book which teaches jdbc in brief really
this tutorial is very good and covered what we required for to developing projects.
Thank u rose india

Regrads
Mallikarjun Haranal

Posted by Mallikarjun Haranal on Wednesday, 02.21.07 @ 17:12pm | #8727

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.