Insert TIMESTAMP value : Date Time Timestamp « Database « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » Database » Date Time Timestamp 
20.20.6.Insert TIMESTAMP valuePrevious/Next
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;

public class Main {
  public static void main(String[] argsthrows Exception {
    Connection conn = getConnection();
    Statement st = conn
        .createStatement();

    st.executeUpdate("create table survey (id int,myDate TIMESTAMP );");
    String INSERT_RECORD = "insert into survey(id, myDate) values(?, ?)";
    
    PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD);
    pstmt.setString(1"1");
    java.sql.Timestamp  sqlDate = new java.sql.Timestamp(new java.util.Date().getTime());
    pstmt.setTimestamp(2, sqlDate);
    
    pstmt.executeUpdate();
    
    ResultSet rs = st.executeQuery("SELECT * FROM survey");

    outputResultSet(rs);

    rs.close();
    st.close();
    conn.close();
  }
  private static void outputResultSet(ResultSet rsthrows Exception {
    ResultSetMetaData rsMetaData = rs.getMetaData();
    int numberOfColumns = rsMetaData.getColumnCount();
    for (int i = 1; i < numberOfColumns + 1; i++) {
      String columnName = rsMetaData.getColumnName(i);
      System.out.print(columnName + "   ");

    }
    System.out.println();
    System.out.println("----------------------");

    while (rs.next()) {
      for (int i = 1; i < numberOfColumns + 1; i++) {
        System.out.print(rs.getString(i"   ");
      }
      System.out.println();
    }

  }

  private static Connection getConnection() throws Exception {
    Class.forName("org.hsqldb.jdbcDriver");
    String url = "jdbc:hsqldb:mem:data/tutorial";

    return DriverManager.getConnection(url, "sa""");
  }
}
ID   MYDATE   
----------------------
1   2007-02-02 12:45:19.914000000
20.20.Date Time Timestamp
20.20.1.Working with Date, Time, and Timestamp in JDBC
20.20.2.Insert Date value
20.20.3.Get Date value from ResultSet
20.20.4.Insert TIME value
20.20.5.Get TIME value from ResultSet
20.20.6.Insert TIMESTAMP value
20.20.7.Get TIMESTAMP value from ResultSet
20.20.8.Get the Current Date as a java.util.Date Object
20.20.9.Creating java.sql.Date Using java.util.Date
20.20.10.Creating java.sql.Date Using java.util.Calendar
20.20.11.Creating java.sql.Date Using the java.sql.Date.valueOf() Method
20.20.12.Creating java.sql.Date Using GregorianCalendar
20.20.13.Getting a Timestamp Object Using java.util.Date
20.20.14.Getting a Timestamp Object Using System.currentTimeMillis()
20.20.15.Get the Current Timestamp as a java.sql.Time Object?
20.20.16.Convert a String Date Such as 2003/01/10 to a java.sql.Date Object?
20.20.17.Convert a Timestamp to Month-Day-Year
20.20.18.Get a Date Label from a java.sql.Timestamp Object
20.20.19.Convert a java.sql.Timestamp Object to a java.util.Date Object?
20.20.20.Make a java.sql.Timestamp Object for a Given Year, Month, Day, Hour
20.20.21.Get a Date for a Specific Time Zone
20.20.22.Returns a java.sql.Timestamp equal to the current time
20.20.23.Convert into java.sql.Time into java.util.Calendar
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.