我有以下代码。我的问题是该程序只运行一次,即使我希望它每5秒运行一次。你能指导我搞错吗?
基本上,程序必须在数据库中检入最后两个条目,并在时间戳之间进行区分。
最后,如果通过的时间是必需的,则必须向用户发送消息。这应该连续运行,但我只能让它运行一次。
这是我的代码:
java.util.Timer timer = new java.util.Timer();
public TimerChangeButtonBackground() {
timer.schedule(new TimerTask(){
public void run()
{
try {
String DB_Connection = "jdbc:sqlserver://127.0.0.1:1433;databaseName=Dizertatie;instance=SQLServer;integratedSecurity=true;";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
final Connection conn = DriverManager.getConnection(DB_Connection);
Statement state = conn.createStatement();
ResultSet rs = state.executeQuery("set dateformat dmy; \n" +
"\n" +
"SELECT TOP 2 DIFF FROM DIFF_CT ORDER BY Full_Time_Stamp DESC ");
JOptionPanex.getRootFrame().dispose();
if(rs.next()){
rs.next();
String value = rs.getString(1);
int difference = Integer.valueOf(value);
if (difference <= 5){
ImageIcon icon = new ImageIcon("C:\\Users\\Hory\\Desktop\\likegreen.png");
JOptionPanex.showConfirmDialog(
null,
"Part in target",
"In Target", JOptionPanex.OK_CANCEL_OPTION,JOptionPanex.CANCEL_OPTION, icon);
System.out.printf("target");
}
else{
ImageIcon icon2 = new ImageIcon("C:\\Users\\Hory\\Desktop\\likered.png");
JOptionPanex.showConfirmDialog(
null,
"Part NOT in target",
"Not in target",JOptionPanex.OK_CANCEL_OPTION,JOptionPanex.CANCEL_OPTION,icon2);
System.out.printf("NOtarget");
}
}}catch(Exception e){
System.out.println(e);
}}},3,5000);
`