如何将String转换为java.sql.Date()? [英] How to convert String to java.sql.Date()?

查看:301
本文介绍了如何将String转换为java.sql.Date()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,


我对如何将java.sql.Date插入ms-sql服务器有一个疑问。

我试过以下代码,但它无法插入。我的错误在

我的代码是ps.setDate(1,full_date_time)到ps.setString(1,full_date_time);

我怎样才能转换字符串(full_date_time )到java.sql.Date()并在ms-sql数据库中插入MM / DD / YYYY格式?

我在我的产品表中将datetime设置为数据类型。


<%

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

日期d =新日期();


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();

%>


提前感谢



V. Prasath。

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.

推荐答案


亲爱的所有人,


我对如何将java.sql.Date插入ms-sql server感到疑惑。

我尝试了以下代码,但无法插入。我的错误在

我的代码是ps.setDate(1,full_date_time)到ps.setString(1,full_date_time);

我怎样才能转换字符串(full_date_time )到java.sql.Date()并在ms-sql数据库中插入MM / DD / YYYY格式?

我在我的产品表中将datetime设置为数据类型。


<%

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

日期d =新日期();


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();

%>


提前感谢



V. Prasath。
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 ?



发生了什么错误信息你得到 ?
What error message did you get ?



我错误地输入了ps.setDate(1,date_of_insert);而不是

ps.setInt(1,date_of_insert);

所以请原谅我。



当我编译它编码会引发跟随错误......


方法的类型不兼容。无法将java.lang.String转换为java.sql.Date。

ps.setDate(1,date_of_insert);

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);



我错误地输入了ps.setDate(1,date_of_insert);而不是

ps.setInt(1,date_of_insert);

所以请原谅我。



当我编译它编码会引发跟随错误......


方法的类型不兼容。无法将java.lang.String转换为java.sql.Date。

ps.setDate(1,date_of_insert);
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);



要将String转换为sql Date,请使用Date.valueOf(string)。该字符串的格式必须为yyyy-mm-dd

To convert the String to sql Date, you use Date.valueOf (string). The string must be in the format yyyy-mm-dd


这篇关于如何将String转换为java.sql.Date()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