1、导入c3p0-0.9.1.2.jar 、mysql-connector-java-5.1.28-bin.jar 两个数据库连接jar包(需要的话自己上网找或者留言我给发)
2、写c3p0配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<c3p0-config>
<default-config>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/dataanaysismodel</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="user">root</property>
<property name="password">root</property>
<property name="acquireIncrement">3</property>
<property name="initialPoolSize">10</property>
<property name="minPoolSize">2</property>
<property name="maxPoolSize">10</property>
</default-config>
</c3p0-config>
3、写c3p0连接util
import javax.sql.DataSource;
import org.apache.commons.dbutils.QueryRunner;
import com.mchange.v2.c3p0.ComboPooledDataSource;
/**
* 封装常用的操作
* @author zuoan
*/
public class JdbcUtils {
// 初始化连接池
private static DataSource dataSource;
static {
dataSource = new ComboPooledDataSource();
}
public static DataSource getDataSource() {
return dataSource;
}
/**
* 创建DbUtils常用工具类对象
*/
public static QueryRunner getQuerrRunner() {
return new QueryRunner(dataSource);
}
}
4、将.sql文件导入数据库,导入日志可选
import java.io.*;
import java.sql.*;
import java.text.SimpleDateFormat;
import org.apache.commons.dbutils.QueryRunner;
/**
* 1.读取指定文件下sql脚本,执行到数据库
* 按行读取分批处理批量插入数据库
*
*/
public class TestReadFile {
public static void main(String[] args) throws SQLException {
QueryRunner qr = JdbcUtils.getQuerrRunner();
String path = "G:\\DataAnaysisModel\\singapodata_ahstm3w5d2.sql";
//获取系统时间
SimpleDateFormat begintime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
File f= new File(path);
System.err.println("begin");
String begintimeStr = begintime.format(System.currentTimeMillis());
getData(path);
//获取系统时间
SimpleDateFormat endtime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String endtimeStr = endtime.format(System.currentTimeMillis());
if (f.exists() && f.isFile()){
long fsize = f.length();
String fname =f.getName();
String sql = "insert into log(filename,begintime,endtime,filesize)"+
"values('"+fname+"','"+begintimeStr+"','"+endtimeStr+"', '"+fsize+"')";
qr.update(sql);
}
}
private static void getData(String path) {
//读取文件
BufferedReader reader;
Connection conn = null;
Statement pst = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/dataanaysismodel?useUnicode=true&characterEncoding=utf-8", "root", "root");
pst = conn.createStatement();
reader = new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-8"));
String line;
int i = 0;
while ((line = reader.readLine()) != null) {
pst.addBatch(line);
/* System.out.println("-----------------------");
System.out.println(line);
System.out.println("-----------------------"); */
if (i % 100 == 0) {
System.out.println("执行了:" + i);
//pst.executeBatch();
}
i += 1;
}
reader.close();
// 执行批量更新
pst.executeBatch();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (pst != null) {
pst.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}