Take last table value from bank and show in Jtextfield

Asked

Viewed 331 times

0

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

I wonder how do I always take the last value and show in a JTextField, and every minute he tries to make that query.

In case, this code suits me perfectly, but my doubt now how do I get the last database value that in the 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);
}
  • Yes, it’s possible. What you’ve done?

  • @Articuno, So far I have only made the change of these values, so that the person can set the values of an equipment from inside the office, but in order to stay loyal, I needed to bring this value real pq the equipment even setando, it never reaches the ideal temperature, because he always has the variation.

  • In the case of java-swing, there is the class swing.Timer which will probably be the one you need to use. At this link teaches how it works and how to use, if you present something you’ve done in the format of a [mcve], I can demonstrate its use in practice.

  • At the moment I don’t have much to present to you, it’s more of a doubt and how can I start.

  • Yes, the problem is that the doubt becomes broad in the way the question is, because beyond the way I explained it, there are countless others that cannot be explained here. If you present something to exemplify what you intended in a [mcve], it is easier to suggest something more specific.

  • Blz, I’ll see what I can do with Timer, and if I have any doubts I’ll come to you.

  • This is not the timer class you should use. It is the one I left the link, because it is a graphical interface.

  • But in that case if I put a task for him to go in the bank and get the last information would work too, wouldn’t? or could give future problems?

Show 3 more comments

1 answer

1


If using the class java.util.Timer, make sure that the component is updated within the EDT, because this Thread is the one who should be solely responsible for managing swing API components:

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 needs to be 60*1000 because the argument that the method scheduleFixedRate() awaits both the delay (delay for first execution) how much the interval(time between repetitions) in milliseconds.

As for the search in the table, from what I understand you want to seek the last result inserted in the table, you probably need to adapt to query down within the TimerTask, returning the field only of the last row of the table:

SELECT TOP 1 * FROM <tabela> ORDER BY <campo a ser ordenado> DESC
  • I’m trying to show an optionpane, but it’s not working. da a peek ai q vou postar la cima

  • @Lucas16 without a [mcve] this is the most I can help, because there is no way I simulate something that was not presented. the same answer was made based on what I supposedly understood of the problem. And the issue entered a completely different doubt from the original so I reversed the question.

  • Got it. I’m grateful, it worked out in a matter of time, but my problem is now time to get the information, but you’ve helped me a lot. thanks.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.