需要修改java时间戳以从数据库中获取最新记录

时间:2014-05-08 14:23:23

标签: java mysql jdbc timestamp

我编写了一个Web服务,负责将数据库中最新插入或更新的记录显示给用户界面。

这是我的查询

String selectSQL = "SELECT id , VendorName , Item , updated_at , created_at  from Orders where updated_at > ? OR created_at > ?";

这是我的表结构

mysql> select * from Orders;
+-------+--------------+----------+---------------------+---------------------+
| id    | VendorName   | Item     | updated_at          | created_at          |
+-------+--------------+----------+---------------------+---------------------+
| 12345 | PoppyCounter | Chocltae | 2014-05-08 18:49:42 | 2014-05-08 18:49:42 |
+-------+--------------+----------+---------------------+---------------------+
1 row in set (0.00 sec)

我正在检查当前时间戳,如下所示

java.sql.Timestamp date = new java.sql.Timestamp(new java.util.Date().getTime());

如何让日期不到1分钟?

package com.serviceees;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import com.google.gson.Gson;

import dto.Orders;

@Path("/updates")
public class DBPollerService {
    @GET
    @Produces("application/json")
    public String getUpdates() {
        System.out.println("getUpdates called");
        String clientResponse = "Error";
        ArrayList<Orders> newOrdersList = new ArrayList<Orders>();  
        Gson gson = new Gson();
        try {

            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            System.out.println("connection");
            Connection connection = null;
            PreparedStatement preparedStatement = null;
            String selectSQL = "SELECT id , VendorName , Item , updated_at , created_at  from Orders where updated_at > ? OR created_at > ?";
            try {
                connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "", "");
                while (true) {
                    System.out.println("Into while true");
                    try {

                        java.sql.Timestamp date = new java.sql.Timestamp(new java.util.Date().getTime());
                        preparedStatement = connection.prepareStatement(selectSQL);
                        preparedStatement.setTimestamp(1, date);
                        preparedStatement.setTimestamp(2, date);
                        ResultSet rs = preparedStatement.executeQuery();
                        while (rs.next()) {
                            Orders order = new Orders();
                            order.setId(rs.getInt(1));
                            order.setVendorName(rs.getString(2));
                            order.setItem(rs.getString(3));
                            newOrdersList.add(order);
                        }

                        Thread.sleep(3000);
                        clientResponse =    "jsonCallback(["+gson.toJson(newOrdersList)+"])";
                        return clientResponse;
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            } catch (SQLException e) {
                System.out.println("Connection Failed! Check output console");
                e.printStackTrace();

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        clientResponse =    gson.toJson(newOrdersList); 
        System.out.println("clientResponse"+clientResponse);
        return clientResponse;
    }

}

提前致谢。

3 个答案:

答案 0 :(得分:2)

如何让日期不到1分钟?

只需减去60000毫秒。

Timestamp date = new Timestamp(new Date().getTime()-60 * 1000);

new Date().getTime()返回自此Date对象表示的1970年1月1日00:00:00 GMT以来的毫秒数。

答案 1 :(得分:2)

您必须从表示当前时间的长值中减去60,000毫秒

Timestamp date = new Timestamp(new java.util.Date().getTime() - (60 * 1000));

现在,date表示一分钟后的时间

答案 2 :(得分:2)

虽然这在技术上不是您问题的答案,但可以或者修改您的SQL语句以简单地返回最新的行。在MSSQL语法中,它看起来像这样:

SELECT TOP 1 * 
FROM Orders
INNER JOIN (
    SELECT id, updated_at AS modified from Orders
    UNION
    SELECT id, created_at from Orders 
) AS whatever ON whatever.id = Orders.id
ORDER BY whatever.modified DESC

这将始终返回最后修改的行(如在创建或更新中),并且不带任何参数。