364,083 Members | 5987 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

How to convert String to java.sql.Date()?

prasath03
P: 30
Dear All,

I have one doubt for how to insert the java.sql.Date into ms-sql server.
I tried the below code but it couldn't be inserted. I did wrong in
my code that is ps.setDate(1,full_date_time) to ps.setString(1,full_date_time);
How can i covert the String(full_date_time) to java.sql.Date() and insert the MM/DD/YYYY format in ms-sql database?
I set datetime as datatype in my products table.

<%
SimpleDateFormat sdf=new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
Date d=new Date();

String full_date_time=sdf.format(d).trim();
out.println("Date--------"+full_date_time);
ps = con.prepareStatement("insert into products values(?)");
ps.setDate(1,product_id);
ps.executeUpdate();
%>

thanks in advance


V. Prasath.
Jun 22 '07 #1
Share this Question
Share on Google+
14 Replies


r035198x
10K+
P: 10,083
Dear All,

I have one doubt for how to insert the java.sql.Date into ms-sql server.
I tried the below code but it couldn't be inserted. I did wrong in
my code that is ps.setDate(1,full_date_time) to ps.setString(1,full_date_time);
How can i covert the String(full_date_time) to java.sql.Date() and insert the MM/DD/YYYY format in ms-sql database?
I set datetime as datatype in my products table.

<%
SimpleDateFormat sdf=new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
Date d=new Date();

String full_date_time=sdf.format(d).trim();
out.println("Date--------"+full_date_time);
ps = con.prepareStatement("insert into products values(?)");
ps.setInt(1,product_id);
ps.executeUpdate();
%>

thanks in advance


V. Prasath.
What error message did you get ?
Jun 22 '07 #2

prasath03
P: 30
What error message did you get ?
I wrongly typed in ps.setDate(1,date_of_insert); instead of
ps.setInt(1,date_of_insert);
so please pardon me.


when i compile that coding it throws the followin error......

Incompatible type for method. Can't convert java.lang.String to java.sql.Date.
ps.setDate(1,date_of_insert);
Jun 22 '07 #3

r035198x
10K+
P: 10,083
I wrongly typed in ps.setDate(1,date_of_insert); instead of
ps.setInt(1,date_of_insert);
so please pardon me.


when i compile that coding it throws the followin error......

Incompatible type for method. Can't convert java.lang.String to java.sql.Date.
ps.setDate(1,date_of_insert);
To convert the String to sql Date, you use Date.valueOf (string). The string must be in the format yyyy-mm-dd
Jun 22 '07 #4

sumittyagi
Expert 100+
P: 187
I wrongly typed in ps.setDate(1,date_of_insert); instead of
ps.setInt(1,date_of_insert);
so please pardon me.


when i compile that coding it throws the followin error......

Incompatible type for method. Can't convert java.lang.String to java.sql.Date.
ps.setDate(1,date_of_insert);
at one hand you are saying you want to insert date(and you are using the correct method as well), on the other hand you are passing the string object to it. Pass date object only. You don't have to worry about the database date format for that, setDate function will be taking care of that.
Jun 22 '07 #5

r035198x
10K+
P: 10,083
at one hand you are saying you want to insert date(and you are using the correct method as well), on the other hand you are passing the string object to it. Pass date object only. You don't have to worry about the database date format for that, setDate function will be taking care of that.
Yes, there's no need to change the date to string in the first place.
Jun 22 '07 #6

prasath03
P: 30
To convert the String to sql Date, you use Date.valueOf (string). The string must be in the format yyyy-mm-dd
thanks for reply..

i tried the following code to ur suggestion but it shows the
the following error: java.lang.IllegalArgumentException
at java.sql.Date.valueOf(Date.java:104)

java.sql.Date dt = java.sql.Date.valueOf(full_date_time);
Jun 22 '07 #7

r035198x
10K+
P: 10,083
thanks for reply..

i tried the following code to ur suggestion but it shows the
the following error: java.lang.IllegalArgumentException
at java.sql.Date.valueOf(Date.java:104)

java.sql.Date dt = java.sql.Date.valueOf(full_date_time);
Look at my first reply again, it requires the date to be in yyyy-mm-dd format. Better still don't change the date to the String at all. Just set it as a Date objec without formating it with the date format.
Jun 22 '07 #8

sumittyagi
Expert 100+
P: 187
thanks for reply..

i tried the following code to ur suggestion but it shows the
the following error: java.lang.IllegalArgumentException
at java.sql.Date.valueOf(Date.java:104)

java.sql.Date dt = java.sql.Date.valueOf(full_date_time);
I am not able to understand what u are trying to do.
you first converted date to string.
then again converting string to date.
what for?

and second thing, you can't use valueOf(String) function here (I havn't heard of this function in date class, but it might exist for any other purpose) because, how will the date class be knowing what format your date string is in. there are thousands of formats of a date string.
You can get your date object back from string with SimpleDateFormat object only, by using the parse(String) function.
and if the date string is not in the same format as of SimpleDateFormat object then it will throw some parse Exception(I don't remember the exact exception).

But I don't understand why u need to convert date to string in the first place.
Jun 22 '07 #9

Velmurugan1982
P: 2
Prasath,

did u get solution for this problem??? If so, please let me know...

Thanks
Nov 20 '07 #10

r035198x
10K+
P: 10,083
Prasath,

did u get solution for this problem??? If so, please let me know...

Thanks
Did you read the replies in this thread?
What problem are you getting?
Nov 20 '07 #11

Velmurugan1982
P: 2
Did you read the replies in this thread?
What problem are you getting?
I have to insert a date field in Oracle database from my java code. But the date field format in DB is MM/DD/YYYY HH/MM/SS AM/PM. I had read the earlier replies. But it is meant to only MM/DD/YY format. But in my case, the format must include the full date & time and also the am/pm notation. please advice.
Nov 20 '07 #12

r035198x
10K+
P: 10,083
I have to insert a date field in Oracle database from my java code. But the date field format in DB is MM/DD/YYYY HH/MM/SS AM/PM. I had read the earlier replies. But it is meant to only MM/DD/YY format. But in my case, the format must include the full date & time and also the am/pm notation. please advice.
Have a look at Oracle's TO_DATE function.
Nov 20 '07 #13

heat84
100+
P: 118
I have to insert a date field in Oracle database from my java code. But the date field format in DB is MM/DD/YYYY HH/MM/SS AM/PM. I had read the earlier replies. But it is meant to only MM/DD/YY format. But in my case, the format must include the full date & time and also the am/pm notation. please advice.
Will java.sql.Date be the accurate date since you want the date up to the second. Doesn't it give only 00/00/00 for the hours , minutes and seconds. I think java.util.Date is more accurate.
Nov 20 '07 #14

r035198x
10K+
P: 10,083
Will java.sql.Date be the accurate date since you want the date up to the second. Doesn't it give only 00/00/00 for the hours , minutes and seconds. I think java.util.Date is more accurate.
Why not use java.sql.Timestamp?
Nov 20 '07 #15

Post your reply

Help answer this question



Didn't find the answer to your Java question?

You can also browse similar questions: Java