各地旅遊資訊,或是各地自由行規範範本


文章標籤

咪卡恰比 發表在 痞客邦 留言(0) 人氣()

var jsonObj=[]; 
var tempPrdList=[];

tempPrdList.push("12");
tempPrdList.push("34");
tempPrdList.push("56");

for (i = 0, j = tempPrdList.length; i < j; i++) {
  var obj = new Object;
  obj.id = tempPrdList[i];  //key=id
  jsonObj.push(obj);
}

console.log(JSON.stringify(jsonObj));  //[{"id":"12"},{"id":"34"},{"id":"56"}]

文章標籤

咪卡恰比 發表在 痞客邦 留言(0) 人氣()

java.util.Date cannot be cast to java.sql.Date

Date date=new Date(); 
new java.sql.Date(date.getTime()) ;

https://stackoverflow.com/questions/18614836/using-setdate-in-preparedstatement

 

 


咪卡恰比 發表在 痞客邦 留言(0) 人氣()

執行時產生 runtime exception  ( unchecked exception ):

  • extends Exception
  • 程式上會有開發者無法預期的錯誤
  • 異常會一路拋出至JVM處理
class selfException extends RuntimeException{
}

應受控制產生  Checked Exception :

  • 此類型錯誤應該被處理(開發者認知會有異常發生)
  • 需用try catch於當下處理
    class selfException extends Exception{
    }

參考資料:https://openhome.cc/Gossip/Java/ThrowableHierarchy.html


文章標籤

咪卡恰比 發表在 痞客邦 留言(0) 人氣()

把EXCEL資料爬出成bean
雖然好像有一些人寫了用ANNOCATION去對應BEAN的JAR檔
不過先用簡單的方式來做一次吧

重點是beanTitle 要和EXCEL資料對應到,EXCEL的一行的第一格會是beanTitle[0]
 

public void mainWork(){
        String filePath = "feebeeApSet.xlsx";
        String[] beanTitle = {"id", "type", "startDate"};  //excel欄位對應到bean的tag
        this.translateToBeanFromXls(filePath, TestBean.class, beanTitle);  //call function
}

//testBean.class
public class TestBean implements Serializable {
        
    private String id;
    
    private String type;

    private String startDate;
    
        public String getId() {
                return id;
        }

        public void setId(String id) {
                this.id = id;
        }

        public String getType() {
                return type;
        }

        public void setType(String type) {
                this.type = type;
        }

        public String getStartDate() {
                return startDate;
        }

        public void setStartDate(String startDate) {
                this.startDate = startDate;
        }
}


public <T> List<T> translateToBeanFromXls(String filePath, Class<T> objT, String[] beanTitle) throws Exception {

                List<T> beanList = new LinkedList<T>();
                
                if(StringUtils.isBlank(filePath) || objT == null || ArrayUtils.isEmpty(beanTitle)){
                        //data is null!
                }else{
                        FileInputStream fs = null;
                        XSSFWorkbook workbook = null;
                        try{
                                int beanTitleSize = beanTitle.length;
                                fs = new FileInputStream(filePath); 
                                //EXCEL workbook
                                workbook = new XSSFWorkbook(fs);
                                //EXCEL sheet
                                XSSFSheet sheet = workbook.getSheetAt(0);
                                
                                //row num
                                int rowNum = sheet.getPhysicalNumberOfRows();
                                log.info(filePath + "_rowNum: " + rowNum);
                                //總行數
                                int lastRowIndex = sheet.getLastRowNum();
                                log.info(filePath + "_lastRowNum: " + lastRowIndex);
                                //TITLE單位格數
                                int titleLastCellNum = sheet.getRow(0).getLastCellNum();
                                log.info(filePath + "_title.lastCellNum: " + titleLastCellNum);
                                
                                //判斷EXCEL是否基本資料錯誤
                                //1.含title 最低限制為兩列
                                //2.TITLE的格數須與beanTitle資料相同
                                if (rowNum < 2) {
                                        //ERROR
                                }else if(beanTitleSize!=titleLastCellNum){
                                        //ERROR
                                }else{
                                        for(int i = 1; i <= lastRowIndex; i++) {
                                                XSSFRow row = sheet.getRow(i);
                                                //判斷該行的單元數和beanTitle數是否能相應
                                                int thisLastCellNum = row.getLastCellNum(); 
                                                if(beanTitleSize!=thisLastCellNum){
                                                        //ERROR
                                                }else{
                                                        T bean = objT.newInstance();
                                                        
                                                        //處理格數
                                                        for (int j = 0; j < thisLastCellNum; j++) {
                                                                XSSFCell cell = row.getCell(j);
                                                                Object cellValue = null;
                                                                
                                                                switch (cell.getCellType()) {
                                                                        case XSSFCell.CELL_TYPE_BLANK:
                                                                                cellValue = "";
                                                                                break;
                                                                        case XSSFCell.CELL_TYPE_STRING:
                                                                                cellValue = cell.getStringCellValue().trim();
                                                                                break;
                                                                        case XSSFCell.CELL_TYPE_BOOLEAN:
                                                                                cellValue = cell.getBooleanCellValue();
                                                                                break;
                                                                        case XSSFCell.CELL_TYPE_NUMERIC:
                                                                                short format = cell.getCellStyle().getDataFormat();
                                                                                if (HSSFDateUtil.isCellDateFormatted(cell)) {
                                                                                        Date date = cell.getDateCellValue();
                                                                                        DateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                                                                        cellValue = formater.format(date);
                                                                                }else if(format == 14 || format == 31 || format == 57 || format == 58){
                                                                                        DateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
                                                                                        Date date = DateUtil.getJavaDate(cell.getNumericCellValue());
                                                                                        cellValue = formater.format(date);
                                                                                }else if (format == 20 || format == 32) {
                                                                                        DateFormat formater = new SimpleDateFormat("HH:mm");
                                                                                        Date date = DateUtil.getJavaDate(cell.getNumericCellValue());
                                                                                        cellValue = formater.format(date);
                                                                                } else{
                                                                                        cellValue = cell.getNumericCellValue();
                                                                                }
                                                                                break;

                                                                        default:
                                                                                //其他格式不處理
                                                                                cellValue = null;
                                                                                break;
                                                                }
                                                                
                                                                if(cellValue!=null){
                                                                        // 轉成bean
                                                                        BeanUtils.setProperty(bean,
                                                                                        beanTitle[j],
                                                                                        cellValue);
                                                                }
                                                        }
                                                        beanList.add(bean);
                                                }
                                        }
                                }
                        }finally{
                                if(fs!=null){fs.close();}
                        }
                }
                
                return beanList;
}

 


文章標籤

咪卡恰比 發表在 痞客邦 留言(0) 人氣()


STEP 1:

第一層:使用 .navbar
附加設訂:

  • bootstrap v4:
    設定適應度:.navbar-expand{-sm|-md|-lg|-xl}
    設定顏色:color scheme class
  • bootstrap v3:
文章標籤

咪卡恰比 發表在 痞客邦 留言(0) 人氣()

sheet.getPhysicalNumberOfRows();
獲取有記錄的行數 (NULL列數略過不計)


sheet.getLastRowNum();
獲取最後行數,回傳值為最後行數-1 (只有一行的話回傳0)
the number of the last row contained in this sheet, zero based.
Owing to idiosyncrasies in the excel file format, if the result of calling this method is zero, you can't tell if that means there are zero rows on the sheet, or one at position zero. For that case, additionally call getPhysicalNumberOfRows() to tell if there is a row at position zero or not.

文章標籤

咪卡恰比 發表在 痞客邦 留言(0) 人氣()