JavaServer Pages (JSP) and JSTL - how to insert date in sqlserver in (yyyy-MM-dd HH:mm:ss) forma

please help how to insert date in sqlserver in (yyyy-MM-dd HH:mm:ss) format, I have tried java.sql.Date but in that case the time portion automatically changed to 00:00:00 and while using java.sql.Timestamp it through me an error as data type mismatch in data base.

I have set data type as datetime in sqlserver instead of timestamp the reason is I have two datetime column in my table and if I use timestamp it will not allow to columns................

thanks in advance

Amit

[501 byte] By [Amit_bhardwaja] at [2007-11-14]
# 1

Hi,

using now() function in mysql database sysdate in oracle.

or

java.util.Date dt = new java.util.Date();

java.sql.Date sdt = new java.sql.Date(dt.getYear(),dt.getMonth(),dt.getDate(),dt.getHours(),dt.getMinutes,getSeconds());

String rdate = sdt.toString();

System.out.println(rdate);

sure_2912a at 2007-7-10 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 2
set data type of date field as timestampand use now()it will insert it into database as u want
harrriiiia at 2007-7-10 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 3

Hi..thanks to both of you for ur response but the issue is I m taking values from user input form and I have converted those parameters to yyyy-MM-dd HH:mm:ss form using SimpleDateFormat so I guess using now() will not help me, second I cannot set datatype field as timestamp bcoz in sqlserver only one column is allowed as timestamp and I require start time and end time columns.....waiting for ur response or anyother suggestion

Regards,

Amit

Amit_bhardwaja at 2007-7-10 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 4

when you say you are getting the values from user input you mean you ask user:

day? and gives you say 24?

String insert ="Insert INTO table(yourDate) Values(''"+year+"-"+month+"-"+day+"')";

Statement stmt = con.createConnection();

ResultSet rs = stmt.executeUpdate(insert);

Otherwise please specify

despinaa at 2007-7-10 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 5

datetime is the correct sql data type for your purpose. Timestamp is something completely different.

java.sql.Timestamp IS what you want to use. Theoretically it should map to a MSSQL datetime.

That detail should be handled by the JDBC driver.

What JDBC driver are you using?

Format of the date is irrelevant. You should create it as a java.sql.timestamp

So take your prepared statment

String sql = "insert into myTable(id, startTime, endTime) values (?,?,?)";

PreparedStatement stmt = conn.prepareStatement(sql);

stmt.setInt(1, record.getId());

stmt.setTimestamp(2, ....)

stmt.setTimestamp(3, ...)

Given a java.util.Date, the best way to get a java.sql.Timestamp is via

new java.sql.Timestamp(date.getTime());

evnafetsa at 2007-7-10 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 6
Hey thanks a lot buddy..its working fine...I more thing I like to knw if u can help me....is there a way to remove .f from timestamp like i m getting out in HH:mm:ss.f while printing timestamp..........Regards,Amit
Amit_bhardwaja at 2007-7-10 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 7

How are you outputting the timestamp? With just a println()?

Run it through another SimpleDateFormat to convert it to string.

Basically you should communicate with the database using java.sql.Timestamp.

For displaying the value, use SimpleDateFormat to get the correct format.

evnafetsa at 2007-7-10 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 8

Yes ur absolutly correct but the thing is now I have to retrieve the data which I have inserted ok lets say this way:

Select * from table where ? between StartTime and EndTime;

PreparedStatement ps = con.prepareStatement(qry);

ps.setTimestamp(1, starttime);

and so on.....

but the issue is I m not getting anything so checked the reason and found that timestamp setting the data in yyyy-MM-dd HH:mm:ss.f format and in my database its showing in yyyy-MM-dd HH:mm:ss AM/PM format (which I inserted through the option suggested by you earlier)

now plz suggest me how to retrieve the data....

Amit_bhardwaja at 2007-7-10 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 9
Hey thanx a lot for ur support problem has been resolved....Gr8...Regards,Amit
Amit_bhardwaja at 2007-7-10 > top of java,Enterprise & Remote Computing,Web Tier APIs...