多线程方式增加数据
import java.io.IOException;
import java.sql.SQLException;
import java.text.NumberFormat;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
public class TimeDimesion
{
public static void main(String[] args) throws IOException, SQLException
{
int threadCount = 10;
int totalSeconds = 3600 * 24;
int pieceSeconds = totalSeconds / threadCount;
int startTime = 0;
int endTime = 0;
for (int i = 0; i < threadCount; i++)
{
if (i < 9)
{
startTime = pieceSeconds * i;
endTime = pieceSeconds * (i + 1) - 1;
Thread timeDimesionThread = new Thread(new TimeDimesionThread(
startTime, endTime));
timeDimesionThread.start();
} else
{
startTime = pieceSeconds * i;
endTime = totalSeconds - 1;
Thread timeDimesionThread = new Thread(new TimeDimesionThread(
startTime, endTime));
timeDimesionThread.start();
}
}
}
static class TimeDimesionThread implements Runnable
{
// 起始时间点
// 用于分使用。
private int startTime = 0;
private int endTime = 0;
private int hour = 0;
private int minute = 0;
private int secod = 0;
private Connection conn = null;
private Statement stmt = null;
public TimeDimesionThread(int startTime, int endTime)
throws IOException, SQLException
{
this.conn = DBConnection.getConnectionInstance();
this.stmt = (Statement) conn.createStatement();
this.startTime = startTime;
this.endTime = endTime;
}
@Override
public void run()
{
int i = startTime;
hour = (int) (startTime / 3600);
int left_seconds = startTime % 3600;
minute = (int) (left_seconds / 60);
secod = left_seconds % 60;
NumberFormat numberFormat = NumberFormat.getNumberInstance();
numberFormat.setMaximumIntegerDigits(2);
numberFormat.setMinimumIntegerDigits(2);
String sql = "";
do
{
secod++;
if (secod > 0 && secod % 60 == 0)
{
minute++;
secod = 0;
}
if (minute > 0 && minute % 60 == 0)
{
hour++;
minute = 0;
}
sql = "REPLACE INTO `dim_time` (`time_key`, `time_value`, `time_hour`, `time_minute`, `time_second`) VALUES('" +
numberFormat.format(hour) +
numberFormat.format(minute) +
numberFormat.format(secod) +
"','" +
numberFormat.format(hour) +
":" +
numberFormat.format(minute) +
":" +
numberFormat.format(secod) +
"'," +
numberFormat.format(hour) +
"," +
numberFormat.format(minute) +
"," +
numberFormat.format(secod) + ");\n";
try
{
stmt.executeUpdate(sql);
} catch (SQLException e)
{
e.printStackTrace();
}
System.out.println(numberFormat.format(hour) + ":" +
numberFormat.format(minute) + ":" +
numberFormat.format(secod));
i++;
} while (i <= endTime);
}
@Override
protected void finalize() throws Throwable
{
this.stmt.close();
this.conn.close();
super.finalize();
}
}
}
表结构
CREATE TABLE `dim_time` (
`time_key` mediumint(8) unsigned NOT NULL COMMENT '时间ID',
`time_value` time NOT NULL COMMENT '时间值',
`time_hour` tinyint(3) unsigned NOT NULL COMMENT '小时',
`time_minute` tinyint(3) unsigned NOT NULL COMMENT '分钟',
`time_second` tinyint(3) unsigned NOT NULL COMMENT '秒',
PRIMARY KEY (`time_key`)
) DEFAULT CHARSET=utf8 COMMENT='时间表'