先在数据库写个存储过程,生成表名为名称_当天日期格式的表,
在新建的数据库下选择函数右键选择过程,
BEGIN
DECLARE `@suffix` VARCHAR(15);
DECLARE `@sqlstr` VARCHAR(2560);
SET `@suffix` = DATE_FORMAT(CURDATE(),'%Y_%m_%d');
SET @sqlstr = CONCAT(
"CREATE TABLE device_",`@suffix`,"(
`id` int(10) NOT NULL AUTO_INCREMENT ,
`sn` varchar(20),
`date` datetime,
`metadata` mediumblob,
`status` int(11),
PRIMARY KEY (`id`)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;");
PREPARE stmt FROM @sqlstr;
EXECUTE stmt;
END
保存存储过程名为device_proc,当前页面的类型选项为 PROCEDURE然后运行,
刷新数据库就可以看见新建的表了。若要每天生成这样的一张表格,还需要写个事件。事件编写如下。
CREATE EVENT if not exists e_test
on schedule every 30 second
on completion preserve
do call device_proc;
接下来写个生成1000个随机数的类 每个数有12位,前3位为版本aa或者bb,
public class RandomSnGennerator { public static final String allChar = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; public static String randomString(int length) { StringBuffer sb = new StringBuffer(); Random random = new Random(); for (int i = 0; i < length; i++) { sb.append(allChar.charAt(random.nextInt(allChar.length()))); } return sb.toString(); } public static String headstr() { String[] str = {"aa", "bb"}; int random = (int) (Math.random() * 2); String headstr = str[random]; return headstr; } public static ArrayList getSN(){ ArrayList sn=new ArrayList(); for(int i=0;i<1000;i++){ String headstr = headstr(); String bodystr = randomString(9); String devicestr = headstr + bodystr; sn.add(devicestr); } return sn; } }接下来写个定时器 其中run方法里面每生成一条数据就往数据库表插入一条数据
run方法的重写在main方法外,
public class TimerUtil extends TimerTask { private static boolean isRunning = false; private ServletContext context = null; public TimerUtil() { super(); } public TimerUtil(ServletContext context) { this.context = context; } public static void main(String[] args) throws Exception { Class.forName("com.mysql.jdbc.Driver"); Timer timer = new Timer(); TimerUtil task = new TimerUtil(); timer.schedule(task, 0, 1000);//1s一次 ,间隔0 } @Override public void run() { if (!isRunning) { context.log("开始执行任务"); DateFormat sdf = new SimpleDateFormat("yyyy_MM_dd"); String s = sdf.format(new Date()); DateFormat sdfc = new SimpleDateFormat("yyyy_MM_dd HH:mm:ss"); String[] str = {"0", "1", "2"}; String a = null; String url = "jdbc:mysql://localhost:3306/balala?useUnicode=true&characterEncoding=utf-8"; Connection conn = null; Statement stat = null; try { conn = DriverManager.getConnection(url, "xx", "oo"); stat = conn.createStatement(); ArrayList devicesn = RandomSnGennerator.getSN(); for (int i = 0; i < 1000; i++) { int random = (int) (Math.random() * 3); String status = str[random]; a = (String)sn.get(i); String sql = "insert into devicedata_" + s + " (devicesn,createdate,
status) values('" + a + "','" + sdfc.format(new Date())
+ "','" + status + "')"; stat.executeUpdate(sql); } stat.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } isRunning =false; context.log("指定任务执行结束"); } else { context.log("上次任务未执行结束"); } } }可以向数据表每秒钟插入1000条数据后,还得写个web定时器,在web项目启动时,执行定时器,
package com.example.utills; import java.util.Calendar; import java.util.Date; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class ContextListener implements ServletContextListener{ public ContextListener() { } private java.util.Timer timer = null; /** * 初始化定时器 * web 程序运行时候自动加载 */ @Override public void contextInitialized(ServletContextEvent arg0) { /** * 设置一个定时器 */ timer = new java.util.Timer(true); arg0.getServletContext().log("定时器已启动"); /** * 定时器到指定的时间时,执行某个操作(如某个类,或方法) */ int period = 24 * 60 * 60 * 1000; //每天的date时刻执行task,每隔persion 时间重复执行 timer.schedule(new TimerUtil(arg0.getServletContext()),0, 1000); // 在 指定的date时刻执行task, 仅执行一次 arg0.getServletContext().log("已经添加任务调度表"); } /** * 销毁 */ @Override public void contextDestroyed(ServletContextEvent arg0){ timer.cancel(); arg0.getServletContext().log("定时器销毁"); } }这个类里面定时器每秒执行,与上面 的定时器类配合
还需要在web.xml中写入配置监听
com.example.utills.ContextListener
运行一下项目,控制台出现如下语句。
信息: 指定任务执行结束
十一月 21, 2017 2:59:21 下午 org.apache.catalina.core.ApplicationContext log
信息: 开始执行任务
十一月 21, 2017 2:59:22 下午 org.apache.catalina.core.ApplicationContext log
信息: 指定任务执行结束
十一月 21, 2017 2:59:22 下午 org.apache.catalina.core.ApplicationContext log
信息: 开始执行任务
十一月 21, 2017 2:59:24 下午 org.apache.catalina.core.ApplicationContext log
信息: 指定任务执行结束