【问题标题】:Need to modify java Timestamp to get thae Latest records from Database需要修改 java Timestamp 从数据库中获取最新记录
【发布时间】:2014-06-26 00:17:58
【问题描述】:

我编写了一个 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;
    }

}

提前致谢。

【问题讨论】:

    标签: java mysql jdbc timestamp


    【解决方案1】:

    虽然这在技术上不是您问题的答案,但您可以修改您的 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
    

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

    【讨论】:

      【解决方案2】:

      如何让日期少于 1 分钟??

      只需负 60000 毫秒。

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

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

      【讨论】:

        【解决方案3】:

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

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

        现在,date 代表一分钟前的时间

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-01-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-10-24
          • 1970-01-01
          • 2016-01-07
          相关资源
          最近更新 更多