java 定时器删除数据库记录

来源:百度知道 编辑:UC知道 时间:2024/06/28 02:55:27
import java.io.IOException;
import java.util.*;

public class TimerTest
{
public static void main(String[] args)
{
Timer timer = new Timer();

//每隔三秒执行一次
timer.schedule(new MyTask(), 0, 3000);

while (true)
{
/*
*用来停止任务,
*否则就一直循环执行.
*/
try
{
int ch = System.in.read();
if (ch - 'c' == 0)
{
//使用cancel方法退出循环
timer.cancel();
}
} catch (IOException e)
{
e.printStackTrace();
}
}
}

/**
*自定义类继承TimerTask
*/
private static class MyTask extends TimerTask
{
public void run()
{

//删除数据库所有记录,如何写呢?
}
}
}

你 看看java.sql.*;吧

private static class MyTask extends TimerTask
{
private String url="依照你数据库类别不同而不同";
private String sql="sql语句";
private String driver="数据库jdbc驱动类";
private Connection conn=null;
private PreparedStatement sta=null;

Class.forName(driver);

public void run()
{
conn=getConnection(url);
sta=conn.prepareStatement(sql);
sta.execute(); //执行
sta.close();
conn.close();
}
}

只能单条写吧!虽然麻烦点。