使用Java代码中的sql更新db的当前日期和时间

时间:2014-11-01 06:35:27

标签: java sql date time

我有一个表“queue_in_progress”,其结构如下:

enter image description here

我想更新表格的DATE_TIME_TOKEN_TAKEN,CE_PK,Service_status。为此,我有以下代码:

    String sqlQuery = "UPDATE queue_in_progress\n"  +
                 "SET CE_PK="+ce_pk+" ,SERVICE_STATUS=1 \n"  +
                 "WHERE CATEGORY_PK="+Category_PK+" AND TOKEN_NO="+ Token_PK+" "
                    + " AND SERVICE_COUNTER="+service_counter+" AND SERVICE_CENTER_PK="+service_center+" ;";

java.util.Date utilDate = new Date();  // Convert it to java.sql.Date
java.sql.Date date = new java.sql.Date(utilDate.getTime());

PreparedStatement stmt = con.prepareStatement(sqlQuery);
                    stmt.setDate(1, date);
                    success = stmt.executeUpdate();

 But the success flag is returning -1 and the table is not updated . What is the problem ? What can I do to fix this problem ? 

2 个答案:

答案 0 :(得分:1)

我的查询中没有DATE_TIME_TOKEN_TAKEN=?(绑定参数),我想你想要

String sqlQuery = "UPDATE queue_in_progress SET DATE_TIME_TOKEN_TAKEN=?, "
        + "CE_PK=" + ce_pk
        + ", SERVICE_STATUS=1 WHERE CATEGORY_PK="
        + Category_PK
        + " AND TOKEN_NO="
        + Token_PK
        + " AND SERVICE_COUNTER="
        + service_counter + " AND SERVICE_CENTER_PK=" + service_center;

答案 1 :(得分:0)

如果您希望DATE_TIME_TOKEN_TAKEN始终保持当前时间值,则可以在数据库端设置它,无需在代码中设置它。

ALTER TABLE queue_in_progress
MODIFY DATE_TIME_TOKEN_TAKEN DEFAULT CURRENT_TIMESTAMP;