2013年9月6日星期五

Request a fixed time insert a random number to the database instance

Request a fixed time insert a random number to the database instance
------ Solution ----------------------------- ---------------
System.out.println ("daoci");
final Timer t1 = new Timer ();
if (value.equals ("true")) {
System.out.println ("true8");
t1.schedule (new MyTask (), 1000,5000);}
/ * if (value.equals ("false")) {/ / This is used to stop the task , otherwise it has been circulating to perform this task
System.out.println (" enter " ) ; ;
try {

Thread.sleep (2000);
t1.cancel ();

System.out.println ("xin");
} catch (InterruptedException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
} * /
/ / Use this method to exit the task ;
try {

Thread.sleep (20000);
t1.cancel ();
} catch (InterruptedException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
}
System.out.println ("jieshu");

request.getRequestDispatcher ("data1.jsp"). forward (request, response); ;
/ / response.sendRedirect ("/ data.jsp") ;
}

class MyTask extends java.util.TimerTask {
@ Override
public void run () {
int random1 = (int) (Math.random () * 100);
String sql = "insert into rs_00246 (col1) values ​​('" + random1 + "' ) " ;
DBUtil db = new DBUtil ();
db.openDb ();
System.out.println (sql);
db.update (sql);
db.closeDb ();
System.gc ();

}
------ For reference only ----------------- ----------------------
job

没有评论:

发表评论