JDBC Tutorials - Herong's Tutorial Examples - Version 2.21, by Dr. Herong Yang

Handling Date and Timestamp Values

This section describes how to handle Date and Timestamp Values.

When you are dealing with Date or Timestamp values in database, you should use the data type classes provided in the java.sql.* package, not the java.lang.* package. java.sql.* classes are better mapped to database data types. For example, java.sql.Date class is mapped to the database DATE data type better than java.lang.Date class. The toString() method of java.sql.Date class will format a Date object to the format yyyy-mm-dd supported by most database servers.

The following sample Java program shows you how to use java.sql.Date and java.sql.Timestamp classes to handle date and timestamp values in SQL statements and result sets:

/**
 * DerbyInsertDates.java
 * Copyright (c) 2007 by Dr. Herong Yang. All rights reserved.
 */
import java.sql.*;
public class DerbyInsertDates {
  public static void main(String [] args) {
    Connection con = null;
    try {
      con = DriverManager.getConnection(
        "jdbc:derby://localhost/TestDB");
      Statement sta = con.createStatement(); 

// Converting Java Date to SQL TIMESTAMP
      int count = 0;
      java.sql.Date birthDate = java.sql.Date.valueOf("1980-02-29");
      java.sql.Timestamp now = new java.sql.Timestamp(
        System.currentTimeMillis());
      count += sta.executeUpdate(
        "INSERT INTO Profile"
        + " (FirstName, LastName, BirthDate, ModTime)"
        + " VALUES ('Keith', 'Harris', '"+birthDate+"', '"+now+"')");
      System.out.println("Number of rows inserted: "+count);

// Checking inserted rows
      ResultSet res = sta.executeQuery(
        "SELECT * FROM Profile");
      System.out.println("List of Profiles: "); 
      while (res.next()) {
         System.out.println(
           "  "+res.getInt("ID")
           + ", "+res.getString("FirstName")
           + ", "+res.getString("LastName")
           + ", "+res.getFloat("Point")
           + ", "+res.getDate("BirthDate")
           + ", "+res.getTimestamp("ModTime"));
      }
      res.close();

      sta.close();
      con.close();
    } catch (Exception e) {
      System.err.println("Exception: "+e.getMessage());
    }
  } 
}

Noticed that I used res.getTimestamp() instead of res.getDate() to retrieve time and millisecond information:

Number of rows inserted: 1
List of Profiles:
  1, Herong, null, 0.0, 1988-12-31, 2006-12-31 23:59:59.999
  2, Janet, Gates, 999.99, 1984-10-13, 2006-12-31 23:59:59.999
  3, 144, 14859, 615.1516, 1988-12-31, 2006-12-31 23:59:59.999
  4, 21d6, efd17, 289.4137, 1988-12-31, 2006-12-31 23:59:59.999
  5, 977, 6779f, 766.5425, 1988-12-31, 2006-12-31 23:59:59.999
  6, 1002, e3873, 996.2754, 1988-12-31, 2006-12-31 23:59:59.999
  7, 1b2c, 8f798, 594.6182, 1988-12-31, 2006-12-31 23:59:59.999
  8, 11bd, 58ad0, 741.9156, 1988-12-31, 2006-12-31 23:59:59.999
  9, 1352, 17d9, 274.19855, 1988-12-31, 2006-12-31 23:59:59.999
  10, 13ba, 88356, 115.04227, 1988-12-31, 2006-12-31 23:59:59.999
  11, 1090, 3fb07, 767.21246, 1988-12-31, 2006-12-31 23:59:59.999
  12, 19c2, 8770b, 383.88382, 1988-12-31, 2006-12-31 23:59:59.999
  13, Keith, Harris, 0.0, 1980-02-29, 2007-04-01 16:10:29.906

Table of Contents

 About This Book

 JDBC (Java Database Connectivity) Introduction

 Downloading and Installing JDK - Java SE

 Installing and Running Java DB - Derby

 Derby (Java DB) JDBC Driver

 Derby (Java DB) JDBC DataSource Objects

Java DB (Derby) - DML Statements

 Tables with Primary Key Column "GENERATED ... AS IDENTITY"

 "INSERT INTO" Statements

 "INSERT INTO" Statements with INDENTITY Columns

Handling Date and Timestamp Values

 "UPDATE" Statements

 "DELETE FROM" Statements

 Java DB (Derby) - ResultSet Objects of Queries

 Java DB (Derby) - PreparedStatement

 MySQL Installation on Windows

 MySQL JDBC Driver (MySQL Connector/J)

 MySQL - PreparedStatement

 MySQL - Reference Implementation of JdbcRowSet

 MySQL - JBDC CallableStatement

 MySQL CLOB (Character Large Object) - TEXT

 MySQL BLOB (Binary Large Object) - BLOB

 Oracle Express Edition Installation on Windows

 Oracle JDBC Drivers

 Oracle - Reference Implementation of JdbcRowSet

 Oracle - PreparedStatement

 Oracle - JBDC CallableStatement

 Oracle CLOB (Character Large Object) - TEXT

 Oracle BLOB (Binary Large Object) - BLOB

 Microsoft SQL Server 2005 Express Edition

 Microsoft JDBC Driver for SQL Server - sqljdbc.jar

 Microsoft JDBC Driver - Query Statements and Result Sets

 Microsoft JDBC Driver - DatabaseMetaData Object

 Microsoft JDBC Driver - DDL Statements

 Microsoft JDBC Driver - DML Statements

 SQL Server - PreparedStatement

 SQL Server CLOB (Character Large Object) - TEXT

 SQL Server BLOB (Binary Large Object) - BLOB

 JDBC-ODBC Bridge Driver - sun.jdbc.odbc.JdbcOdbcDriver

 JDBC-ODBC Bridge Driver - Flat Text Files

 JDBC-ODBC Bridge Driver - MS Access

 JDBC-ODBC Bridge Driver - MS SQL Server

 Summary of JDBC Drivers and Database Servers

 Additional Tutorial Notes to Be Added

 References

 PDF Printing Version

Handling Date and Timestamp Values - Updated in 2014, by Dr. Herong Yang