zoukankan      html  css  js  c++  java
  • java 读取excel 将数据插入到数据库

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.util.ArrayList;
    import java.util.List;
     
    import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.xssf.usermodel.XSSFCell;
    import org.apache.poi.xssf.usermodel.XSSFRow;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
     
    import com.lt.util.DBUtil;
    import com.lt.util.Oracle;
     
    public class ReadMain {
     
     
    public static void readFile(File file){
     
    Connection con = null;
    PreparedStatement ps = null;
    String sql = "insert into t_xls(mobile) values(?)";
    int count = 0;
    try {
    con = DBUtil.getConnection();
    ps = con.prepareStatement(sql);
     
     
    InputStream is = new FileInputStream(file);
    XSSFWorkbook wb = new XSSFWorkbook(is);
    XSSFCell cell = null;
    for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets(); sheetIndex++) {
    XSSFSheet st = wb.getSheetAt(sheetIndex);
    for (int rowIndex = 0; rowIndex <= st.getLastRowNum(); rowIndex++) {
    XSSFRow row = st.getRow(rowIndex);
    cell = row.getCell(0);
    cell.setCellType(XSSFCell.CELL_TYPE_STRING);
    count++;
    ps.setString(1, cell.getStringCellValue());
    ps.addBatch();
     
    if(count % 10000 == 0){
    ps.executeBatch();
    }
    }
    }
    ps.executeBatch();
     
    }catch (Exception e) {
    e.printStackTrace();
    }finally{
    DBUtil.close(ps);
    DBUtil.close(con);
    }
    }
     
    public static void main(String[] args) {
    DBUtil.startProxool();
    readFile(new File("C:\Documents and Settings\Administrator\桌面\22.xlsx"));
     
    }
    }
  • 相关阅读:
    [题解] P2513 [HAOI2009]逆序对数列
    [题解]洛谷P2709 小B的询问
    题解 P1944 最长括号匹配_NOI导刊2009提高(1)
    [题解]SP703 SERVICE
    Bzoj2427: [HAOI2010]软件安装
    【题解】UVA11362 Phone List
    【题解】P2922 [USACO08DEC]秘密消息Secret Message
    Tire树的学习
    【题解】P1171 售货员的难题
    计算机与编程资源教程汇总
  • 原文地址:https://www.cnblogs.com/austinspark-jessylu/p/6168669.html
Copyright © 2011-2022 走看看