Storing todays date?

Hey,

Can some one tell me how I can save today抯 date into a variable? I need it for my project so that when I add a new client it saves the date of when I added the client. I know there抯 a TIMESTAMP built into MySQL but that will change every time I edit the client抯 details. I don抰 need anything over the top just need to simply save the current date and save it to a variable so I can insert it with a SQL statement.

Thanks for all your help

Mike

[473 byte] By [michaeln31a] at [2007-11-26 15:57:28]
# 1
In your method that inserts the new user, create a new TimeStamp (should be automatically initialized to the present day/time) and put that into your sql query
tjacobs01a at 2007-7-8 22:18:22 > top of Java-index,Java Essentials,New To Java...
# 2
java.sql.Date dtCreated = new java.sql.Date(System.currentTimeMillis());~Tim
SomeoneElsea at 2007-7-8 22:18:22 > top of Java-index,Java Essentials,New To Java...
# 3
will that put the date into a sql format? ive tried to use the util.Date import but i must be doing something wrong. anyone one know how to use it?Mike
michaeln31a at 2007-7-8 22:18:22 > top of Java-index,Java Essentials,New To Java...
# 4

> will that put the date into a sql format? ive tried

> to use the util.Date import but i must be doing

> something wrong. anyone one know how to use it?

>

> Mike

pay attention.

import java.sql.Date

NOT

import java.util.Date

notice it was put in the post above yours..

*Edited to make more obvious

Aknibbsa at 2007-7-8 22:18:22 > top of Java-index,Java Essentials,New To Java...
# 5

> will that put the date into a sql format?

Use PreparedStatement.

> ive tried

> to use the util.Date import but i must be doing

> something wrong. anyone one know how to use it?

[url http://java.sun.com/developer/onlineTraining/Database/JDBC20Intro/JDBC20.html]http://java.sun.com/developer/onlineTraining/Database/JDBC20Intro/JDBC20.html[/url]

import java.sql.*;

...

Timestamp ts = new Timestamp (System.currentTimeMillis());

PreparedStatement ps = con.prepareStatement("insert into foo (name, create_date) values (?, ?)");

ps.setString(1, name);

ps.setTimestamp(2, ts);

ps.executeUpdate();

jverda at 2007-7-8 22:18:22 > top of Java-index,Java Essentials,New To Java...
# 6

It's a much better idea to let your DBMS supply the date or time stamp rather than getting your client application to do it (this has nothing to do with being written in Java). This is for consistency reasons: client apps may be on different machines with different clocks (perhaps even in different time zones). Once you figure out how to do it correctly with your DBMS, you'll see that it's even easier than messing about in Java. For example, "record creation" timestamps are usually supplied by "insert triggers".

DrLaszloJamfa at 2007-7-8 22:18:22 > top of Java-index,Java Essentials,New To Java...
# 7

> It's a much better idea to let your DBMS supply the

> date or time stamp rather than getting your client

> application to do it

I don't know. I think it depends on the requirements.

> (this has nothing to do with

> being written in Java). This is for consistency

> reasons: client apps may be on different machines

> with different clocks (perhaps even in different time

> zones).

If everyone's clocks are set correctly, the TZs won't matter.

> Once you figure out how to do it correctly

> with your DBMS, you'll see that it's even easier than

> messing about in Java. For example, "record creation"

> timestamps are usually supplied by "insert triggers".

The downside there is that now you have to take this additional step outside your app that may be different for each DB vendor.

jverda at 2007-7-8 22:18:22 > top of Java-index,Java Essentials,New To Java...
# 8

> It's a much better idea to let your DBMS supply the

> date or time stamp rather than getting your client

> application to do it (this has nothing to do with

> being written in Java). This is for consistency

> reasons: client apps may be on different machines

> with different clocks (perhaps even in different time

> zones). Once you figure out how to do it correctly

> with your DBMS, you'll see that it's even easier than

> messing about in Java. For example, "record creation"

> timestamps are usually supplied by "insert triggers".

True, except i'm doing server apps, so there is consistancy, and the db I'm working with I either don't have that privledge, not sure the db can do that, but your point is very true.

Aknibbsa at 2007-7-8 22:18:22 > top of Java-index,Java Essentials,New To Java...
# 9

> > Once you figure out how to do it correctly

> > with your DBMS, you'll see that it's even easier than

> > messing about in Java. For example, "record creation"

> > timestamps are usually supplied by "insert triggers".

>

> The downside there is that now you have to take this

> additional step outside your app that may be

> different for each DB vendor.

The upside for me is that this is a DB problem, and I let the DB guy worry about it :-)

DrLaszloJamfa at 2007-7-8 22:18:22 > top of Java-index,Java Essentials,New To Java...
# 10

> > > Once you figure out how to do it correctly

> > > with your DBMS, you'll see that it's even easier

> than

> > > messing about in Java. For example, "record

> creation"

> > > timestamps are usually supplied by "insert

> triggers".

> >

> > The downside there is that now you have to take

> this

> > additional step outside your app that may be

> > different for each DB vendor.

>

> The upside for me is that this is a DB problem, and I

> let the DB guy worry about it :-)

I like the way you think!

jverda at 2007-7-8 22:18:22 > top of Java-index,Java Essentials,New To Java...
# 11

> It's a much better idea to let your DBMS supply the

> date or time stamp rather than getting your client

> application to do it (this has nothing to do with

> being written in Java).

In MySQL, I think this is done with the NOW() function, if the triggers don't take care of it.

INSERT INTO table_name

(name, creation_date)

VALUES

('Me', NOW());

kevjavaa at 2007-7-8 22:18:22 > top of Java-index,Java Essentials,New To Java...
# 12

Yah the...

import java.sql.*;

...

Timestamp ts = new Timestamp (System.currentTimeMillis());

PreparedStatement ps = con.prepareStatement("insert into foo (name, create_date) values (?, ?)");

ps.setString(1, name);

ps.setTimestamp(2, ts);

ps.executeUpdate();

...should work alright, i was letting the program deal with the date, better to let the DB deal with, thanks jverd!

FAO Aknibbs relax man, i said i tried to use the java.util! I know the difference between .sql and .util. I might be in the New to Java form but not the New To Reading one! Pay attention!

Oh yah thanks to SomeElse too!

Mike

michaeln31a at 2007-7-8 22:18:22 > top of Java-index,Java Essentials,New To Java...