Hi,

The following code works well but the problem is it does noy reads the null values.For Example if csv file contains" Surendar,Java,maths,science,,history,english "

Here the program reads the first four strings properly and for fifth string instead of reading null value it reads the next string.

Please Correct the same code.

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 Previous_Report;
            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 = "Book2.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();
                        Previous_Report=field.substring(0,n); 
                    }   

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

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

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

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

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

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

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



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



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

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

                    if(count==20){
                        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,Previous_Report,Onsite,OnsBil,Account,Joining_Date,Billing_Date,Parent_Org_Unit ,NB_Free_Days,Sec_Supervisor_Name,Region_Name,Planned_Released_Date) values('"+Emp_No+"','"+Location+"','"+Cost_Center+"','"+Parent_Cost_Center+"','"+Status+"','"+Previous_Report+"','"+Onsite+"','"+OnsBil+"','"+Account+"','"+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

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Please put your code in between a code wrapper.
so we can help you more and not struggle while we try to read your code.

Thank you =)!

This article has been dead for over six months. Start a new discussion instead.