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).