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