Java高效实现批处理Ecel数据导入数据库代码_第1页
Java高效实现批处理Ecel数据导入数据库代码_第2页
Java高效实现批处理Ecel数据导入数据库代码_第3页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、(SQLServer)库入数据处理Excel敌据导批Java实现注:这是我Z前写的Java实现批处理Excel数据导入数据库代码绝对原创(掀 初发表于百度帕客)可以实现批址处理.效率很高.便用时里面有些(尤其那些红色部分)地方需耍改为自己的数 据信息package com.jxl;import jxl.Cell;import jxl.Sheet;import java.io.File;import java.i 0nputStream;import java.sql?;import java.util.Vector;import java.io.Filel nputStream;import

2、jxl .Workbook;/* 耗时 315.469 秒 jxl.jar excel 导入 SQL 包*耗时131.469秒*耗时125.448秒(系数6000)*/public class ExcelReadPre public static void main(StringQ args) ExcelReadPre cr = new ExcelReadPre();long start = System.currentTimeMillis();/ 记录开始时间cr.testRun();/ 开始执行long end = System.currentTimeMillis();System.out

3、.println(=耗時:=+ (end - start) /1000.0);public void testRun() Wk0527;/自己存放excel文件的目录球械柿jOD?F:QJtryFile fileDir = new File(path);File filesQ = fileDir.listFiles();for (int i = 0; i < files.length; i+) System.out.println(P 第?桶?个文件:=+ filesi.getAbsolutePath();this.writeToDb(filesi.getAbsolutePath();i

4、f (this.con != null && !con.isClosed() this.con.close); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();*执行指定的Sql*/private String url = jdbc:sqlserver:/:1433;database=moble_db;/fl(2的数据库名称private Connection con 二 null;PreparedStateme nt pst = null;public v

5、oid getCOnn() / System.out.pri ntln(二二=二=二二=+sql);tryif (con = null | con.isClosedO) Class.forName(com.microsoft.sqlserverjdbc.SQLServerDriver);con = DriverManager.getConnection(url,烫屋?猫?);/这里数据库的用户名和密码 catch (Exception e) IITODO Auto-generated catch blocke.printStackTrace();严*导入一指定文件数据*批处理SQL* para

6、m filePath*/public void writeToDb(String filePath) /这里是我口己的表结构String sql = insert into msg_mobile (spcode.spname,bizcode,biz name,flow,cost,dow nd ate)valuestrythis.getCOnn();关闭爭务自动捉交 con.setAutoCommit(false);pst = con.prepareStatement(sql);Vector<CellQ> v = readExcel(filePath);int i = 0;for (

7、Ce叩 cells : v) int j = 0;pst.setString(+j, cellsO.getContents();pst.setString(+j, cellsDJ.getContentsO);pst.setString(+j, cellsj.getContents();pst.setString(+j, cellsOJ.getContentsO);pst.setString(+j, cellsOl.getContentsO);pst.setString(+j, cellsUJ.getContentsO);pst.setString(+j, cellsj.getContents(

8、);pst.addBatch(); i+;if(i>6000X/防止一次数址过大.导致内存泄漏 pst.executeBatch();con .commit();pst.clearBatch();i=0;continue;SQL命令加入命令列表把一个/ 执行批 更新 pst.executeBatch();语句执行完毕.提交本爭务/mitO;pst.clearBatch();pst.close();/ System.gcO; catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();严readEx

9、cel*方法名:*参数:filePath为Execl文件的绝对略径放在数组中返回数据.用途:读取Execl*返回类型:Vector*/public static Vector<CellO> readExcel(String filePath) Vector<CellQ> v = new Vector<CellO>();Workbook rwb = null;tryInputstream is = new FilelnputStream(filePath);rwb = Workbook.getWorkbook(is);/获取第一张Sheet表Sheet rs = rwb.getSheet(O);fo(int i = 1; i < rs.getRows(); i+)/ for (int j = 0; j < rs.getColumns(); j+) H System.out.print( +(+rs.getCell(j,II k).getContents().trim();v.add(rs.getRow(i);/ 放入一行记录/ System.out.

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论