将Excel表格中的数据更新到数据库

来源:互联网 发布:python自动化开发 编辑:程序博客网 时间:2024/06/07 14:26

在工作中,遇见数据的导入。有的是文本文件,有的是在Excel表格里面的数据。我这次的工作就是讲Excel表中的数据更新到数据的表里面。

需要更新的数据库表里面字段如上图:表A、ADMINSTRATIVE_CODE字段、TAR_TOPONYM字段;

右图为:Excel表格里面的数据                                                                          

业务逻辑:根据Excel表格里面 第二列的数据去遍历 表A中的TAR_TOPONYM字段 找到 值 相等的 数据 去更新A表中 ADMINSTRATIVE_CODE字段;

代码如下:

package test;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.sql.Statement;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFWorkbook;public class Test07{  public static void main(String[] args) throws SQLException, ClassNotFoundException{    // 文件所在路径      String execelFile = "D:/asdf.xlsx";    try {        // 构造 Workbook 对象,execelFile 是传入文件路径(获得Excel工作区)        Workbook book = null;        try {            // Excel 2007获取方法            book = new XSSFWorkbook(new FileInputStream(execelFile));        } catch (Exception ex) {            // Excel 2003获取方法            book = new HSSFWorkbook(new FileInputStream(execelFile));        }                // 读取表格的第一个sheet页        XSSFSheet sheet = (XSSFSheet)book.getSheetAt(0);        // 定义 row、cell        // 总共有多少行,从0开始        int totalRows = sheet.getLastRowNum() ;        Class.forName("oracle.jdbc.driver.OracleDriver");        //第二步:创建数据库连接        Connection con =DriverManager.getConnection("jdbc:oracle:thin:@117.78.35.241:1521:alix", "alix", "alix");      con.setAutoCommit(false);      Statement st = con.createStatement();      int commitCount = 0;      // 循环输出表格中的内容,首先循环取出行,再根据行循环取出列        for (int i = 0; i <= totalRows; i++) {          XSSFRow row = sheet.getRow(i);            // 处理空行            if(row == null){                continue ;            }            // 总共有多少列,从0开始            String cell1=null;          String cell2=null;          int totalCells = row.getLastCellNum() ;            for (int j = row.getFirstCellNum(); j < totalCells; j++) {                // 处理空列                if(row.getCell(j) == null){                    continue ;                }                // 通过 row.getCell(j).toString() 获取单元格内容                 cell1=publicExcel(sheet.getRow(i).getCell(0));               cell2=publicExcel(sheet.getRow(i).getCell(1));                        }                      String sql="UPDATE TSYS_PRM_ADDRESS  set ADMINISTRATIVE_CODE = '"+cell1 +"' where "              + "TAR_TOPONYM='"+cell2+"'";          //System.out.println(sql);          st.addBatch(sql);          if(i > 0 && (i % 100 == 0)){            System.out.println("100条提交一次!");            commitCount++;            try{              int[] res = st.executeBatch();              for(int j=0;j<res.length;j++){                //System.out.println(res[j]);                  if(res[j]<0){                    System.out.println(sql);                  }              }                            con.commit();            }catch(Exception e){              e.printStackTrace();              con.rollback();            }finally{              st.clearBatch();            }          }      }        System.out.println("提交批次数:"+commitCount);      st.close();      con.close();    } catch (FileNotFoundException e) {          e.printStackTrace();      } catch (IOException e) {          e.printStackTrace();      }     }    /**    * execl数据格式的转换    * @param xssfCell    * @return    */    public static String publicExcel( XSSFCell xssfCell){        String value = null;                      switch (xssfCell.getCellType()) {              case HSSFCell.CELL_TYPE_NUMERIC:               value = "" + xssfCell.getNumericCellValue();               break;              case HSSFCell.CELL_TYPE_STRING:               value = xssfCell.getStringCellValue();               break;              case HSSFCell.CELL_TYPE_BLANK:               ;               break;              default:             }        return value;    }    }


阅读全文
1 0