0

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?

3
  • Try to do some thing like this if(null==COMPLETEDATE || "".equals(COMPLETEDATE )){//do something} Commented Apr 7, 2014 at 4:13
  • Did you check if COMPLETEDATE is null or just "" (empty string)? It is more likely it is an empty string. Commented Apr 7, 2014 at 4:20
  • Can u paste your stack trace here? Commented Apr 7, 2014 at 4:53

5 Answers 5

1

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
}
Sign up to request clarification or add additional context in comments.

4 Comments

I tried using this but getting this error error: cannot find symbol if (StringUtils.isEmpty(COMPLETEDATE)) { symbol: method isEmpty(String)
@seema download and add the apache commons library in your classpath.
hi rohit can u please share link for that library.i tried commons-lang3-3.3.1 from this commons.apache.org/proper/commons-lang/download_lang.cgi site and added jar files but again same error
@seema It's commons collection, not commons-lang. Here's the link - commons.apache.org/proper/commons-collections/…
0

try changing if statement to:

if(COMPLETEDATE.equals("")) 

2 Comments

this will cause an exception if COMPLETEDATE is null
yes, but if COMPLETEDATE is having null reference, then the try-catch block will handle it.The program requires COMPLETEDATE 'not to have null reference'.So the if statement in the program,only requires to check for null-value, which is exactly what my answer is doing.
0

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

Comments

0

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

Comments

0

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
}

14 Comments

tried this if part is geting executed eventhough COMPLETEDATE is null.
@seema haan but if COMPLETEDATE is null then it will store default Date na? so what is still exception? can u just paste your StaceTrace here
as per ur suggestion if completed date is null it should execute else part.and I don't know what is stacktrace.its my first java program :).I was .net programmer just started with java
@seema its okey StackTrace is what u getting exception in Console just let me know and i have updated that ans too try that.
again same.no any change.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.