Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners retrieve multiple columns values from multiple csv files in java
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


lata
retrieve multiple columns values from multiple csv files in java
1 Answer(s)      a year and 11 months ago
Posted in : Java Beginners


Suppose there is folder in C:\fileupload which contains these two csv files:

file1.csv has 3 columns(VendorID,Name)

file2.csv has 2 columns(VendorID,address)///// here VendorID is corresponding to file1.csv

i want to retrive data from these two files and store in oracle database as:

VendorID,Name,Address

and plz be specific in telling me through java only Any help will be much appreciated.

Thanks


Advertisement
View Answers

November 22, 2012 at 12:41 PM


Here is a code that retrieves the column values from csv file and save it into database.

import java.io.*;
import java.sql.*;
import java.util.*;
import jxl.*;
import jxl.read.biff.BiffException;

class  StoreCSVDataToDatabase
{
    public static void main(String[] args) throws Exception
    {

ArrayList<String> list1=new ArrayList<String>();
ArrayList<String> list2=new ArrayList<String>();
ArrayList<String> list3=new ArrayList<String>();

    Cell rowData[] = null;
    int rowCount = 0;
    int columnCount = 0;
    int totalSheet = 0;

       WorkbookSettings ws = new WorkbookSettings();
        ws.setLocale(new Locale("en", "EN"));
       Workbook workbook = Workbook.getWorkbook(new File("C:/data.csv"), ws);

        Sheet s = workbook.getSheet(0);
        rowCount = s.getRows();
        columnCount = s.getColumns();
        for(int i = 1; i < rowCount; i++){
            rowData = s.getRow(i);
            if(rowData[0].getContents().length() != 0){ 
                for(int j = 0; j < columnCount ;j++){
                 switch(j){
                    case 0:
                        System.out.println("ID: "+rowData[j].getContents());
                        list1.add(rowData[j].getContents());
                        break;
                    case 1:
                        System.out.println("Name: "+rowData[j].getContents());
                        list2.add(rowData[j].getContents());
                        break;
                    case 2:
                        System.out.println("Address: "+rowData[j].getContents());
                        list3.add(rowData[j].getContents());
                        break;
                       }
                    }
                  }
        }
    workbook.close(); 
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/roseindia","root", "root");
Statement st=conn.createStatement();
for(int i=0;i<list1.size();i++){
    String id=list1.get(i).toString();
    String name=list2.get(i).toString();
    String address=list3.get(i).toString();
    st.executeUpdate("insert into data(id,name,address) values('"+id+"','"+name+"','"+address+"')");
}
System.out.println("Data is successfully inserted into the database");

    }
}

For the above code, you need jxl.jar and put it into the lib of jdk and apache tomcat.



