java code for importing xls data into mysql database [duplicate] - java

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I'm trying to import xls file into mysql but getting an error of Nullpointer EXCEPTION please give me the solution
package abhi;
public class ImportData {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/test","root","abhi1402");
con.setAutoCommit(false);
PreparedStatement pstm = null ;
FileInputStream input = new FileInputStream("D:/New Folder/apnakhata1.xls");
POIFSFileSystem fs = new POIFSFileSystem( input );
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
Row row;
for(int i=1; i<=sheet.getLastRowNum(); i++){
row = sheet.getRow(i);
String CountryName = row.getCell(1).getStringCellValue();
String CountryCode = row.getCell(2).getStringCellValue();
String BankName = row.getCell(3).getStringCellValue();
String Website = row.getCell(4).getStringCellValue();
String Name = row.getCell(5).getStringCellValue();
String SMS = row.getCell(6).getStringCellValue();
int SMSNumber = (int) row.getCell(7).getNumericCellValue();
int CustomerCare = (int) row.getCell(8).getNumericCellValue();
String sql = "INSERT INTO Bank VALUES('"+CountryName+"','"+CountryCode
+"','"+BankName+"','"+Website+"','"+Name+"','"+SMS+"','"+SMSNumber
+"','"+CustomerCare+"')";
pstm = (PreparedStatement) con.prepareStatement(sql);
pstm.execute();
System.out.println("Import rows "+i);
}
con.commit();
pstm.close();
con.close();
input.close();
System.out.println("Success import excel to mysql table");
}catch(ClassNotFoundException e){
System.out.println(e);
}catch(SQLException ex){
System.out.println(ex);
}catch(IOException ioe){
System.out.println(ioe);
}
}
}
}
}

Reason of NullPointerException in your code is due to
for(int i=1; i<=sheet.getLastRowNum(); i++)
Check your Excel file. It may contains some blank rows or may be some blank column values and then run your code.

you have to put
String CountryName = row.getCell(0).getStringCellValue();
String CountryCode = row.getCell(1).getStringCellValue();
String BankName = row.getCell(2).getStringCellValue();
String Website = row.getCell(3).getStringCellValue();
String Name = row.getCell(4).getStringCellValue();

Related

How to store unique data from excel to database(using apache poi)?

