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 |

|
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