1:新建一个基础表 message
CREATE TABLE message ( a VARCHAR(32) NOT NULL PRIMARY KEY, message VARCHAR(200))engine=myisam ;
2:工具类 ConnectUtil
package testMerge.util;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
/**
* <B>说 明 </B>:数据库连接类
*
* @author 作 者 名 :wenjie<br/>
* @version 版 本 号 :V1.0.12.0<br/>
* 创建时间: 2017年7月3日 上午11:18:30
* @email duanwenjie@vrvmail.com.cn
*
*/
public class ConnectUtil {
private static Connection connection = null;
// 连接emptyclassroom数据库
private static String DatabaseName = "test";
public static final String name = "com.mysql.jdbc.Driver";
public static final String user = "root";// 默认root
public static final String password = "123456"; // 默认root
public static Connection getCon() {
String url = "jdbc:mysql://192.168.118.130:3306/" + DatabaseName
+ "?useUnicode=true&characterEncoding=utf8&useSSL=true";
try {
Class.forName("com.mysql.jdbc.Driver");
connection = (Connection) DriverManager.getConnection(url, user, password);
// System.out.println(connection.getMetaData().getURL());
return connection;
} catch (Exception e) {
System.out.println("创建连接失败");
e.printStackTrace();
}
return null;
}
public void closeCon() {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("关闭连接失败");
e.printStackTrace();
}
}
public static void main(String[] args) {
System.out.println(ConnectUtil.getCon());
}
public static void executeUpdate(String sql)
{
try{
connection = getCon();
// sql = "select * from total"; //SQL语句
PreparedStatement ps = connection.prepareStatement(sql); //准备执行SQL语句
ps.executeUpdate(); //执行语句,返回结果集
}catch(Exception e){
e.printStackTrace();
}
}
public static int executeQuery(String sql)
{
int count = 0;
try{
connection = getCon();
PreparedStatement ps = connection.prepareStatement(sql); //准备执行SQL语句
ResultSet rs = ps.executeQuery(); //执行语句,返回结果集
while(rs.next())
{
String message=rs.getString("message");
System.out.println(message);
count++;
}
}catch(Exception e){
e.printStackTrace();
}
return count;
}
}
3:实现类 : AutoCreateTableTask
package testMerge.task;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import org.apache.log4j.Logger;
import testMerge.util.ConnectUtil;
/**
*
* <B>说 明</B>:自动创建表Task
* <B>描 述</B>:
* @author: duanwenjie
* @email : duanwenjie@vrvmail.com.cn
* @version:V1.0
* 创建时间 2017年5月16日
*/
public class AutoCreateTableTask extends TimerTask {
public static Timer autoCreateTable_timer; //清理数据
private static Logger logger = Logger.getLogger(AutoCreateTableTask.class);
private static DateFormat formatDate = new SimpleDateFormat("YYYYMMDDhhmmss");
// private static Connection conn = ConnectUtil.getCon();
public synchronized void execute() {
String dateStr = formatDate.format(new Date());
//创建表
String sqlCreate = "CREATE TABLE message_" + dateStr + " LIKE message";
ConnectUtil.executeUpdate(sqlCreate);
System.out.println("=========tableName=======[message_" + dateStr + "]");
//插入数据
String sqlInsert = "insert into message_"+dateStr+" values ("+dateStr+",'message_"+dateStr+"')";
ConnectUtil.executeUpdate(sqlInsert);
}
@Override
public void run() {
execute();
}
public static void main(String[] args) {
//间隔时间 1秒 一天
int n = 10;
long intervalTime = n*1000;
Calendar calendar = Calendar.getInstance();
/*** 定制每日2:00执行方法 ***/
// calendar.set(Calendar.HOUR_OF_DAY, 2);
Date dateSql=calendar.getTime(); //第一次执行定时任务的时间
//如果第一次执行定时任务的时间 小于 当前的时间,此时要在 第一次执行定时任务的时间 加一天,以便此任务在下个时间点执行。如果不加一天,任务会立即执行。
// if (dateSql.before(new Date())) {
// dateSql = this.addDay(dateSql, 1);
// }
//执行任务
//scheduleAtFixedRate 可以用来处理并发
autoCreateTable_timer = new Timer();
autoCreateTable_timer.schedule(new AutoCreateTableTask(), dateSql, intervalTime);
}
}
总结: 该方法比较局限性,必须要针对日期时间来做,查询和插入时间都需要以时间为维度。
分页查询使用union all 来处理
mysql按日期时间分表
最新推荐文章于 2024-08-13 08:51:22 发布