您的位置:首页 > 产品设计 > UI/UE

使mysql executeBatch生效 设置rewriteBatchedStatements=true,用mysql-connector-java-5.1.34-bin.jar

2014-12-16 11:07 483 查看
<span style="color:#ff0000;">关键一点:mysql驱动包低版本的不支持批处理,尽量用高版本,如:mysql-connector-java-5.1.34-bin.jar是支持的。</span>
drvier-url=jdbc:mysql://218.241.2.115:3306/appunion?rewriteBatchedStatements=true
driver-class=com.mysql.jdbc.Driver
user=wifiunion
password=wifree1234


public static void parseLogFile(Connection conn, File file) {
String optSql = "insert into wifis_operate_log(createTime,routeMac,province,city,userId,userMac,optType,imei_mac)"
+ " values(?,?,?,?,?,?,?,?)";
int optStep = 1;
String connSql = "insert into wifis_connect_log(createTime,routeMac,province,city,userId,userMac,isSuccess,imei_mac)"
+ " values(?,?,?,?,?,?,?,?)";
String chinaNetSql = "insert into chinanetuser_use_log(logtype,createTime,username,usermac,version,area,province,city) "
+ " values(?,?,?,?,?,?,?,?)";

try
{
conn.setAutoCommit(false); // 设置手动提交
if (file.isFile() && file.exists())
{
PreparedStatement ps = conn.prepareStatement(optSql);
PreparedStatement ps2 = conn.prepareStatement(connSql);
PreparedStatement ps3 = conn.prepareStatement(chinaNetSql);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("入库开始时间="+format.format(new Date()));
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8"));
String lineText = null;
//1.记录wifi操作日志与连接日志。
while ((lineText = bufferedReader.readLine()) != null)
{
if(lineText.contains("wifis_operate_log:"))
{
String s = lineText.substring(lineText.indexOf("wifis_operate_log:")+"wifis_operate_log:".length());
String[] logValues = s.split("\\|",-1);
Date date = format.parse(logValues[0]);
ps.setTimestamp(1, new java.sql.Timestamp(date.getTime()));
ps.setString(2, logValues[1]);
ps.setString(3, logValues[2]);
ps.setString(4, logValues[3]);
ps.setInt(5, Integer.parseInt(logValues[4]));
ps.setString(6, logValues[5]);
ps.setInt(7, Integer.parseInt(logValues[6]));
ps.setString(8, logValues[7]);
ps.addBatch();
}
else if(lineText.contains("wifis_connect_log:"))
{
String s = lineText.substring(lineText.indexOf("wifis_connect_log:")+"wifis_connect_log:".length());
String[] logValues = s.split("\\|",-1);
Date date = format.parse(logValues[0]);
ps2.setTimestamp(1, new java.sql.Timestamp(date.getTime()));
ps2.setString(2, logValues[1]);
ps2.setString(3, logValues[2]);
ps2.setString(4, logValues[3]);
ps2.setInt(5, Integer.parseInt(logValues[4]));
ps2.setString(6, logValues[5]);
ps2.setInt(7, Integer.parseInt(logValues[6]));
ps2.setString(8, logValues[7]);
ps2.addBatch();
}else if(lineText.contains("ChinaNetLog:"))
{
String s = lineText.substring(lineText.indexOf("ChinaNetLog:")+"ChinaNetLog:".length());
String[] logValues = s.split("\\|",-1);

ps3.setString(1, logValues[0]);
Date date = format.parse(logValues[1]);
ps3.setTimestamp(2, new java.sql.Timestamp(date.getTime()));
ps3.setString(2, logValues[1]);
ps3.setString(3, logValues[2]);
ps3.setString(4, logValues[3]);
ps3.setString(5, logValues[4]);
ps3.setString(6, logValues[5]);
if(logValues.length>6)
{
ps3.setString(7, logValues[6]);
ps3.setString(8, logValues[7]);
}else{
ps3.setString(7,null);
ps3.setString(8, null);
}
ps3.addBatch();
}
optStep++;

bf6d
if(optStep%1000==0)
{
System.out.println("已运行到第"+optStep+"条,commit前,"+format.format(new Date()));
ps.executeBatch();
// ps.clearBatch();
ps2.executeBatch();
// ps2.clearBatch();
ps3.executeBatch();
// ps3.clearBatch();
conn.commit();
System.out.println("已运行到第"+optStep+"条,commit后,"+format.format(new Date()));
}

}
bufferedReader.close();
ps.executeBatch();
ps2.executeBatch();
ps3.executeBatch();
conn.commit();
ps.close();
ps2.close();
ps3.close();
System.out.println("入库结束时间="+format.format(new Date()));
//2.预统计(新增共享热点数量、取消共享热点数量、净增共享热点数量、累积共享热点总数、当日活跃热点数量、连接热点次数、连接热点失败次数、连接活跃用户数量、共享活跃用户数量)。
/*conn.setAutoCommit(true);
SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_YEAR, -1);
String preDay = format2.format(calendar.getTime());
System.out.println(preDay);
CallableStatement callStmt = conn.prepareCall("{call preStatistic(?)}");
callStmt.setString(1,preDay);
callStmt.executeUpdate();
callStmt.close();*/
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}

dbURL=jdbc:mysql://218.241.2.115:3306/appunion?rewriteBatchedStatements=true
F:\JKJ\appunion.log
2014-12-15
入库开始时间=2014-12-16 11:03:55
已运行到第1000条,commit前,2014-12-16 11:03:55
已运行到第1000条,commit后,2014-12-16 11:03:55
已运行到第2000条,commit前,2014-12-16 11:03:55
已运行到第2000条,commit后,2014-12-16 11:03:56
已运行到第3000条,commit前,2014-12-16 11:03:56
已运行到第3000条,commit后,2014-12-16 11:03:56
已运行到第4000条,commit前,2014-12-16 11:03:56
已运行到第4000条,commit后,2014-12-16 11:03:56
已运行到第5000条,commit前,2014-12-16 11:03:57
已运行到第5000条,commit后,2014-12-16 11:03:57
已运行到第6000条,commit前,2014-12-16 11:03:57
已运行到第6000条,commit后,2014-12-16 11:03:57
已运行到第7000条,commit前,2014-12-16 11:03:57
已运行到第7000条,commit后,2014-12-16 11:03:57
已运行到第8000条,commit前,2014-12-16 11:03:58
已运行到第8000条,commit后,2014-12-16 11:03:58
已运行到第9000条,commit前,2014-12-16 11:03:58
已运行到第9000条,commit后,2014-12-16 11:03:58
入库结束时间=2014-12-16 11:03:59
over
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