Mega Code Archive

 
Categories / Java Tutorial / Database
 

Set Timestamp by using the Prepared Statement

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)");   } }