Dumping bulk data from csv file into MS-Access DB.

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2009
Posts: 2
Reputation: AbiSuren is an unknown quantity at this point 
Solved Threads: 0
AbiSuren AbiSuren is offline Offline
Newbie Poster

Dumping bulk data from csv file into MS-Access DB.

 
0
  #1
Sep 16th, 2009
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.


[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 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



[icode]
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,692
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 227
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Dumping bulk data from csv file into MS-Access DB.

 
0
  #2
Sep 16th, 2009
Use code tags. Click this symbol when you write a post: "#"
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,445
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 261
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Dumping bulk data from csv file into MS-Access DB.

 
0
  #3
Sep 16th, 2009
Just use split. See the API docs for String.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC