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.

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

Ads

Ads

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

Advertisements

Ads
Share on Google+Share on Google+

Set Time 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 Time by using the Prepared Statement   View All Comments

Post your Comment


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

Ads

 

Ads