| | |
Dumping bulk data from csv file into MS-Access DB.
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2009
Posts: 2
Reputation:
Solved Threads: 0
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]
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]
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
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
----------------------------------------------
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
![]() |
Similar Threads
- How to download large data in csv file in jsp (JSP)
- Import and export mixed data from and to csv file in VBA6 (Visual Basic 4 / 5 / 6)
- reading, modifying data within .csv file (C++)
- Problem in importing data from text file to MS-Access file (VB.NET)
- Populating a MySQL table with data from a csv file (PHP)
- How to read data from csv file in an array and parse (C++)
Other Threads in the Java Forum
- Previous Thread: rich:extendedDataTable subtotal the amount
- Next Thread: Switching between Multiple Frames
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary bluetooth character chat class classes client code component consumer csv database desktop draw eclipse error event exception fractal ftp game givemetehcodez graphics gui html ide image input integer j2me japplet java javaarraylist javaee javaprojects jmf jni jpanel julia linked linux list loop mac map method methods mobile netbeans newbie objects online oracle oriented panel print printf problem program programming project projects properties recursion replaydirector reporting researchinmotion robot rotatetext rsa scanner screen se server set size sms sort sql string swing template test threads time tree ubuntu windows working






