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)///// 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
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.
Ads
Related Tutorials/Questions & Answers:
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
Advertisements
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
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 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 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
select tag multiple values
select tag
multiple values I want to insert
multiple values in database which i have selected
from select tag
multiple select values
multiple select
values can you provide an example for
multiple select
values for html:select tag
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
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
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
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
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
Java Method Return Multiple Values
Java Method Return
Multiple Values
In this section we will learn about how a method can return
multiple values
in
Java.
This example explains you how... the steps required in to return
multiple values in
Java. In this example you will see