Insert a record into the database every 5 seconds-Learn notes

Source: Internet
Author: User
Tags uuid

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.