importing CSV data into Database in java when the no of columns are known using javacsv
I have imported CSV data into postgres Database in java using javaCSV jar. But now i have to convert it in such way that if the no of columns in the csv are not known then also i should be able to import it. But the condition i can use only javaCSv jar. No other jar is allowed. And also the performance should not degrade if i insert 100000 records.
Here is my code-
DataImport.java
package programs;
public class DataImport {
public static void main(String[] args) {
CSVLoader csvLoader=new CSVLoader();
String fileName="D:/File1.csv";
csvLoader.loadCSV(fileName);
}
}
CSVLoader.java
package programs;
import java.io.FileNotFoundException;
import java.io.FileReader;
public class CSVLoader {
static CSVToDB cd = new CSVToDB();
public CSVLoader(char seprator) {
super();
this.seprator = seprator;
}
public void loadCSV(String fileName) {
CsvReader products = null;
try {
products = new CsvReader(fileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
cd.retrieveData(products);
products.close();
}
}
CSVTotDB.java
import java.io.FileInputStream;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
//import java.util.Properties;
import com.csvreader.CsvReader;
public class CSVToDB {
void retrieveData(CsvReader products) {
Connection c = null;
PreparedStatement stmt = null;
try {
c = ConnectionManager.getConnection();
System.out.println(products.readHeaders());
System.out.println(products.getHeaderCount());
String a;
stmt = c.prepareStatement("insert into csvtodb values(?,?,?)");
final int batchSize = 1000;
int count = 0;
while (products.readRecord()) {
stmt.setString(1, products.get("Name"));
stmt.setString(2, products.get("Address"));
stmt.setString(3, products.get("City"));
stmt.addBatch();
if (++count % batchSize == 0) {
stmt.executeBatch();
}
}
stmt.executeBatch();
} catch (Exception e) {
System.err.println(e.getClass().getName() + " : " + e.getMessage());
System.exit(0);
} finally {
try {
products.close();
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
c.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("Records created successfully");
}
}
ConnectionManager.java
package programs;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Properties;
public class ConnectionManager {
private static Connection c = null;
PreparedStatement stmt = null;
private static String Driver = null;
private static String url = null;
private static String UserName = null;
private static String Password = null;
public static Connection getConnection() {
Properties props = new Properties();
try {
props.load(new FileInputStream("src/Demo.properties"));
Driver = props.getProperty("Driver");
url = props.getProperty("connectionURL");
UserName = props.getProperty("UserName");
Password = props.getProperty("Password");
Class.forName(Driver);
c = DriverManager.getConnection(url, UserName, Password);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException ee) {
// TODO Auto-generated catch block
ee.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return c;
}
}
Demo.properties
Driver=org.postgresql.Driver
connectionURL=jdbc:postgresql://localhost:5432/Test
UserName=postgres
Password=p@ssw0rd
Please reply as early as possible
View Answers
Related Tutorials/Questions & Answers:
Advertisements
importing data and edit that data importing data and edit that
data hello sir i have
data in excel... that
data like adding new
columns and new rows and creating new tables with that
data in the same
database Exporting data from mysql into csv using jspExporting
data from mysql into
csv using jsp Hi friends.... I want to export the
data from mysql to
csv file
using... i am having 30
columns in my
database.. Eg- text1,text2,text3,....,upto text30... i want to export this
data code for selected checkbox columns data from databasecode for selected checkbox
columns data from database in my page iam getting all column names of a particular table with checkboxes.
when iam select one or more checkboxes i have to get
data of that selected
columns only.
what
Importing in java . . . . Importing in
java . . . . We all know that
when we are
importing a class of a package in another package , the methods which are public are only available and we can use it . But suppose the method has default access specifier
Export data into CSV File using Servlet Export
data into
CSV File
using Servlet
... to Export
data
into
CSV file
using Servlet. We have created file "JdbcCsvFile.java" to export
data from this .
java file..
Brief
How to save form data to a csv file using jquery or ajaxHow to save form
data to a
csv file
using jquery or ajax Please let...=data.responseText;
Now the problem is ,i should write form
data to a
csv file
using ajax... this.
i am able to read the
csv file
using this code
if (window.XMLHttpRequest
retrieve data from database using jsf retrieve
data from
database using jsf Hello
I want an example of source code to retrieve
data from
database
i have a
database (oracle) name as db1 it's contain table 'author' composed of three
columns idauthor(primary key
Export Database table to CSV FileExport
Database table to
CSV File
In this tutorial, you will learn how to retrieve
data from
database and save
it to
CSV File.
The Comma-separated values, better knows as
CSV is having a delimited
data
format. It has field/
columns insert data in the database using checkboxinsert
data in the
database using checkbox i am fetching
data from the
database using servlet on the jsp page and there is checkbox corresponding... should i insert only checked
data into
database on submission.
We
create csv file in php in multiple columnscreate
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 columnscreate
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
Err while reading CSV file using POI - Java BeginnersErr while reading
CSV file
using POI Hi,
When i am trying to read a
CSV file
using POI Dile system, the below error is coming up... know, why this is coming up. Is there any other way to read the
CSV file.
Thanks
how to insert data into database using jsp & retrivehow to insert
data into
database using jsp & retrive Hello,
I have created 1 html page which contain username, password & submit button. in my oracle10G
database already contain table name admin which has name, password
Importing in eclipse - Java BeginnersImporting in eclipse I'm
using eclipse for designing struts application...
I'm manually copying the tld files and creating struts-config.xml file for my project...
Is there the best way to import the tld files
or to add
how to insert data in database using html+jsphow to insert
data in
database using html+jsp anyone know what... and
database name. Here machine name id localhost and
database name...";
// declare a connection by
using Connection interface
Populate a combo box using data from a databasePopulate a combo box
using data from a database Hi Guys, In need... to populate the first one, and then how to send the
data off to a servlet and preform a search on the
database for the values I want,
but then how do I pass that back
Exporting data from mysql to csv fileExporting
data from mysql to
csv file Hi friends....
I want to export the
data from mysql to
csv file... i am having 30
columns in my
database.. Eg... example that retrieves the
data from the
database and save it into
csv file
retrive data from database using jsp in struts? retrive
data from
database using jsp in struts? *search.jsp*
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>