Get the last table value from the database and display it in JTextField

0

I have a program here in the factory that, every 1 minute, an equipment connected to a supervisor sends a direct value into a table in the database (SQL Server).

I would like to know how to always get the last value and display it in a JTextField , and every minute it tries to do this query.

In case, this code caters me perfectly, but my question now is how do I get the last value from the database that in this case would be (SQL Server).

    int delay = 5000;   // delay de 5 seg.
    int interval = 5000;  // intervalo de 1 seg.
    Timer timer = new Timer();

    timer.scheduleAtFixedRate(new TimerTask() {
        public void run() {
          jTextField6.setText("1");
        }
    }, delay, interval);
}
    
asked by anonymous 31.10.2017 / 12:51

1 answer

1

If you are going to use the java.util.Timer class, make sure the component is updated within the < , because this thread should be solely responsible for managing components of the swing API:

int delay = 5000;   // delay de 5 seg.
int interval = 5000;  // intervalo de 1 seg.
Timer timer = new Timer();

timer.scheduleAtFixedRate(new TimerTask() {
    public void run() {
      SwingUtilities.InvokeLater(()-> jTextField6.setText("1"));
    }
}, delay, interval);

Remembering that the time is 1 minute, the interval must be 60 * 1000 because the argument that the scheduleFixedRate() expects both delay (delay for first execution) and interval / em> (time between reps) in milliseconds.

As far as the search in the table, so I understand you want to fetch the last result inserted in the table, you will probably need to adapt the query below within TimerTask , returning the field only from the last row of the table :

SELECT TOP 1 * FROM <tabela> ORDER BY <campo a ser ordenado> DESC
    
31.10.2017 / 13:30