how can i set a special date in PreparedStatement parameters?

痴心易碎 提交于 2019-12-25 18:18:50

问题


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 +++++

thanks


回答1:


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

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



回答2:


When you use function to_date in your sql query, you should use setString(int,String) in your java code. When you want to use use setDate(int,Date) in java code, your query should look as follows:

select sum(cost) from mem_Outcome where trunc(date_out) = ?;

and set a date you request report for.




回答3:


TO_DATE() expects a String as well as the Date format. So you need to use rs.setString() as well as pass the Date format like you passed when you ran your query directly.

Using a String date:

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");
ResultSet rs = pst.executeQuery();

If you want to use rs.setDate(), you no longer require the TO_DATE() in your SQL query.

Using a java.sql.Date object:

PreparedStatement pst =
    conn.prepareStatement("select sum(cost) from mem_income where trunc(date_in) = ?");

pst.setDate(1, new java.sql.Date(
               SimpleDateFormat("dd-MMM-yyyy").parse("31-jul-2013").getTime()));
ResultSet rs = pst.executeQuery();

If you have an instance of java.util.Date already, just use

pst.setDate(1, new java.sql.Date(utilDate.getTime());



回答4:


This also will work...

String Allocated_On = "03/20/2013";
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
java.util.Date dtAllocated_On= new Date();
try {
   dtAllocated_On = (Date)formatter.parse(Allocated_On);
} catch (ParseException pe) {
   log.info("error parsing date needed: "+pe);
}
....
PreparedStatement pst =
  conn.prepareStatement("select sum(cost) from mem_income where date_in = ?");

pst.setDate(1,new java.sql.Date(dtAllocated_On.getTime()));


来源:https://stackoverflow.com/questions/18055209/how-can-i-set-a-special-date-in-preparedstatement-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!