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
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
java.sql.Date dtCreated = new java.sql.Date(System.currentTimeMillis());~Tim
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
> 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
> 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();
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".
> 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.
> 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.
> > 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 :-)
> > > 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!
> 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());
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