Skip to main content
Post Closed as "Needs details or clarity" by Jorge Campos, Shadow, user207421

i I want to implement real time countdown in sqlSQL

iI used events that trigger if 30 minutes has pass it will automatic setpassed, automatically setting the status to no action if the employee did not aprrovedapprove.

the order then i wantI would like to see the output which will display how many minutes has passhad passed.

i want to implement real time countdown in sql

i used events that if 30 minutes has pass it will automatic set the status no action if the employee did not aprroved

the order then i want to see the output how many minutes has pass

I want to implement real time countdown in SQL

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

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

added 460 characters in body
Source Link
public void loadCartItemspopulateTableWithFormattedCountdown(JTable table, int userId) {
        try {
            ConnectionDefaultTableModel contableModel = db(DefaultTableModel) table.getConnectiongetModel();
  
    Connection connection = null;
    StringPreparedStatement sqlstatement = "SELECT DATE_FORMAT(c.timeAndDate, '%Y-%m-%d %H:%i') AS formattedDateTime, "null;
      ResultSet resultSet = null;

    try {
        connection += "cdb.CustomBurgerPicture, "getConnection();
          
        String query = "SELECT O.ORDER_ID, C.BURGER_INGREDIENTS, +O.TOTAL, "cO.burger_ingredientsORDER_STATUS, " +
                       + "c.total"GREATEST(0, " 
              TIMESTAMPDIFF(SECOND, NOW(), DATE_ADD(O.currentDate, INTERVAL 30 MINUTE))) AS remaining_time " + "c.cart_id "
                       + "FROM cartORDERS cO INNER JOIN cart_itemsCART ciC ON cO.cart_idCART_ID = ciC.cart_idCART_ID "
  +
                      + "WHERE cO.user_idUSER_ID = ? "
   AND O.ORDER_STATUS = 'Waiting For Approval'";

        statement = connection.prepareStatement(query);
     + "ORDER BY cstatement.timeAndDate DESC";setInt(1, 
 userId);
            PreparedStatement psresultSet = constatement.prepareStatementexecuteQuery(sql);
   
        // Clear ps.setInt(1,existing userId);rows
            ResultSet rs = pstableModel.executeQuerysetRowCount(0);

            while (rsresultSet.next()) {
                String formattedDateTimeorderId = rsresultSet.getString("formattedDateTime""ORDER_ID");
                BlobString imageBlobburgerIngredients = rsresultSet.getBlobgetString("CustomBurgerPicture""BURGER_INGREDIENTS");
           
                String ingredientstotal = rsresultSet.getString("burger_ingredients""TOTAL");
                String totalorderStatus = rsresultSet.getString("total""ORDER_STATUS");
                Stringint cartIdremainingTimeInSeconds = rsresultSet.getStringgetInt("cart_id""remaining_time");
     
            // Process theAlways imagepositive blob
countdown (if 0, stays 0)
            ifint (imageBlobminutes != null)
     remainingTimeInSeconds / 60;
         {
   int seconds = remainingTimeInSeconds % 60;

            byte[]String imageBytesformattedTime = imageBlobString.getBytesformat(1"%02d:%02d Minutes Left", (int)minutes, imageBlob.length()seconds);
        
            tableModel.addRow(new ImageIconObject[]{orderId, imageIconburgerIngredients, =total, neworderStatus, ImageIcon(imageBytesformattedTime});
 
                 }
           } catch (SQLException e) {
     int id = Integere.parseIntprintStackTrace(cartId);
                 } finally {
 // Add cart item with image, item names, and date to thetry UI{
                if (resultSet != null) cartresultSet.addCartItemWithImageAndItemsclose(imageIcon, ingredients, formattedDateTime, total, id);
                if (statement != null) setCart_idstatement.close(cartId);
                }
         catch (SQLException e) }
{
            rse.closeprintStackTrace();
        }
    ps.close();}
  }







public void startCountdownTimer(JTable table, int userId) {
    conjavax.close();
   swing.Timer timer = new javax.swing.Timer(1000, }e catch-> populateTableWithFormattedCountdown(Exceptiontable, euserId) {);
            etimer.printStackTracestart();
        }
    }
public void loadCartItems(int userId) {
        try {
            Connection con = db.getConnection();
            String sql = "SELECT DATE_FORMAT(c.timeAndDate, '%Y-%m-%d %H:%i') AS formattedDateTime, "
                       + "c.CustomBurgerPicture, "
                       + "c.burger_ingredients, " 
                       + "c.total, " 
                       + "c.cart_id "
                       + "FROM cart c INNER JOIN cart_items ci ON c.cart_id = ci.cart_id "
                        + "WHERE c.user_id = ? "
                       + "ORDER BY c.timeAndDate DESC"; 
 
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setInt(1, userId);
            ResultSet rs = ps.executeQuery();

            while (rs.next()) {
                String formattedDateTime = rs.getString("formattedDateTime");
                Blob imageBlob = rs.getBlob("CustomBurgerPicture");
           
                String ingredients = rs.getString("burger_ingredients");
                String total = rs.getString("total");
                String cartId = rs.getString("cart_id");
                // Process the image blob
                if (imageBlob != null)
                {
                    byte[] imageBytes = imageBlob.getBytes(1, (int) imageBlob.length());
                    ImageIcon imageIcon = new ImageIcon(imageBytes);
 
                 
                    int id = Integer.parseInt(cartId);
                    // Add cart item with image, item names, and date to the UI
                    cart.addCartItemWithImageAndItems(imageIcon, ingredients, formattedDateTime, total, id);
                    setCart_id(cartId);
                }
            }

            rs.close();
            ps.close();
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
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();
}
Source Link

i want to implement real time countdown in sql

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

i used events that if 30 minutes has pass it will automatic set the status no action if the employee did not aprroved

public void loadCartItems(int userId) {
        try {
            Connection con = db.getConnection();
            String sql = "SELECT DATE_FORMAT(c.timeAndDate, '%Y-%m-%d %H:%i') AS formattedDateTime, "
                       + "c.CustomBurgerPicture, "
                       + "c.burger_ingredients, " 
                       + "c.total, " 
                       + "c.cart_id "
                       + "FROM cart c INNER JOIN cart_items ci ON c.cart_id = ci.cart_id "
                       + "WHERE c.user_id = ? "
                       + "ORDER BY c.timeAndDate DESC"; 

            PreparedStatement ps = con.prepareStatement(sql);
            ps.setInt(1, userId);
            ResultSet rs = ps.executeQuery();

            while (rs.next()) {
                String formattedDateTime = rs.getString("formattedDateTime");
                Blob imageBlob = rs.getBlob("CustomBurgerPicture");
           
                String ingredients = rs.getString("burger_ingredients");
                String total = rs.getString("total");
                String cartId = rs.getString("cart_id");
                // Process the image blob
                if (imageBlob != null)
                {
                    byte[] imageBytes = imageBlob.getBytes(1, (int) imageBlob.length());
                    ImageIcon imageIcon = new ImageIcon(imageBytes);

                 
                    int id = Integer.parseInt(cartId);
                    // Add cart item with image, item names, and date to the UI
                    cart.addCartItemWithImageAndItems(imageIcon, ingredients, formattedDateTime, total, id);
                    setCart_id(cartId);
                }
            }

            rs.close();
            ps.close();
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

the order then i want to see the output how many minutes has pass