You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Uma Maheswar <um...@globalleafs.com> on 2002/02/11 19:00:43 UTC

Inserting DATE into SQL server

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

Re: Inserting DATE into SQL server

Posted by Jonathan Eric Miller <je...@uchicago.edu>.
Unless your table only has one column, you need to specify which column you
are setting the value for in your INSERT statement.

You should also use the JDBC escape sequence for a date as well that way it
is DBMS independent.

INSERT INTO table1 (column1) VALUES ({d 'yyyy-mm-dd'})

Jon

----- Original Message -----
From: "Uma Maheswar" <um...@globalleafs.com>
To: "Tomcat Users List" <to...@jakarta.apache.org>
Sent: Monday, February 11, 2002 12:00 PM
Subject: Inserting DATE into SQL server


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:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


Re: Inserting DATE into SQL server

Posted by ba...@generationjava.com.
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:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>