aspose file tools*
The moose likes JDBC and the fly likes Date, time and timestamp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Date, time and timestamp" Watch "Date, time and timestamp" New topic
Author

Date, time and timestamp

Yudi Lukman
Greenhorn

Joined: Jul 06, 2001
Posts: 1
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.
smitha rai
Greenhorn

Joined: Jun 29, 2001
Posts: 18
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();

}
}

================================================================
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Date, time and timestamp