Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void ReadExcelDemo () throws java.io.IOException, javax.servlet.ServletException{
- FileInputStream fis = new FileInputStream(
- new File("C:/Users/hunt/Desktop/DAILY MONITORING.xls"));
- HSSFWorkbook workbook = new HSSFWorkbook(fis);
- HSSFSheet spreadsheet = workbook.getSheetAt(0);
- Iterator rowIterator = spreadsheet.rowIterator();
- while (rowIterator.hasNext())
- {
- HSSFRow row = (HSSFRow)rowIterator.next();
- Iterator cellIterator = row.cellIterator();
- while ( cellIterator.hasNext())
- {
- HSSFCell cell = (HSSFCell) cellIterator.next();
- switch (cell.getCellType())
- {
- case HSSFCell.CELL_TYPE_NUMERIC:
- System.out.print(
- cell.getNumericCellValue() + " tt " );
- break;
- case HSSFCell.CELL_TYPE_STRING:
- System.out.print(
- cell.getStringCellValue() + " tt " );
- break;
- case HSSFCell.CELL_TYPE_BLANK:
- System.out.print(
- cell.getStringCellValue() + " tt " );
- break;
- }
- }
- System.out.println();
- }
- fis.close();
- }
- public void insertData () throws java.io.IOException, javax.servlet.ServletException{
- try{
- Logger.getInstance().logDebug("MSKASMAKSKASM");
- Driver DB2Driver = (java.sql.Driver)
- Class.forName("COM.ibm.db2os390.sqlj.jdbc.DB2SQLJDriver").newInstance();
- Logger.getInstance().logDebug("MSKASMAKSKASM");
- Connection con = (Connection) DriverManager.getConnection("jdbc:db2://192.168.63.128:50000/IPMS_TMP", "ipms", "ipms");
- //jdbc:db2://192.168.63.128:50000/TMP", "ipms", "ipms"
- Logger.getInstance().logDebug("erererererere");
- con.setAutoCommit(false);
- PreparedStatement pstm = null ;
- FileInputStream input = new FileInputStream("C:/Users/hunt/Desktop/DAILY MONITORING.xls");
- Logger.getInstance().logDebug("after database");
- POIFSFileSystem fs = new POIFSFileSystem( input );
- HSSFWorkbook wb = new HSSFWorkbook(fs);
- HSSFSheet sheet = wb.getSheetAt(0);
- HSSFRow row;
- //HSSFCell cell = null;
- for(int i=1; i<=sheet.getLastRowNum(); i++){
- row = sheet.getRow(i);
- if(row ==null) continue;
- HSSFCell cell = row.getCell((short) 0);
- if(cell==null) continue;
- String val = cell.toString();
- SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy",Locale.ENGLISH);
- Date MONITOR_DATE = row.getCell((short) 8).getDateCellValue();
- String SHIFT_1_TECHNICIAN = row.getCell((short) 9).getStringCellValue();
- String SHIFT_2_TECHNICIAN = row.getCell((short) 10).getStringCellValue();
- String SHIFT_3_TECHNICIAN = row.getCell((short) 11).getStringCellValue();
- int METER_FROM = (int) row.getCell((short) 14).getNumericCellValue();
- int METER_TO = (int) row.getCell((short) 14).getNumericCellValue();
- int METER_HOURS = (int) row.getCell((short) 14).getNumericCellValue();
- String DEPLOYMENT_1ST = row.getCell((short) 14).getStringCellValue();
- String DEPLOYMENT_2ND = row.getCell((short) 14).getStringCellValue();
- String DEPLOYMENT_3RD = row.getCell((short) 14).getStringCellValue();
- int PM_FROM = (int) row.getCell((short) 14).getNumericCellValue();
- int PM_TO = (int) row.getCell((short) 14).getNumericCellValue();
- int PM_HOURS = (int) row.getCell((short) 14).getNumericCellValue();
- int BREAK_FROM = (int) row.getCell((short) 14).getNumericCellValue();
- int BREAK_TO = (int) row.getCell((short)14).getNumericCellValue();
- int BREAK_HOURS = (int) row.getCell((short) 14).getNumericCellValue();
- String REMARKS = row.getCell((short) 14).getStringCellValue();
- String PHE_NO = row.getCell((short) 14).getStringCellValue();
- String CAPACITY = row.getCell((short) 14).getStringCellValue();
- String sql = "INSERT INTO EQUIPMENT_MONITORING('"+MONITOR_DATE+"','"+SHIFT_1_TECHNICIAN+"','"+SHIFT_2_TECHNICIAN+"','"+SHIFT_3_TECHNICIAN+"','"+METER_FROM+"','"+METER_TO+"','"+METER_HOURS+"', '"+DEPLOYMENT_1ST+"', '"+DEPLOYMENT_2ND+"', '"+DEPLOYMENT_3RD+"','"+PM_FROM+"','"+PM_TO+"', '"+PM_HOURS+"','"+BREAK_FROM+"','"+BREAK_TO+"','"+BREAK_HOURS+"', '"+REMARKS+"','"+PHE_NO+"', '"+CAPACITY+"' )VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
- pstm = (PreparedStatement) con.prepareStatement(sql);
- pstm.execute();
- System.out.println("Import rows "+i);
- }
- con.commit();
- pstm.close();
- con.close();
- input.close();
- System.out.println("Success import excel to db2 table");
- }catch(SQLException ex){
- System.out.println(ex);
- }catch(IOException ioe){
- System.out.println(ioe);
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (InstantiationException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement