2017-06-23 14 views
-1

私のExcelシートには7つの列があります。それぞれの列名を持つデータベースに空のテーブルを作成しました。 どうすればExcelのすべてのデータをデータベーステーブルに挿入できますか? 私は、ありがとう助けてください。JAVAを使用してExcelからOracleデータベースにデータを挿入

+1

例のデータとコード作成の距離を教えてください。ギルドラインはこれも指定しています。 – selten98

+0

1. Excellから読み込みます。2.データベースに入れます。あなたはどんな問題を抱えていますか? –

+0

申し訳ありませんが、私はそれを試していません。私はそれの完全なコードが欲しいです。 –

答えて

0

あなたはそうするためにPOIを使うことができます。サンプルコードは以下の通りです。詳細についてはthisを参照してください。

/* Create Connection objects */ 
      Class.forName ("oracle.jdbc.OracleDriver"); 
      Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@//localhost:1521/xe", "hr", "hr"); 
      PreparedStatement sql_statement = null; 
      String jdbc_insert_sql = "INSERT INTO XLS_POI" 
          + "(KEYWORD, TOTAL_COUNT) VALUES" 
          + "(?,?)"; 
      sql_statement = conn.prepareStatement(jdbc_insert_sql); 
      /* We should now load excel objects and loop through the worksheet data */ 
      FileInputStream input_document = new FileInputStream(new File("xls_to_oracle.xls")); 
      /* Load workbook */ 
      HSSFWorkbook my_xls_workbook = new HSSFWorkbook(input_document); 
      /* Load worksheet */ 
      HSSFSheet my_worksheet = my_xls_workbook.getSheetAt(0); 
      // we loop through and insert data 
      Iterator<Row> rowIterator = my_worksheet.iterator(); 
      while(rowIterator.hasNext()) { 
        Row row = rowIterator.next(); 
        Iterator<Cell> cellIterator = row.cellIterator(); 
          while(cellIterator.hasNext()) { 
            Cell cell = cellIterator.next(); 
            switch(cell.getCellType()) { 
            case Cell.CELL_TYPE_STRING: //handle string columns 
              sql_statement.setString(1, cell.getStringCellValue());                      
              break; 
            case Cell.CELL_TYPE_NUMERIC: //handle double data 
              sql_statement.setDouble(2,cell.getNumericCellValue()); 
              break; 
            } 

          } 
      //we can execute the statement before reading the next row 
      sql_statement.executeUpdate(); 
      } 
      /* Close input stream */ 
      input_document.close(); 
      /* Close prepared statement */ 
      sql_statement.close(); 
      /* COMMIT transaction */ 
      conn.commit(); 
      /* Close connection */ 
      conn.close(); 
関連する問題