Question

I have written a webservice which will be responsible to display the latest inserted or updated records in Database to the User Interface .

This is my query

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

And this is my table structure

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)

I am checking with the currenttimestamp as shown below

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

How can i make the date less than 1 minute ??

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;
    }

}

Thanks in advance .

Was it helpful?

Solution

While this is technically not an answer to your question, you could alternatively modify your SQL statement to simply return the newest row. In MSSQL syntax it looks something like this:

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

This will always return whichever row was modified (as in created or updated) last, and takes no parameters.

OTHER TIPS

How can i make the date less than 1 minute ??

Just minus 60000 milliseconds.

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

new Date().getTime() returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.

You have to subtract 60,000 milliseconds from the long value representing the current time

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

Now, date represents the time dated a minute back

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top