如何在PreparedStatement参数中设置特殊日期? [英] how can i set a special date in PreparedStatement parameters?
问题描述
嘿... 我想在代码中使用日期时遇到问题 我有一个名为ReportService的类,在该类中,我使用jdbc连接数据库,此后我想在特殊的日期从数据库中获取报告.
heloo... I have a problem when I want to use date in my code I have a class that named ReportService and in this class I use jdbc to connect my database and after that I want to get a report from my database in a special date.frist I write this :
("select sum(cost) from mem_income where trunc(date_out) = to_date ('31-jul-2013' , 'dd-mm-yyyy')");
这项工作很好.但是在那之后,我想从我的主要班级传递日期:
and this work good.but after that I want to pass my date from my main class:
PreparedStatement pst =
conn.prepareStatement("select sum(cost) from mem_income where trunc(date_in) = to_date (?)");
pst.setDate(1, +++++ );
ResultSet rs = pst.executeQuery();
我不知道该写些什么,而不是+++++
I don't know what I should write instead of +++++
谢谢
推荐答案
如您的第一个SQL查询所示,to_date()函数需要2个参数,而不仅仅是一个.而且这两个参数都是字符串,而不是日期.
As shown in your first SQL query, to to_date() function takes 2 arguments, and not just one. And both of these arguments are strings, and not dates.
因此您可以将代码更改为
So you could change the code to
PreparedStatement pst =
conn.prepareStatement("select sum(cost) from mem_income where trunc(date_in) = to_date(?, 'dd-mm-yyyy')");
pst.setString(1, '31-jul-2013');
但是更好的选择是直接传递日期,而忽略to_date函数:
But a better option would be to pass a date directly, and forget about the to_date function:
PreparedStatement pst =
conn.prepareStatement("select sum(cost) from mem_income where trunc(date_in) = ?");
pst.setDate(1, java.sql.Date.valueOf('2013-07-31'));
这篇关于如何在PreparedStatement参数中设置特殊日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!