Using setDate in PreparedStatement

后端 未结 6 1655
我在风中等你
我在风中等你 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:42

    Not sure, but what I think you're looking for is to create a java.util.Date from a String, then convert that java.util.Date to a java.sql.Date.

    try this:

    private static java.sql.Date getCurrentDate(String date) {
    
        java.util.Date today;
        java.sql.Date rv = null;
        try {
    
            SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
            today = format.parse(date);
            rv = new java.sql.Date(today.getTime());
            System.out.println(rv.getTime());
    
        } catch (Exception e) {
            System.out.println("Exception: " + e.getMessage());
        } finally {
            return rv;
        }
    
    }    
    

    Will return a java.sql.Date object for setDate();

    The function above will print out a long value:

    1375934400000

提交回复
热议问题