This content has been marked as final. Show 9 replies
Are the column types DATE and DATETIME, respectively?
The column is datetime, its one column and I get both the styles of date in it.
are u formatting the date in java or just creates the date object?
I suppose you use sql date object, right?
Here's my few code snippets.
import java.sql.Date; /* */ Date cdate = new Date(new java.util.Date().getTime()); Connection con = null; /* */ con = ConnectionFactoryWrapper.getConnection("sqlServer"); ps = con.prepareStatement(insert); ps.setInt(1, id); ps.setInt(2, rating); ps.setString(3, comments); ps.setTime(4, cdate.getTime()); ps.setString(5, completed); ps.execute(); /* */
Yup. Those are the next questions:
Show me the code:
1. How do you insert dates?
2. How do you retrieve dates?
3. How do you format dates for display?
ps.setTime(4, cdate.getTime());??? You've never stated what you want in that column -- the date, the date&time, or just the time?
Date and time is preferred. Right now it randomly does both so when I run a query on the data it doesn't make it easy to grab it all.
Date and time is preferred.In that case, I would use java.sql.Timestamp and PreparedStatement method setTimestamp. A java.sql.Date is supposed to be normalized to have hours, minutes, seconds and millis all set to 0.
The problem cold be this
remember, getTome() returns the date in miliseconds, my solutions is:
you insert statement cold look like "insert into table vales (CAST('?' AS datetime))"
and add this to your code
GregorianCalendar gc = new GregorianCalendar(); gc.setTime(cdate.getTime()); int year = gc.get( GregorianCalendar.YEAR ); int month = gc.get( GregorianCalendar.MONTH )+1; int day = gc.get( GregorianCalendar.DAY_OF_MONTH ); String cDate = month+"/"+day+"/"year; //instead of //ps.setTime(4, cdate.getTime()); //use ps.setString(4, cDate);