(SQLServer)
库入数据处理Excel数据导批Java实现注:这是我之前写的Java实现批处理Excel数据导入数据库代码,绝对原创(最初发表于百度博客),可以实现批量处理,效率很高。
使用时,里面有些(尤其那些红色部分)地方需要改为自己的数据信息。
package com.jxl;
import jxl.Cell;
import jxl.Sheet;
import java.io.File;
import java.io.InputStream;
import java.sql.*;
import java.util.Vector;
import java.io.FileInputStream;
import jxl.Workbook;
/**
* 耗时315.469秒jxl.jar excel导入SQL 包
* 耗时131.469秒
* 耗时125.448秒(系数6000)
*/
public class ExcelReadPre {
public static void main(String[] args) {
ExcelReadPre cr = new ExcelReadPre();
long start = System.currentTimeMillis();// 记录开始时间
cr.testRun();// 开始执行
long end = System.currentTimeMillis();
System.out.println(============耗時:==== + ((end - start) / 1000.0));
}
public void testRun() {
\\k0527\\;//自己存放excel文件的目录瑓楲杮瀠瑡??F:\\学习
try {
File fileDir = new File(path);
File files[] = fileDir.listFiles();
for (int i = 0; i < files.length; i++) {
System.out.println(P第?椨??个文件:===== + files[i].getAbsolutePath());
this.writeToDb(files[i].getAbsolutePath());
}
if (this.con != null && !con.isClosed()) {
this.con.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/**
* 执行指定的Sql
*/
private String url = jdbc:sqlserver://127.0.0.1:1433;database=moble_db;//自己的数据库名称
private Connection con = null;
PreparedStatement pst = null;
public void getCOnn() {
// System.out.println(==========+sql);
try {
if (con == null || con.isClosed()) {
Class.forName(com.microsoft.sqlserver.jdbc.SQLServerDriver);
con = DriverManager.getConnection(url, 獜屡?獜??);//这里数据库的用户名和密码
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 导入一指定文件数据
* 批处理SQL
* @param filePath
*/
public void writeToDb(String filePath) {
//这里是我自己的表结构
String sql = insert into msg_mobile (spcode,spname,bizcode,bizname,flow,cost,downdate)values (?,?,?,?,?,?,?);
try {
this.getCOnn();
关闭事务自动提交//
con.setAutoCommit(false);
pst = con.prepareStatement(sql);
Vector<Cell[]> v = readExcel(filePath);
int i = 0;
for (Cell[] cells : v) {
int j = 0;
pst.setString(++j, cells[j].getContents());
pst.setString(++j, cells[j].getContents());
pst.setString(++j, cells[j].getContents());
pst.setString(++j, cells[j].getContents());
pst.setString(++j, cells[j].getContents());
pst.setString(++j, cells[j].getContents());
pst.setString(++j, cells[j].getContents());
pst.addBatch(); i++;
if(i>6000){
//防止一次数量过大,导致内存泄漏
pst.executeBatch(); mit();
pst.clearBatch();
i=0;
continue;
}
SQL命令加入命令列表// 把一个}
// 执行批量更新pst.executeBatch();
语句执行完毕,提交本事务//
mit();
pst.clearBatch();
pst.close();
// System.gc();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*
readExcel* 方法名:* 参数:filePath为Execl文件的绝对路径
,放在数组中返回数据* 用途:读取Execl* 返回类型:Vector
**/
public static Vector<Cell[]> readExcel(String filePath) {
Vector<Cell[]> v = new Vector<Cell[]>();
Workbook rwb = null;
try {
InputStream is = new FileInputStream(filePath);
rwb = Workbook.getWorkbook(is);
// 获取第一张Sheet表
Sheet rs = rwb.getSheet(0);
for (int i = 1; i < rs.getRows(); i++) {
// for (int j = 0; j < rs.getColumns(); j++) {
// System.out.print(\+(\+rs.getCell(j,
// k).getContents()).trim());
v.add(rs.getRow(i));// 放入一行记录
// System.out.println(==========+rs.getRow(i)[4].getContents());
// }
// System.out.println();
}
is.close();
rwb.close();
System.gc();
} catch (Exception e) {
e.printStackTrace();
}
return v;
}
}。