title: Best way to insert date and timestamp into database using java
body: <p>Before finishing off the coding, I would like to know what would be the best way to insert date and time stamp in database using java. </p>
  
  <p>Database could be oracle, mssql, db2 or mysql. </p>
  
  <p>I want to insert date and time stamp in a way so that its retrieval should be easy for reconciliation of records. Lets say our DB admin might want to pull records between range Aug 2015 and Oct 2015 and some time range as well.</p>
  
  <p><strong>Option 1:</strong> using different sql query scripts for each database type</p>
  
  <p><strong>Example:</strong> </p>
  
  <pre><code>INSERT INTO YOUR_TABLE (YOUR_DATE_COLUMN, COLUMN_2, COLUMN_3) 
  VALUES (SYSDATE, 1234, 567);
  </code></pre>
  
  <p><strong>Option 2:</strong> using java syntax as follows</p>
  
  <p><strong>Example:</strong> </p>
  
  <pre><code>java.util.Date date = new java.util.Date();
  long t = date.getTime();
  java.sql.Date sqlDate = new java.sql.Date(t);
  java.sql.Time sqlTime = new java.sql.Time(t);
  java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp(t);
  
  pstmt.setDate(2, sqlDate);
  pstmt.setTime(3, sqlTime);
  pstmt.setTimestamp(4, sqlTimestamp);
  pstmt.executeUpdate();
  </code></pre>
  
  <p>Would option 2 handle proper insertion of date and timestamp in different db types?</p>