0

[this is the output 00 minutes](https://i.sstatic.net/9Qte4SBK.png)

I used events that trigger if 30 minutes passed, automatically setting the status to no action if the employee did not approve.

public void populateTableWithFormattedCountdown(JTable table, int userId) {
    DefaultTableModel tableModel = (DefaultTableModel) table.getModel();

    Connection connection = null;
    PreparedStatement statement = null;
    ResultSet resultSet = null;

    try {
        connection = db.getConnection();

        String query = "SELECT O.ORDER_ID, C.BURGER_INGREDIENTS, O.TOTAL, O.ORDER_STATUS, " +
                       "GREATEST(0, TIMESTAMPDIFF(SECOND, NOW(), DATE_ADD(O.currentDate, INTERVAL 30 MINUTE))) AS remaining_time " +
                       "FROM ORDERS O INNER JOIN CART C ON O.CART_ID = C.CART_ID " +
                       "WHERE O.USER_ID = ? AND O.ORDER_STATUS = 'Waiting For Approval'";

        statement = connection.prepareStatement(query);
        statement.setInt(1, userId);
        resultSet = statement.executeQuery();

        // Clear existing rows
        tableModel.setRowCount(0);

        while (resultSet.next()) {
            String orderId = resultSet.getString("ORDER_ID");
            String burgerIngredients = resultSet.getString("BURGER_INGREDIENTS");
            String total = resultSet.getString("TOTAL");
            String orderStatus = resultSet.getString("ORDER_STATUS");
            int remainingTimeInSeconds = resultSet.getInt("remaining_time");

            // Always positive countdown (if 0, stays 0)
            int minutes = remainingTimeInSeconds / 60;
            int seconds = remainingTimeInSeconds % 60;

            String formattedTime = String.format("%02d:%02d Minutes Left", minutes, seconds);

            tableModel.addRow(new Object[]{orderId, burgerIngredients, total, orderStatus, formattedTime});
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        try {
            if (resultSet != null) resultSet.close();
            if (statement != null) statement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}







public void startCountdownTimer(JTable table, int userId) {
    javax.swing.Timer timer = new javax.swing.Timer(1000, e -> populateTableWithFormattedCountdown(table, userId));
    timer.start();
}

I would like to see the output which will display how many minutes had passed.

New contributor
Aj Catli is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
5
  • 3
    I see your code and some things you say you want, but it really helps if you actually ask a question. What is your question that you would like help answering?
    – ysth
    Commented 17 hours ago
  • 2
    Like mentioned above your question is lacking details, where is this being used, what is the context, how this method is being called. Besides all that I believe you are overengineering this. If your order has a start date all you need to do is to create a query that compute in minutes the time passed since start date, you don't need to mix sql and java code for that. Second, since you are using Swing, just add a timer to update whatever view from time to time. Commented 17 hours ago
  • 1
    here is an example for the query: sqlfiddle.com/mysql/… Commented 17 hours ago
  • @JorgeCampos thank you for the example query. It's working now! It's just that the data type of the current date in my MySQL database is Date instead of DateTime.
    – Aj Catli
    Commented 8 hours ago
  • For DateTime it is easier to use Java "milliseconds" as the datetime from outside the SQL and store as a large integer , some database software versions have all three for column type if wanted, Oder only have time and date data types. Commented 4 hours ago

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.