class runner extends TimerTask {
public void run() {
getQuantityStatus();
//checkProdExpiry();
//checkRawExpiry();
//getCountNotify();
}
}
public dashboard() {
Timer t1 = new Timer();
t1.schedule(new runner(), 5,10000);
}
public void getQuantityStatus() {
int countAll = 0;
int countRead = 0;
query1 = "select * from stockProduct";
query5 = " select count(*) from notify_prod where prodID = '"+ID+"' AND location = '"+location+"' AND type is NULL";
query6 = " select count(*) from notify_prod where prodID = '"+ID+"' AND location = '"+location+"' AND status = 'read' AND type is NULL";
try {
stmt1 = conn.createStatement();
rs1 = stmt1.executeQuery(query1);
while(rs1.next()) {
ID = rs1.getString("prodID");
location = rs1.getString("location");
qty = rs1.getString("avl_qty");
stmt5 = conn.createStatement();
rs5 = stmt5 .executeQuery(query5);
while(rs5.next()) {
countAll = rs5.getInt("count(*)");
}
stmt6 = conn.createStatement();
rs6 = stmt6.executeQuery(query6);
while(rs6.next()) {
countRead = rs6.getInt("count(*)");
}
if(countAll == countRead) {
stmt3 = conn.createStatement();
if(stmt3.executeUpdate(query3) == 1) {
//JOptionPane.showMessageDialog(null, "Stock for Product ID " + ID + " at Location - " + location.toUpperCase() + " is currently getting low.", "Warning", JOptionPane.WARNING_MESSAGE);
JOptionPane.showMessageDialog(null, countAll , "Warning", JOptionPane.WARNING_MESSAGE);
JOptionPane.showMessageDialog(null, countRead , "Warning", JOptionPane.WARNING_MESSAGE);
}
}
//if(Integer.parseInt(qty) <= 20) {
//}
}
}
catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "Something went wrong", "Error", JOptionPane.ERROR_MESSAGE);
ex.printStackTrace();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
在我的数据库中,我有2个表,分别称为“ notifyProd”表和“ stockProd”。因此,我要做的是从stockProd表中检查所有内容,并在获取“ prodID”和“ location”的同时,在“ notifyProd”内部检查表中是否存在“ ID”和“位置”。
getQuantityStatus函数在计时器内部运行,该计时器计划每5秒执行一次。目前,根据给定的ID和位置,notifyProd表中还存在2条记录。所以我的问题是,当我执行代码时,在第一次尝试中,它为变量“ countAll”和“ countRead”提供了0和0,然后在5秒钟后为变量'2和0提供了正确的值。 countAll”和“ countRead”。我不知道为什么。 “ Query5”获取特定ID和位置的所有记录中的No,“ Query6”获取对给定ID和位置的所有读取记录。构造函数是dashboard(),并在其中调用了计时器。