The following program works well.But it cant identify any null values present in the csv file. For Example if csv file contains "Suren,Java,Dojo,Hibernate,,Spring,SOA" .I reads first four strings properly but for fifth string instead of reading the null value it reads the sixth string.
Pl. help.

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.StringTokenizer;

import java.sql.*;

    public class readCSVnew1 { 

        static int a=1;


            static String Sl_NO;
            static String Emp_No; 
            static String Employee_Name;
            static String Grade;
            static String Job; 
            static String Location;
            static String Cost_Center; 
            static String Parent_Cost_Center;
            static String Status; 
            static String Onsite; 
            static String OnsBil; 
            static String Account; 
            static String Joining_Date; 
            static String Billing_Date; 
            static String Parent_Org_Unit; 
            static String NB_Free_Days;
            static String Sec_Supervisor_No; 
            static String Sec_Supervisor_Name; 
            static String Region_Name; 
            static String Planned_Released_Date; 



            public static void main(String args[]) throws IOException { 

            //main method starts 



            int n;



            String fName = "Book3.csv";//csv file which u wanna read 

            String thisLine; //string variable which take each record at a time

            int count=0; //to count no. of records





            FileInputStream fis = new FileInputStream(fName); 
            //A FileInputStream obtains input bytes from a file in a file system

            DataInputStream myInput = new DataInputStream(fis);
            /*data input stream lets an application read primitive Java data types from an underlying input stream in a machine-independent way*/

            while ((thisLine = myInput.readLine()) != null){ //beginning of outer while loop 
                count=0;


                if(a==1){
                thisLine = myInput.readLine();
                a++;
                }


                StringTokenizer st =new StringTokenizer(thisLine,",");

                while(st.hasMoreElements()){

                    String field = st.nextToken();

                //System.out.print(field+"@$@");




                if(count==0){

                        n=field.length();   
                        Sl_NO=field.substring(0,n);


                    }


                    if(count==1){
                        n=field.length();   
                        Emp_No=(field.substring(0,n));
                        //System.out.print(Serial);
                    }



                    if(count==2){
                        n=field.length();
                        Employee_Name=field.substring(0,n);
                    }


                    if(count==3){
                        n=field.length();
                        Grade=field.substring(0,n);
                    }


                    if(count==4){
                        n=field.length();
                        Job=field.substring(0,n); 
                    }



                    if(count==5){
                        n=field.length();
                        Location=field.substring(0,n); 
                    }

                    if(count==6){
                        n=field.length();
                        Cost_Center=field.substring(0,n);
                    }

                    if(count==7){
                        n=field.length();
                        Parent_Cost_Center=field.substring(0,n); 
                    }

                    if(count==8){
                        n=field.length();
                        Status=field.substring(0,n);
                    }



                    if(count==9){
                        n=field.length();
                        Onsite=field.substring(0,n); 
                    }

                    if(count==10){
                        n=field.length();
                        OnsBil=field.substring(0,n); 
                    }

                    if(count==11){
                        n=field.length();

                        Account=field.substring(0,n); 
                    }

                    if(count==12){
                        n=field.length();
                        Joining_Date=field.substring(0,n); 
                    }

                    if(count==13){
                        n=field.length();
                        //Billing_Date="NIL";
                        Billing_Date=field.substring(0,n); 
                        //if(n=="\n")
                        //Billing_Date="NIL";

                    }

                    if(count==14){
                        n=field.length();
                        Parent_Org_Unit=field.substring(0,n); 
                    }

                    if(count==15){
                        n=field.length();
                        NB_Free_Days=field.substring(0,n); 
                    }



                    if(count==16){
                        n=field.length();
                        Sec_Supervisor_No=field.substring(0,n);  
                    }       



                    if(count==17){
                        n=field.length();
                        //Sec_Supervisor_Name="Nil";
                        //if(n>0)
                        Sec_Supervisor_Name=field.substring(0,n); 
                    }

                    if(count==18){
                        n=field.length();
                        Region_Name=field.substring(0,n); 
                    }

                    if(count==19){
                        n=field.length();
                        Planned_Released_Date=field.substring(0,n); 
                    }





                count++;

            }

            System.out.println();
            //System.out.print(Project_Category+"@$@");




            try {




                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                String dataSourceName="mdbTEST";
                String dbURL = "jdbc:odbc:DATABASE_NAMEXXX";

                Connection con=DriverManager.getConnection(dbURL," "," ");


                Statement s=con.createStatement( );


                System.out.println("Hi");


                int a=23;       

                System.out.println("Hii");


                s.executeUpdate("insert into SAP_DUMP(Emp_No,Location,Cost_Center,Parent_Cost_Center,Status,Onsite,OnsBil,Joining_Date,Billing_Date,Parent_Org_Unit,NB_Free_Days,Sec_Supervisor_No,Sec_Supervisor_Name,Region_Name,Planned_Released_Date)values('"+Emp_No+"','"+Location+"','"+Cost_Center+"','"+Parent_Cost_Center+"','"+Status+"','"+Onsite+"','"+OnsBil+"','"+Joining_Date+"','"+Billing_Date+"','"+Parent_Org_Unit+"','"+NB_Free_Days+"','"+Sec_Supervisor_No+"','"+Sec_Supervisor_Name+"','"+Region_Name+"','"+Planned_Released_Date+"')");



                System.out.println("Hiiii");


                System.out.println("hi");



                ResultSet rs=s.getResultSet();



                System.out.println(" Hi hello");



                System.out.println("hello");
                s.close();

                con.close();



            }

            catch (Exception err) {

                System.out.println("Error: " +err);
            }




        } //ending of outer while loop 
            //System.out.println(count);
    } //main method ends 
} //readCSV class ends here

Recommended Answers

All 2 Replies

Use code tags. Click this symbol when you write a post: "#"

Just use split. See the API docs for String.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.