seema
seema

Reputation: 13

Handle null string in java

I am reading CSV file and inserting into table.but when I get completdate as null I want to insert default date.i checked this

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

Here is the whole function.

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();
   } 
} 

The problem is else part is never get executed eventhough copmletedate is null. Any solution?

Upvotes: 0

Views: 237

Answers (5)

Kishan Bheemajiyani
Kishan Bheemajiyani

Reputation: 3439

U can do one thing that Compare both thing

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

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

May this will help.

and if u Taking loop then try this.

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

Upvotes: 0

webpandit
webpandit

Reputation: 47

Use StringUtils.isBlank() which checks for null or empty string.

visit http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringUtils.html

Upvotes: 0

Rohit Jain
Rohit Jain

Reputation: 213223

You can use Apache Commons Utility StringUtils.isEmpty(CharSequence) to check for null or empty string. BTW, why are you storing dates as string, and not as date?

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

Upvotes: 1

Juned Ahsan
Juned Ahsan

Reputation: 68715

You may need to check for null and empty string. Something like this:

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

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

Upvotes: 0

Sumedh
Sumedh

Reputation: 404

try changing if statement to:

if(COMPLETEDATE.equals("")) 

Upvotes: 0

Related Questions