Import Java.sql.sqlexception;import java.util.timer;import Java.util.timertask;import Java.util.UUID;import cn.itcast.web.dao.systemdao;//Course Exercise 1public class Demo3 {public static void main (string[] args) {Timer timer = new Timer () ; Timer.schedule (New Youtimertask (), 0,5*1000); }}//thread Task Class Youtimertask extends timertask{public void run () {try {Systemdao Systemdao = new Systemdao (); systemdao.i NIT ("Systeminit", Uuid.randomuuid (). toString ()); } catch (SQLException e) {e.printstacktrace ();}}} /*drop table if exists systeminit;create table if not exists systeminit (ID varchar (+) primary key, Curr_time timestamp n OT null); */import Java.sql.sqlexception;import Org.apache.commons.dbutils.queryrunner;import Cn.itcast.web.util.jdbcutil;public class Systemdao {//delete table public void droptable (String tableName) throws sqlexception{Q Ueryrunner runner = new Queryrunner (Jdbcutil.getdatasource ()); String sql = "DROP table if exists" + tableName; Runner.update (SQL); }//CREATE table public void CreateTable (String tableName) throws sqlexception{Queryrunner runner = new Queryrunner (Jdbcutil.getdatasource ()); String sql = "CREATE table if not exists" +tablename+ "(ID varchar (+) Primary key,curr_time timestamp not null)"; Runner.update (SQL); }//Initialize data public void init (String tablename,string id) throws sqlexception{queryrunner runner = new Queryrunner (jdbcutil. Getdatasource ()); String sql = "INSERT INTO" +tablename+ "(ID) VALUES (?)"; Runner.update (Sql,id); }}
Insert a record into the database every 5 seconds-Learn notes