Using setDate in PreparedStatement

后端 未结 6 1649
我在风中等你
我在风中等你 2020-11-22 12:35

In order to make our code more standard, we were asked to change all the places where we hardcoded our SQL variables to prepared statements and bind the variables instead. <

6条回答
  •  忘掉有多难
    2020-11-22 12:49

    The docs explicitly says that java.sql.Date will throw:

    • IllegalArgumentException - if the date given is not in the JDBC date escape format (yyyy-[m]m-[d]d)

    Also you shouldn't need to convert a date to a String then to a sql.date, this seems superfluous (and bug-prone!). Instead you could:

    java.sql.Date sqlDate := new java.sql.Date(now.getTime());
    prs.setDate(2, sqlDate);
    prs.setDate(3, sqlDate);
    

提交回复
热议问题