0

The problem is that I am using a timer (java.util.timer) to get information every 2s (could be up to 5s) from a ms-access db (mdb file), if my query seems to last more than 1s to get a response then it just skips while and goes to the next line.

public void readDB() throws ClassNotFoundException, SQLException {
     String dbURL = "jdbc:ucanaccess://"+path;
     Connection conn = DriverManager.getConnection(dbURL);
     Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
     Statement s = conn.createStatement();
     ResultSet rs = s.executeQuery("SELECT top 10 AVG(columnName) AS Pro, ID from table WHERE ID = 'ID' ORDER BY ID DESC");
     while (rs.next()){
        System.out.println("AVG Result: " + rs.getDouble(1));
     }
     if (conn != null) { conn.close(); }
     if (s != null) { s.close(); }
     if (rs != null) { rs.close(); }
}

I've done the same thing but without the "where and order by" and it works just perfectly, also tried to give more time to the timer but it's not working.

public void startTimer(){
        TimerTask task = new TimerTask(){
            public void run(){
                    try {
                        readDB();
                    } catch (ClassNotFoundException | SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        };
        timer = new Timer();
        timer.scheduleAtFixedRate(task, 0, 2000);
    }

With that said I just need a way to make the timer not to jump that line and wait for a response from the db or simply find a way to get the data every x amount of time.

Edit: added information about the timer and added code.

Pd: I am using the paint method as well to draw the result but, I don't think that matters in this case because of what I said before.

Pd2: I am using ucanaccess to connect to the db, I'm not sure if that is a problem or if I can use anything different (free to use if possible).

Ezequiel
  • 69
  • 1
  • 8
  • So you want to defeat the timer? Could you simply remove it, or turn it off? – Robert Harvey Oct 04 '21 at 17:18
  • Check the answer to this: [SO: Waiting for a timer to finish-java](https://stackoverflow.com/questions/1321620/waiting-for-a-timer-to-finish-in-java). – Alias Cartellano Oct 04 '21 at 17:57
  • @RobertHarvey the thing is that I need this timer to keep receiving information, if I turn it off then then it kills the purpose to have a timer but, I know that making a timer wait for an action is not what you are supposed to do – Ezequiel Oct 04 '21 at 20:07
  • Sounds like you don't have enough information in your question. I don't see any code that describes what you just said. – Robert Harvey Oct 04 '21 at 20:08
  • @RobertHarvey just updated the post, please give it a look. – Ezequiel Oct 04 '21 at 20:53
  • @AliasCartellano I don't need to shut it down and let the code finish, I need it to keep giving me information, but will try to do some sort of do it once and kill it then call it again (trying to create another thread before it dies controlled by a variable) until I specify to stop to see if it works . – Ezequiel Oct 04 '21 at 20:56
  • This is really odd code. The timer object should be declared *outside* of the method that is starting it, and should execute some other method that "captures the information" you need (whatever that is). If this is a simple timeout mechanism, you should rely on the SQL connection to do that for you, not a timer. – Robert Harvey Oct 04 '21 at 20:57
  • @Ezequiel what progress have you made on this? – Alias Cartellano Oct 09 '21 at 17:24
  • @AliasCartellano well I just did it with a ScheduledExecutorService and made some changes to my code. – Ezequiel Oct 12 '21 at 15:58

0 Answers0