How to store unique data from cells(for example phone number and email Id should be unique) in excel to database if there exist multiple duplicate values in the sheet (using apache poi)?
below is the code to read excel data and send it to mysql database
public class ExcelToDatabase {
public static void main(String[] args) throws SQLException, IOException {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbName","root","root");
Statement statement = con.createStatement();
FileInputStream fis = new FileInputStream("/home/ist/Proj/ApachePOI/datafiles/TestData.xlsx");
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet sheet = workbook.getSheetAt(0);
int rows = sheet.getLastRowNum();
for(int r=1; r<=rows; r++) {
XSSFRow row = sheet.getRow(r);
double EmpId = row.getCell(0).getNumericCellValue();
String fName = row.getCell(1).getStringCellValue();
String lName = row.getCell(2).getStringCellValue();
int Age = (int) row.getCell(3).getNumericCellValue();
String Email = row.getCell(4).getStringCellValue();
String Phone = row.getCell(5).getStringCellValue();
String sql = "INSERT INTO table_name values('"+EmpId+"','"+fName+"','"+lName+"', '"+Age+"','"+Email+"', '"+Phone+"')";
statement.execute(sql);
statement.execute("commit");
}
workbook.close();
fis.close();
con.close();
System.out.println("data has been exported to the Database");
}
public static void main(String[] args) throws SQLException, IOException {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbName","root","root");
Statement statement = con.createStatement();
FileInputStream fis = new FileInputStream("/home/ist/Proj/ApachePOI/datafiles/TestData.xlsx");
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet sheet = workbook.getSheetAt(0);
int rows = sheet.getLastRowNum();
}

Exporting from oracle database to csv file

import java.io.*;
import java.sql.*;
public class CsvF {
public static void main(String[] args) {
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn= DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:xe","system","sandp");
conn.setAutoCommit(false);
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery("select * from tet where to_char(S_DATE,'HH24') = '23'");
ResultSetMetaData rsmd = rs.getMetaData();
FileWriter cname = new FileWriter("D:\\asd.csv");
BufferedWriter bwOutFile = new BufferedWriter(cname);
StringBuffer sbOutput = new StringBuffer();
sbOutput.append("S_DATE");
bwOutFile.append(sbOutput);
bwOutFile.append(System.getProperty("line.separator"));
System.out.println("No of columns in the table:"+ rsmd.getColumnCount());
for (int i = 1; i <= rsmd.getColumnCount(); i++)
{
String fname = rsmd.getColumnName(i);
}
System.out.println();
while(rs.next())
{
System.out.print(rs.getString(1));
bwOutFile.append(rs.getString(1));
bwOutFile.append(System.getProperty("line.separator"));
bwOutFile.flush();
System.out.println();
}
conn.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
catch(Exception e)
{
System.out.println("Unable to connect to database" +e);
}
}
}
here i am getting data into asd.csv file as follows=
2015-2-26.23.21. 0. 0
2015-2-26.23.43. 0. 0
2015-2-27.23.28. 0. 0
2015-2-27.23.50. 0. 0
2015-3-1.23.19. 0. 0
2015-3-1.23.41. 0. 0
but i want to get data as following format =
2015-02-26 23:21
2015-02-26 23:43
2015-02-27 23:28
2015-02-27 23:50
2015-03-01 23:19
2015-03-01 23:41
i am getting an extra dot(".") in the output... what i am doing wrong?
i have create a table named tet with coloumn(S_DATE) where S_DATE is timestamp...can anyone help me?
Use rs.getTimestamp() instead of rs.getString() and convert your timestamp to string using SimpleDateFormat.
TimeStamp ts= rs.getTimeStamp(1);
SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd hh:mm");
String yourFormatedDate = formater.format(ts);

i am trying to use two while loop in a single program to iterate two queries but first while loop is not executed

public class JustTest
{
public static void main(String[] args)
{
//Blank workbook
XSSFWorkbook workbook = new XSSFWorkbook();
String url ="";
String username ="";
String password ="";
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection c = DriverManager.getConnection(url,username,password);
c.setAutoCommit(false);
System.out.println("Opened database successfully");
//Create a blank sheet
XSSFSheet sheet = workbook.createSheet("total");
Row headerRow = sheet.createRow(0);
Cell date = headerRow.createCell(0);
date.setCellValue("date");
Cell byDate = headerRow.createCell(1);
byDate.setCellValue("total au");
Cell byDate1 = headerRow.createCell(1);
byDate1.setCellValue("total au_dt");
Statement stmt = c.createStatement();
//passing two queries rs and rs1
ResultSet rs = stmt.executeQuery("select empname,empno from emp");
ResultSet rs1=stmt.executeQuery("select deptno,deptname from dept");
int row = 1;
//first query iterate
while(rs.next()) {
String empname= rs.getString("empname");
int empno = rs.getInt("empno ");
Row dataRow = sheet.createRow(row);
Cell dataNameCell = dataRow.createCell(0);
dataNameCell.setCellValue(dateValue);
Cell dataAddressCell = dataRow.createCell(1);
dataAddressCell.setCellValue(empno );
row = row + 1;
}
//second query iterate
while(rs1.next()) {
String deptno=rs1.getString("deptno");
String deptname =rs1.getString("deptname ");
Row dataRow = sheet.createRow(row1);
Cell dataNameCell1 = dataRow.createCell(2);
dataNameCell1.setCellValue(deptno);
Cell dataNameCell2 = dataRow.createCell(3);
dataNameCell2.setCellValue(deptname );
row1 = row1 + 1;
}
}
catch ( NullPointerException e )
{
System.out.println( " is not updated because cells cannot be left null/empty ");
}
catch ( Exception e )
{
System.err.println( e.getClass().getName()+": "+ e.getMessage() );
System.exit(0);
}
try
{
//Write the workbook in file system
FileOutputStream out = new FileOutputStream(new File("C:\\Users\\jit\\Desktop\\allSheets\\justtest.xlsx"));
workbook.write(out);
out.close();
System.out.println("sheet.xlsx written successfully on disk.");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
java jdbc jdbc-odbc
there are two queries and i am trying to iterate the queries using two while loop ,but for me i am always fetching the data of 2nd loop from db..where as first loop is not executed so data's are not stored for first loop..(storing data in an excel sheet) ..can anyone please edit the code so that data can be stored for both the loops..
Move your second executeQuery to after you process your first while loop. You're probably overwriting
ResultSet rs = stmt.executeQuery("select empname,empno from emp");
int row = 1;
//first query iterate
while(rs.next()) {
String empname= rs.getString("empname");
int empno = rs.getInt("empno ");
Row dataRow = sheet.createRow(row);
Cell dataNameCell = dataRow.createCell(0);
dataNameCell.setCellValue(dateValue);
Cell dataAddressCell = dataRow.createCell(1);
dataAddressCell.setCellValue(empno );
row = row + 1;
}
ResultSet rs1=stmt.executeQuery("select deptno,deptname from dept");

Parsing empty cells in csv file and storing into mysql

I have a csv file which has few cells in some columns empty.Modifying the csv file is not an option as it has around 50k total records.
Whenever I am executing below code it is throwing error as "Incorrect integer value: '' for column 'ParentId' at row 1".My database table has null value as yes for column 'ParentID'.But still it is throwing error.How can I edit this code so that it fills in correct values without giving error?
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Date;
import org.apache.commons.lang.StringUtils;
import au.com.bytecode.opencsv.CSVReader;
public class CSVLoader {
static int count;
private static final
//String SQL_INSERT = "INSERT INTO ${table}(${keys}) VALUES(${values})";
String SQL_INSERT = "INSERT INTO ${table} VALUES(${values})";
private static final String TABLE_REGEX = "\\$\\{table\\}";
//private static final String KEYS_REGEX = "\\$\\{keys\\}";
private static final String VALUES_REGEX = "\\$\\{values\\}";
private Connection connection;
private char seprator;
/**
* Public constructor to build CSVLoader object with
* Connection details. The connection is closed on success
* or failure.
* #param connection
*/
public CSVLoader(Connection connection) {
this.connection = connection;
//Set default separator
this.seprator = ',';
}
/**
* Parse CSV file using OpenCSV library and load in
* given database table.
* #param csvFile Input CSV file
* #param tableName Database table name to import data
* #param truncateBeforeLoad Truncate the table before inserting
* new records.
* #throws Exception
*/
public void loadCSV(String csvFile, String tableName,
boolean truncateBeforeLoad) throws Exception {
CSVReader csvReader = null;
if(null == this.connection) {
throw new Exception("Not a valid connection.");
}
try {
csvReader = new CSVReader(new FileReader(csvFile), this.seprator);
} catch (Exception e) {
e.printStackTrace();
throw new Exception("Error occured while executing file. "
+ e.getMessage());
}
//String[] headerRow = csvReader.readNext();
String[] headerRow = csvReader.readNext();
count++;
if (null == headerRow) {
throw new FileNotFoundException(
"No columns defined in given CSV file." +
"Please check the CSV file format.");
}
/*String questionmarks = StringUtils.repeat("?,", headerRow.length);
System.out.println(headerRow.length);
questionmarks = (String) questionmarks.subSequence(0, questionmarks
.length() - 1);
System.out.println(SQL_INSERT);
String query = SQL_INSERT.replaceFirst(TABLE_REGEX, tableName);
//query = query
// .replaceFirst(KEYS_REGEX, StringUtils.join(headerRow, ","));
query = query.replaceFirst(VALUES_REGEX, questionmarks);
System.out.println("Query: " + query);
*/
//Stirng str1 = "insert into posts values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
String[] nextLine;
Connection con = null;
PreparedStatement ps = null;
try {
con = this.connection;
con.setAutoCommit(false);
ps = con.prepareStatement("insert into posts (Id,PostTypeId,AcceptedAnswerId,ParentId,CreationDate,Score,ViewCount,Body,OwnerUserId,OwnerDisplayName,LastEditorUserId,LastEditorDisplayName,LastEditDate,LastActivityDate,Title,Tags,AnswerCount,CommentCount,FavoriteCount,ClosedDate,CommunityOwnedDate,RowNum) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
if(truncateBeforeLoad) {
//delete data from table before loading csv
con.createStatement().execute("DELETE FROM " + tableName);
}
final int batchSize = 1000;
int count = 0;
Date date = null;
while ((nextLine = csvReader.readNext()) != null) {
if (null != nextLine) {
int index = 1;
for (String string : nextLine) {
date = DateUtil.convertToDate(string);
if (null != date) {
ps.setDate(index++, new java.sql.Date(date
.getTime()));
} else {
ps.setString(index++, string);
}
}
System.out.println(count);
ps.addBatch();
System.out.println(count);
}
if (++count % batchSize == 0) {
System.out.println(count);
ps.executeBatch();
}
}
ps.executeBatch(); // insert remaining records
con.commit();
} catch (Exception e) {
con.rollback();
e.printStackTrace();
throw new Exception(
"Error occured while loading data from file to database."
+ e.getMessage());
} finally {
if (null != ps)
ps.close();
if (null != con)
con.close();
csvReader.close();
}
}
public char getSeprator() {
return seprator;
}
public void setSeprator(char seprator) {
this.seprator = seprator;
}
}
It might be the case where int column is actually having blank spaces, in such case you can place manual check as below and still go ahead. Hope this answers your doubt.
// check if value is null or blank spaces
if(certain_value== null || certain.value.trim().length==0){
ps.setNull(4, java.sql.Types.INTEGER); // This will set null value for int type
}

how to store data from excel sheet into mysql database using jdbc connection

I have written the code for access data from Excel sheet but
i am not able to store into database using JDBC .
my code for access data from excel sheet
try{
List list=new ArrayList();
String fileName="d:\\menu.xls";
File file=new File(fileName);
InputStream input = new BufferedInputStream(new FileInputStream(file));
POIFSFileSystem fs = new POIFSFileSystem( input );
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
//int i=0;
Iterator rows=sheet.rowIterator();
while(rows.hasNext()){
HSSFRow row=(HSSFRow)rows.next();
System.out.println("\n");
Iterator cells=row.cellIterator();
while( cells.hasNext() ) {
HSSFCell cell = (HSSFCell) cells.next();
if(HSSFCell.CELL_TYPE_NUMERIC==cell.getCellType()){
System.out.print(cell.getNumericCellValue()+" " );
// list.add(cell.getNumericCellValue());
}
else if (HSSFCell.CELL_TYPE_STRING==cell.getCellType()){
System.out.print(cell.getStringCellValue()+" " );
//list.add(cell.getStringCellValue());
}
else
if (HSSFCell.CELL_TYPE_BOOLEAN==cell.getCellType()){
System.out.print(cell.getBooleanCellValue()+" " );
//list.add(cell.getBooleanCellValue());
}
else
if(HSSFCell.CELL_TYPE_BLANK==cell.getCellType()){
System.out.print( "BLANK " );}
else
System.out.print("Unknown cell type");
}
}
System.out.println(list);
}catch(Exception e){
e.printStackTrace();
}
I got all the data into myeclipse console bt i have no idea how to store in DB
please tell me how to store data into mysql database using JDBC
Thanx in advance
As you have an ArrayList in which you're adding cell values, you can proceed as follows:
public void insertRowInDB(List cellValues){
Connection con = null;
PreparedStatement preparedStatement = null;
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/databaseName?"
+ "user=sqlUser&password=sqlPassword");
String sql = "INSERT INTO tableName(field1, field2, ..., fieldN) VALUES (?,?,...,?)";
preparedStatement = con.prepareStatement(sql);
int paramIndex = 1;
for(Object cell : cellValues){
preparedStatement.setObject(paramIndex, cell);
paramIndex++;
}
int status = preparedStatement.executeUpdate();
//DO something with the status if needed
} catch(SQLException ex) {
/* log the exception */
} finally {
try{
preparedStatemnt.close();
con.close();
} catch(SQLException ignored) {}
}
}
You'll need make this little change:
while(rows.hasNext()){
List list = new ArrayList(); // initialize the list here
HSSFRow row=(HSSFRow)rows.next();
System.out.println("\n");
Iterator cells=row.cellIterator();
while( cells.hasNext() ) {
/* all your code seems fine here, just uncomment list.add(something) lines */
}
insertRowInDB(list); // insert the cells in your database
}
I also suggest you check out this MySQL and Java JDBC - Tutorial

Categories

Resources