Getting date time from MySQL using Java JDBC

Since I seem to forget how to get/convert dates from the database into Java using JDBC (I use Oracle and MySQL for different projects), a reminder:

Suppose there is a MySQL table structure like:

create table prp_models (
  prp_id        int(10) unsigned not null auto_increment,
  prp_created   datetime default null,
  prp_
)

Then a proper way to get the prp_created column from a resultset (res) is:

java.sql.Timestamp myDateTime = res.getTimestamp(2);  // res #1 is the ID

If you want to get the date time in a displayable format (String), you can use:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(myDateTime.getTime());

Note on the side: within Java you can set myDateTime with system time using:
myDateTime = new Timestamp(System.currentTimeMillis());

weblog launched

Today dotJava got it’s own weblog, just like many others on the web.

Main goal of this weblog is to collect scripts, notes and other interesting documents found around the net. At any time it’s possible for me to access the collection and in the meantime others can enjoy the collection as well.

So, enjoy!