tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From <bay...@generationjava.com>
Subject RE: Inserting DATE into SQL server
Date Mon, 11 Feb 2002 18:39:22 GMT
Good call. I forgot you needed a java.sql.Date rather than a
java.util.Date for the statement.

The provided code didn't have int year/month/date, so it would have to
change that to ints then use the Calendar. Shouldn't be a problem though.

And I'm in favour of using PreparedStatements all the time. Only time to
not use them being:

1) When you want to use CallableStatement for stored procedures.
2) Try and get around driver bugs or PreparedStatement caching bugs.

Bay

On Mon, 11 Feb 2002, [iso-8859-1] Reynir Hübner wrote:

> another way might be to parse it into java.sql.Date
> for example :
>   Calendar cal = Calendar.getInstance();
>   cal.set(int year, int month, int date);
>   java.sql.Date sqlDate = new java.sql.Date(cal.getTime().getTime());
>
>
> then use sqlDate in your insert statement. this would leave your time
> part of the dateTime empty. (00.00.00).
> I also sometimes (depending on the database) use java.sql.TimeStamp for
> DateTime fields, cause it includes both time and date, but of course
> that would not help if you dont have the information needed to populate
> the time.
>
> hope it helps,
>   -reynir
>
> ps. bayard, when would you not use prepared statements ? my experiments
> have shown very improved performance over normal Statements....
>
>
>
>
>
>
> > -----Original Message-----
> > From: bayard@generationjava.com [mailto:bayard@generationjava.com]
> > Sent: 11. febrúar 2002 18:06
> > To: Tomcat Users List
> > Subject: Re: Inserting DATE into SQL server
> >
> >
> > This is the kind of thing you want to use a PreparedStatement
> > for. It will
> > take care of the date-formatting for you.
> >
> > DateFormat format = new SimpleDateFormat("MM/dd/yyyy");
> > Date date = format.parse(month+"/"+date+"/"+year);
> > PreparedStatement ps = connection.prepareStatement("INSERT INTO test
> > VALUES(?)");
> >
> > ps.setDate(date);
> > ps.execute();
> >
> > or some such code close to that.
> >
> > Bay
> >
> > On Mon, 11 Feb 2002, Uma Maheswar wrote:
> >
> > > Hi,
> > > I wanted to insert date values into the database in SQL
> > Server.The data
> > >
> > > type I used in SQL server is "DATETIME". I have <select>
> > tags used in
> > >
> > > HTML page to ask the user select the date. I am doing it in this way
> > >
> > > month = request.getParameter("month");
> > > date = request.getParameter("date");
> > > year = request.getParameter("year");
> > >
> > > totdate = month+"/"+date+"/"+year;
> > >
> > > insert into test values('"+totdate+"');
> > >
> > > I get error in Tomcat saying that the data type or the number of
> > >
> > > columns are mismatched. How do I insert date into SQL server.
> > >
> > > Thanks for any help.
> > >
> > > Uma
> > >
> >
> >
> > --
> > To unsubscribe:   <mailto:tomcat-user-unsubscribe@jakarta.apache.org>
> > For additional commands: <mailto:tomcat-user-help@jakarta.apache.org>
> > Troubles with the list: <mailto:tomcat-user-owner@jakarta.apache.org>
> >
> >
>
> --
> To unsubscribe:   <mailto:tomcat-user-unsubscribe@jakarta.apache.org>
> For additional commands: <mailto:tomcat-user-help@jakarta.apache.org>
> Troubles with the list: <mailto:tomcat-user-owner@jakarta.apache.org>
>
>


--
To unsubscribe:   <mailto:tomcat-user-unsubscribe@jakarta.apache.org>
For additional commands: <mailto:tomcat-user-help@jakarta.apache.org>
Troubles with the list: <mailto:tomcat-user-owner@jakarta.apache.org>


Mime
View raw message