Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials
  

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
JDBC
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
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. 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

                         

Facing Programming Problem?
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

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.

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

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

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

Copyright © 2008. All rights reserved.