码迷,mamicode.com
首页 > 数据库 > 详细

每5秒向数据库中插入一条记录-学习笔记

时间:2018-04-20 10:58:09      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:每5秒向数据库中插入一条记录-学习笔记

import java.sql.SQLException; import java.util.Timer; import java.util.TimerTask; import java.util.UUID; import cn.itcast.web.dao.SystemDao; //课程练习1 public class Demo3 { public static void main(String[] args) { Timer timer = new Timer(); timer.schedule(new YouTimerTask(),0,5*1000); } } //线程任务 class YouTimerTask extends TimerTask{ public void run() { try { SystemDao systemDao = new SystemDao(); systemDao.init("systemInit",UUID.randomUUID().toString()); } catch (SQLException e) { e.printStackTrace(); } } } /* drop table if exists systemInit; create table if not exists systemInit( id varchar(40) primary key, curr_time timestamp not null ); */ import java.sql.SQLException; import org.apache.commons.dbutils.QueryRunner; import cn.itcast.web.util.JdbcUtil; public class SystemDao { //删除表 public void dropTable(String tableName) throws SQLException{ QueryRunner runner = new QueryRunner(JdbcUtil.getDataSource()); String sql = "drop table if exists " + tableName; runner.update(sql); } //创建表 public void createTable(String tableName) throws SQLException{ QueryRunner runner = new QueryRunner(JdbcUtil.getDataSource()); String sql = "create table if not exists "+tableName+"(id varchar(40) primary key,curr_time timestamp not null)"; runner.update(sql); } //初始化数据 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); } }

每5秒向数据库中插入一条记录-学习笔记

标签:每5秒向数据库中插入一条记录-学习笔记

原文地址:http://blog.51cto.com/357712148/2105644

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!