在java中处理空字符串

时间:2014-04-07 04:11:23

标签: java string null

我正在读取CSV文件并插入到table.but当我得到completdate为null时我想插入默认date.i选中此

if(COMPLETEDATE == null){
    css.setString(24, "2013-06-12 00:00:00.0");
}else{
    css.setString(24, COMPLETEDATE);
}      

这是整个功能。

public void ImportCSV() {
    String csvFile = "C:\\seema\\CSV Files\\2013\\August\\15.csv";
    BufferedReader br = null;
    String line = "";
    String cvsSplitBy = ",";
    String PRODLINE,EMPID,EMPFNAME,SHIFT,STATIONID,CURWKDATE,OPCODE,OPDESC,STARTWORKTIME,ENDWORKTIME,PIECECNT,
           BUNDLECNT,PIECERATE,SAM,SKILLLEVEL,DAILYBONUS,NORMALRATE,OVERTIMERATE,WORKDURATION,MONO,DESIGNCODE,
           DESIGNCOLOR,DESIGNSIZE,COMPLETEDATE;

    int i=0;
    try {
    br = new BufferedReader(new FileReader(csvFile));
    while ((line = br.readLine()) != null) {
      try {
        PreparedStatement css = null;
        css= conn.prepareStatement("exec uspInsertEWLProduction ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?");

        String[] country = line.split(",");

        PRODLINE=country[0];
        EMPID=country[1];
        EMPFNAME =country[2];
        SHIFT=country[3];
        STATIONID=country[4];
        CURWKDATE =country[5];
        OPCODE=country[6];
        OPDESC=country[7];
        STARTWORKTIME =country[8];
        ENDWORKTIME=country[9];
        PIECECNT=country[10];
        BUNDLECNT =country[11];
        PIECERATE=country[12];
        SAM=country[13];
        SKILLLEVEL =country[14];
        DAILYBONUS=country[15];
        NORMALRATE=country[16];
        OVERTIMERATE =country[17];
        WORKDURATION=country[18];
        MONO=country[19];
        DESIGNCODE =country[20];
        DESIGNCOLOR=country[21];
        DESIGNSIZE=country[22];
        COMPLETEDATE =country[23];

        if(i!=0) {
           css.setString(1, PRODLINE);    
           css.setString(2, EMPID);
           css.setString(3, EMPFNAME);
           css.setString(4, SHIFT); 
           css.setString(5, STATIONID);
           css.setString(6, CURWKDATE);
           css.setString(7, OPCODE);    
           css.setString(8, OPDESC);
           css.setString(9, STARTWORKTIME);
           css.setString(10, ENDWORKTIME); 
           css.setString(11, PIECECNT);  
           css.setString(12, BUNDLECNT);
           css.setString(13, PIECERATE);
           css.setString(14, SAM);
           css.setString(15, SKILLLEVEL);
           css.setString(16, DAILYBONUS);
           css.setString(17, NORMALRATE);
           css.setString(18, OVERTIMERATE);
           css.setString(19, WORKDURATION);
           css.setString(20, MONO);
           css.setString(21, DESIGNCODE);
           css.setString(22, DESIGNCOLOR);
           css.setString(23, DESIGNSIZE); 

           if(COMPLETEDATE == null) {
              css.setString(24, "2013-06-12 00:00:00.0");
           } else {
              css.setString(24, COMPLETEDATE);
           }
         }
         css.executeUpdate();
       } catch (Exception e) {
         e.printStackTrace();
       }
       i++;
     }
     JOptionPane.showMessageDialog(null, "Data Imported Successfully");
   } catch (FileNotFoundException e) {
     e.printStackTrace();
   } 
} 

问题是其他部分永远不会被执行,尽管copmletedate为null。任何解决方案?

5 个答案:

答案 0 :(得分:1)

您可以使用Apache Commons Utility StringUtils.isEmpty(CharSequence)来检查nullempty字符串。顺便说一句,为什么你把日期存储为字符串,而不是日期?

if (StringUtils.isEmpty(COMPLETEDATE)) {
    // set default date
} else {
    // set COMPLETEDATE
}

答案 1 :(得分:0)

尝试将if语句更改为:

if(COMPLETEDATE.equals("")) 

答案 2 :(得分:0)

您可能需要检查null和空字符串。像这样:

            if(COMPLETEDATE != null && !("".equals(COMPLETEDATE.trim)))
            {
                css.setString(24, COMPLETEDATE);

            }
            else
            {
                css.setString(24, "2013-06-12 00:00:00.0");
            }                

答案 3 :(得分:0)

答案 4 :(得分:0)

你可以做一件事比较两件事

if(COMPLETEDATE != null || !("".equals(COMPLETEDATE.trim())))
        {
            css.setString(intlength, xyz);

        }
        else
        {
            css.setString(intlength, "Stringdate");
        }

愿这会有所帮助。

如果你采取循环然后试试这个。

if(country[23] != null && !(country[23].equals(""))){
      // set your date code
}else{
    // set String date
}