Related Tutorials/Questions & Answers:
retrieve multiple columns values from multiple csv files in java
retrieve multiple columns values from multiple csv files in java  Suppose there is folder in C:\fileupload which contains these two csv files: file1.csv has 3 columns(VendorID,Name) file2.csv has 2 columns(VendorID,address
how to sort multiple key columns in CSV file - Java Beginners
how to sort multiple key columns in CSV file  Hi Any one can assist how to sort the multiple key columns in CSV file? Any example will be appreciated. Thanks
Advertisements
create csv file in php in multiple columns
create csv file in php in multiple columns  i need create csv file in multiple columns like this: id name age date_added 1 john 23 2012-2-12 2 jane 25 2012-3-14 i can build csv file like this: id,name,age,date
create csv file in php in multiple columns
create csv file in php in multiple columns  i need create csv file in multiple columns like this: id name age date_added 1 john 23 2012-2-12 2 jane 25 2012-3-14 i can build csv file like this: id,name,age,date
retrieve multiple attribute values
retrieve multiple attribute values  Hello All Am a beginner. Would... like is to retrieve both the attribute values of process:Output during execution time, store them so that it can be retrieved for later use using Java. Thanks
retrieve multiple attribute values
XML retrieve multiple attribute values  Hello All Am a beginner...; What I would like is to retrieve both the attribute values of process... using Java. Thanks in advance
retrieve multiple attribute values
retrieve multiple attribute values  Hello All Am a beginner. Would... like is to retrieve both the attribute values of process:Output during execution time, store them so that it can be retrieved for later use using Java. Thanks
how to read the values for text and csv files and store those values into database in multiple rows..means one value for one row
how to read the values for text and csv files and store those values into database in multiple rows..means one value for one row  Hai, I need... or .txt) file and get the values from that file and store them into database
java program to read multiple files from a directory and display them on a jframe
java program to read multiple files from a directory and display them on a jframe  hi guys i want to write a java program that will read all files in a directory and display them one by on on a jframe on jpanel with specific key
Retrieve multiple data from database into Table.....
Retrieve multiple data from database into Table.....   hi........... I want to Retrieve multiple data from database into Table but i am not able to.... i am only able to retrieve one value not more than that. means in my
How to return multiple values from a method - Java Beginners
How to return multiple values from a method  I came the following method "How do I return multiple values from a method?" posted elsewhere..."; /** * This is returning a single Object, which contains * multiple values
How to return multiple values from a method - Java Beginners
How to return multiple values from a method  I came the following method "How do I return multiple values from a method?" posted elsewhere..."; /** * This is returning a single Object, which contains * multiple values
How to pass multiple values from a servlet to JSP?
How to pass multiple values from a servlet to JSP?  hi, I want to pass multiple values form a servlet to JSP. Hw do i do that? I am able to send one value at a time without any prb, but I am unable to carry multiple (from two
How to carry multiple values from a Servlet to a JSP?
How to carry multiple values from a Servlet to a JSP?  By using...). ----response.sendRedirect("index.jsp?uname="+username);---- But I want multiple values needs to be carried from my servlet to a JSP. How do I do
Upload and Download multiple files
Upload and Download multiple files  Hello Sir/Madam, I need a simple code for upload and download multiple files(it may be image,doc and txt)which has to be store in database and also retrieve the file from database
select tag multiple values
select tag multiple values   I want to insert multiple values in database which i have selected from select tag
Mysql Alter Add Multiple Columns
The Tutorial illustrate an example from 'Mysql Alter Add Multiple Columns... Mysql Alter Add Multiple Columns       Mysql Alter Add Multiple Columns is used to modify
Reading multiple xml files
Reading multiple xml files  How can we read many xml files from a folder? The same procedure as that of "listfiles
how to store multiple values from drop down in database where i am using java struts 1.3
how to store multiple values from drop down in database where i am using java... is displaying.. i need code in java so that it takes multiple values... where i have to select multiple keyskills. but it is taking only one valuee.. i
Java read multiple files
Java read multiple files In this section you will learn how to read the data of multiple files. Here we have created four files that includes information.... We have to find the students according to particular school from all the files
How to convert multiple files in java to .zip format
How to convert multiple files in java to .zip format  i receive multiple files from a remote URL as for (String reportid : reportList...); } i want to convert the data from inputStream into Zip Stream and so
Mysql Alter Multiple Columns
The Tutorial illustrate an example from 'Mysql Alter Multiple Columns'. To grasp... Mysql Alter Multiple Columns       Mysql Alter Multiple Columns is used to modify the table
multiple select values
multiple select values   can you provide an example for multiple select values for html:select tag
find jar file name from multiple set of jar files
find jar file name from multiple set of jar files   I would like to find jar file name from multiple set of jar files where the given class name√Ę‚?¨¬?com.rational.ft.util.debug√Ę‚?¨¬
Renaming multiple files
Renaming multiple files  Hi, I have number of files in a directory. Files are named as: 1DeoyaRohit0705$.mp4,2DeoyaRohit0705$.mp4, 3_DeoyaRohit0705$.mp4. .... Now I wanted to rename these files as: 1RohitBBorse.mp4
Reading and writting multiple files
Reading and writting multiple files  how can i read and write say two different files at the same time using threads
reading multiple files from a directory and writing them into a single file
reading multiple files from a directory and writing them into a single file... this: /*this program reads multiple files * from a single directory. */ package elite.tech.com...://www.roseindia.net/tutorial/java/core/files/fileconcatenation.html   
retrieve multiple valuesfrom a single field
retrieve multiple valuesfrom a single field  hi i am doing...,s.perumbudur,poonamalle in a single record. i want to take the values from the stages field... take one record the values are source is chennai, destination is kanchipuram
retrieve multiple valuesfrom a single field
retrieve multiple valuesfrom a single field  hi i am doing...,s.perumbudur,poonamalle in a single record. i want to take the values from the stages field... take one record the values are source is chennai, destination is kanchipuram
retrieve multiple valuesfrom a single field
retrieve multiple valuesfrom a single field  hi i am doing...,s.perumbudur,poonamalle in a single record. i want to take the values from the stages field... take one record the values are source is chennai, destination is kanchipuram
retrieve multiple valuesfrom a single field
retrieve multiple valuesfrom a single field  hi i am doing...,s.perumbudur,poonamalle in a single record. i want to take the values from the stages field... take one record the values are source is chennai, destination is kanchipuram
 

 

 

DMCA.com