java代碼匯入excel資料至oracle

來源:互聯網
上載者:User

標籤:java excel

本文處理的excel格式為xlsx:

    1.建立maven項目        

  <dependencies>

    <dependency>  

                <groupId>org.apache.poi</groupId>  

                <artifactId>poi</artifactId>  

                <version>3.14</version>  

        </dependency>   

        <dependency>  

            <groupId>org.apache.poi</groupId>  

            <artifactId>poi-ooxml</artifactId>  

            <version>3.14</version>  

        </dependency> 

  </dependencies>

(另:ojdbc的jar包由於著作權問題無法直接引入,需要手動下載本次用的是ojdbc7.jar。)

2.檔案(將1.xlsx(資料如)的資料 插入到 表a(deptno int,dname varchar,loc varchar))

DEPTNO DNAME LOC
10
ACCU NEWYORK
20 BB BB
30 VV VV
40 CC CC


3.代碼部分


package exceltest;import java.io.File;import java.io.FileInputStream;import java.io.InputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.sql.Statement;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;public class ExeclOperate {//擷取資料庫連接public Connection conn(){          try {          //第一步:載入JDBC驅動          Class.forName("oracle.jdbc.driver.OracleDriver");          //第二步:建立資料庫連接          Connection con =DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "scott", "tiger");          return con;          }catch(ClassNotFoundException cnf){            System.out.println("driver not find:"+cnf);            return null;          }catch(SQLException sqle){            System.out.println("can‘t connection db:"+sqle);            return null;          }            catch (Exception e) {          System.out.println("Failed to load JDBC/ODBC driver.");          return null;          }      }  public void getExcel() throws Exception {  InputStream is = new FileInputStream(new File("D:\\1.xlsx"));        XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is);        // 擷取每一個工作薄        for (int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++) {            XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet);            if (xssfSheet == null) {                continue;            }            // 擷取當前工作薄的每一行            for (int rowNum = 1; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {                XSSFRow xssfRow = xssfSheet.getRow(rowNum);                if (xssfRow != null) {                    //讀取第一列資料                    String a = getValue(xssfRow.getCell(0));                    Integer one = Integer.parseInt(a.substring(0,a.indexOf(".")));                    //讀取第二列資料                    String two = getValue(xssfRow.getCell(1));                    //讀取第三列資料                    String three = getValue(xssfRow.getCell(2));                                        String insert="insert into a values("+one+",‘"+two+"‘,‘"+three+"‘)";                     System.out.println("SQL:"+insert);                                          insert(insert);                }            }        }}    //轉換資料格式    private String getValue(XSSFCell xssfRow) {        if (xssfRow.getCellType() == xssfRow.CELL_TYPE_BOOLEAN) {            return String.valueOf(xssfRow.getBooleanCellValue());        } else if (xssfRow.getCellType() == xssfRow.CELL_TYPE_NUMERIC) {            return String.valueOf(xssfRow.getNumericCellValue());        } else {            return String.valueOf(xssfRow.getStringCellValue());        }    }            //添加資料    public int insert(String insert) throws SQLException{          Connection conn = this.conn();          int re = 0;          try{              conn.setAutoCommit(false);//事務開始                              Statement sm = conn.createStatement();              re = sm.executeUpdate(insert);              if(re < 0){               //插入失敗                  conn.rollback();      //復原                  sm.close();                  conn.close();                    return re;              }              conn.commit();            //插入正常              sm.close();              conn.close();                return re;          }          catch(Exception e){              e.printStackTrace();          }          conn.close();            return 0;               }           //測試    public static void main(String[] args) throws Exception {          ExeclOperate e=new ExeclOperate();          e.getExcel();          System.out.println("匯入完成!");   } }


本文出自 “cw” 部落格,請務必保留此出處http://cw666.blog.51cto.com/12488150/1946379

java代碼匯入excel資料至oracle

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.