• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Date, time and timestamp

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I'm new to Java programming and I want to use date and time type for my database. I use MS Access as my database. Is there anyone have a complete program using date, time, or timestamp ? I can't understand if it's only a part of the program. Afterall, I'm really new. Thanks.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Yudi,
You may get some clues from the following code. Please note that in the code "JDBCMsAccessConnection" represents a class that establishes connection to the database.
Good luck,
Smitha
================================================================

import java.util.*;
import java.sql.*;
import java.lang.*;
import java.text.*;

public class DateTest_MsAccess{


public static java.sql.Timestamp getTstamp() {
java.util.Date date = (Calendar.getInstance()).getTime();
java.sql.Timestamp tStamp = new java.sql.Timestamp(date.getTime());
// OR java.sql.Timestamp tStamp = new java.sql.Timestamp(System.currentTimeMillis());
System.out.println("The Time Stamp : " + tStamp);
return tStamp;
}


public void insertData() {

Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
Calendar cal = Calendar.getInstance();

JDBCMsAccessConnection joc = new JDBCMsAccessConnection();

try {

con = joc.getAccessConnection();

con.setAutoCommit(false);

java.util.Date myDate = new java.util.Date();
java.sql.Date sqlDate = new java.sql.Date(myDate.getTime());

String query = "INSERT INTO TESTING VALUES(?,?,?)";
pstmt=con.prepareStatement(query);
pstmt.setInt(1,600);
pstmt.setTimestamp(2, getTstamp());
// U can also use the following statement which inserts only time
//pstmt.setDate(2, sqlDate);
pstmt.setString(3,"CCCC");
pstmt.executeUpdate();

con.commit();


} catch(SQLException ex) {
System.err.println("SQLException Occurred in method insertData() : " + ex.getMessage());
ex.printStackTrace();
} catch(Exception e){
System.out.println("Exception Occurred in method insertData() :" + e);
}
finally {

joc.closeAll(pstmt, con);

}
}
public void ReadData() {

Connection con = null;
Statement stmt = null;
ResultSet rs = null;

JDBCMsAccessConnection joc = new JDBCMsAccessConnection();

try {

con = joc.getAccessConnection();
stmt = con.createStatement();

java.util.Date dt = new java.util.Date();

String query = "select * from testing";


con.commit();
rs = stmt.executeQuery (query);

while (rs.next()) {
System.out.println(rs.getInt(1) + " " + rs.getString(2) + " " + rs.getString(3));


}

} catch(SQLException ex) {
System.err.println("SQLException Occurred in method ReadData() : " + ex.getMessage());
ex.printStackTrace();
} catch(Exception e){
System.out.println("Exception Occurred in method ReadData() :" + e);
}
finally {

joc.closeAll(rs, stmt, con);
}
}

public static void main (String args[]){

java.util.Date dat = new java.util.Date();
System.out.println("Current Date is : " + dat.getTime());

DateTest_MsAccess dT = new DateTest_MsAccess();

dT.insertData();
dT.ReadData();

}
}

================================================================
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic