Advertisement
Guest User

Untitled

a guest
Mar 14th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.17 KB | None | 0 0
  1. public void ReadExcelDemo () throws java.io.IOException, javax.servlet.ServletException{
  2.  
  3. FileInputStream fis = new FileInputStream(
  4. new File("C:/Users/hunt/Desktop/DAILY MONITORING.xls"));
  5. HSSFWorkbook workbook = new HSSFWorkbook(fis);
  6. HSSFSheet spreadsheet = workbook.getSheetAt(0);
  7. Iterator rowIterator = spreadsheet.rowIterator();
  8. while (rowIterator.hasNext())
  9. {
  10. HSSFRow row = (HSSFRow)rowIterator.next();
  11.  
  12. Iterator cellIterator = row.cellIterator();
  13. while ( cellIterator.hasNext())
  14. {
  15. HSSFCell cell = (HSSFCell) cellIterator.next();
  16. switch (cell.getCellType())
  17. {
  18. case HSSFCell.CELL_TYPE_NUMERIC:
  19. System.out.print(
  20. cell.getNumericCellValue() + " tt " );
  21. break;
  22. case HSSFCell.CELL_TYPE_STRING:
  23. System.out.print(
  24. cell.getStringCellValue() + " tt " );
  25. break;
  26. case HSSFCell.CELL_TYPE_BLANK:
  27. System.out.print(
  28. cell.getStringCellValue() + " tt " );
  29. break;
  30. }
  31. }
  32. System.out.println();
  33. }
  34. fis.close();
  35. }
  36.  
  37. public void insertData () throws java.io.IOException, javax.servlet.ServletException{
  38. try{
  39. Logger.getInstance().logDebug("MSKASMAKSKASM");
  40.  
  41. Driver DB2Driver = (java.sql.Driver)
  42. Class.forName("COM.ibm.db2os390.sqlj.jdbc.DB2SQLJDriver").newInstance();
  43. Logger.getInstance().logDebug("MSKASMAKSKASM");
  44. Connection con = (Connection) DriverManager.getConnection("jdbc:db2://192.168.63.128:50000/IPMS_TMP", "ipms", "ipms");
  45. //jdbc:db2://192.168.63.128:50000/TMP", "ipms", "ipms"
  46.  
  47. Logger.getInstance().logDebug("erererererere");
  48. con.setAutoCommit(false);
  49. PreparedStatement pstm = null ;
  50. FileInputStream input = new FileInputStream("C:/Users/hunt/Desktop/DAILY MONITORING.xls");
  51.  
  52. Logger.getInstance().logDebug("after database");
  53.  
  54. POIFSFileSystem fs = new POIFSFileSystem( input );
  55. HSSFWorkbook wb = new HSSFWorkbook(fs);
  56. HSSFSheet sheet = wb.getSheetAt(0);
  57. HSSFRow row;
  58. //HSSFCell cell = null;
  59. for(int i=1; i<=sheet.getLastRowNum(); i++){
  60. row = sheet.getRow(i);
  61.  
  62.  
  63. if(row ==null) continue;
  64. HSSFCell cell = row.getCell((short) 0);
  65. if(cell==null) continue;
  66. String val = cell.toString();
  67.  
  68. SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy",Locale.ENGLISH);
  69.  
  70.  
  71.  
  72. Date MONITOR_DATE = row.getCell((short) 8).getDateCellValue();
  73. String SHIFT_1_TECHNICIAN = row.getCell((short) 9).getStringCellValue();
  74. String SHIFT_2_TECHNICIAN = row.getCell((short) 10).getStringCellValue();
  75. String SHIFT_3_TECHNICIAN = row.getCell((short) 11).getStringCellValue();
  76. int METER_FROM = (int) row.getCell((short) 14).getNumericCellValue();
  77. int METER_TO = (int) row.getCell((short) 14).getNumericCellValue();
  78. int METER_HOURS = (int) row.getCell((short) 14).getNumericCellValue();
  79. String DEPLOYMENT_1ST = row.getCell((short) 14).getStringCellValue();
  80. String DEPLOYMENT_2ND = row.getCell((short) 14).getStringCellValue();
  81. String DEPLOYMENT_3RD = row.getCell((short) 14).getStringCellValue();
  82. int PM_FROM = (int) row.getCell((short) 14).getNumericCellValue();
  83. int PM_TO = (int) row.getCell((short) 14).getNumericCellValue();
  84. int PM_HOURS = (int) row.getCell((short) 14).getNumericCellValue();
  85. int BREAK_FROM = (int) row.getCell((short) 14).getNumericCellValue();
  86. int BREAK_TO = (int) row.getCell((short)14).getNumericCellValue();
  87. int BREAK_HOURS = (int) row.getCell((short) 14).getNumericCellValue();
  88. String REMARKS = row.getCell((short) 14).getStringCellValue();
  89. String PHE_NO = row.getCell((short) 14).getStringCellValue();
  90. String CAPACITY = row.getCell((short) 14).getStringCellValue();
  91.  
  92. 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(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
  93. pstm = (PreparedStatement) con.prepareStatement(sql);
  94. pstm.execute();
  95. System.out.println("Import rows "+i);
  96. }
  97. con.commit();
  98. pstm.close();
  99. con.close();
  100. input.close();
  101. System.out.println("Success import excel to db2 table");
  102. }catch(SQLException ex){
  103. System.out.println(ex);
  104. }catch(IOException ioe){
  105. System.out.println(ioe);
  106. } catch (ClassNotFoundException e) {
  107. // TODO Auto-generated catch block
  108. e.printStackTrace();
  109. } catch (InstantiationException e) {
  110. // TODO Auto-generated catch block
  111. e.printStackTrace();
  112. } catch (IllegalAccessException e) {
  113. // TODO Auto-generated catch block
  114. e.printStackTrace();
  115. }
  116.  
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement