先在資料庫寫個存儲過程,生成表名為名稱_當天日期格式的表,
在建立的資料庫下選擇函數右鍵選擇過程,
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中寫入配置監聽
<listener>
<listener-class>com.example.utills.ContextListener</listener-class>
</listener>
運作一下項目,控制台出現如下語句。
資訊: 指定任務執行結束
十一月 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
資訊: 指定任務執行結束
至此結束,大功告成!