Insert Timestamp to database in Java
Description
The following code shows how to insert Timestamp to database.
Example
/* w w w. j a v a 2 s. c o m*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Timestamp;
public class Main {
public static void main(String[] argv) throws Exception {
Timestamp tstamp = new Timestamp(0);
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial",
"root", "root");
String sql = "INSERT myTable VALUES(?,?)";
PreparedStatement prest = con.prepareStatement(sql);
prest.setString(1, "x");
prest.setTimestamp(2, tstamp.valueOf("2009-02-24 12:51:42.11"));
int row = prest.executeUpdate();
System.out.println(row + " row(s) affected)");
}
}