Thursday, 17 April 2014

How to read the excel sheet data to Databse

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;



import org.apache.commons.io.FilenameUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


public class SampleExcel {

   
    public static void main(String...ar) {
       
        List<SampleBO> list=new ArrayList<SampleBO>();
        try {
            
            File fileName=new File("d:\\employeeInfo123.xls");
            FileInputStream file = new FileInputStream(fileName);
            
            System.out.println("path"+fileName.getPath());
            String fileExt=FilenameUtils.getExtension(fileName.getPath());
           
            System.out.println(fileExt);
           
            if(fileExt.equals("xls")){
                System.out.println("Excel 97-2003 format");
              
                HSSFWorkbook workbook = new HSSFWorkbook(file);
           
              
                HSSFSheet sheet = workbook.getSheetAt(0);
                
             
                Iterator<Row> rowIterator = sheet.iterator();
               
                while(rowIterator.hasNext()) {
                 
                    Row row = rowIterator.next();
                    
                    Iterator<Cell> cellIterator = row.cellIterator();
                   
                    SampleBO bo=new SampleBO();
                   
                    int columnCount=1;
                  
                    String columns="";
                   
                    while(cellIterator.hasNext()) {
                     
                        Cell cell = cellIterator.next();
                       
                        if(columnCount==1){
                   
                            bo.setEid(cell.getNumericCellValue());
                       
                        }
                       
                        if(columnCount==2){
                       
                            bo.setFname(cell.getStringCellValue());
                       
                        }
                       
                        if(columnCount==3){
                       
                            bo.setMname(cell.getStringCellValue());
                       
                        }
                       
                        if(columnCount==4){
                       
                            bo.setLname(cell.getStringCellValue());
                       
                       
                        }
                       
                       
                        columnCount++;
                    
                    }
                   
                    list.add(bo);
                   
                    System.out.println("");
                }
               
               
               
                for(SampleBO bean:list)
                {
                    System.out.println(" Name is: "+bean.getEid());
                    System.out.println(" Name is: "+bean.getFname());
                    System.out.println(" Name is: "+bean.getMname());
                    System.out.println(" Name is: "+bean.getLname());
                }
                System.out.println("list size\t"+list.size());
                file.close();
                FileOutputStream out = new FileOutputStream(new File("d:\\employeeInfoOutput.xls"));
                workbook.write(out);
                out.close();
            }
            else{
                System.out.println("Excel 2007+ format");
             
                XSSFWorkbook workbook = new XSSFWorkbook(file);
           
               
                XSSFSheet sheet = workbook.getSheetAt(0);
                
               
                Iterator<Row> rowIterator = sheet.iterator();
                while(rowIterator.hasNext()) {
                 
                    Row row = rowIterator.next();
                    
                   
                    Iterator<Cell> cellIterator = row.cellIterator();
                   
                    while(cellIterator.hasNext()) {
                        
                        Cell cell = cellIterator.next();
                        
                      switch(cell.getCellType()) {
                            case Cell.CELL_TYPE_BOOLEAN:
                                System.out.print(cell.getBooleanCellValue() + "\t\t");
                                break;
                            case Cell.CELL_TYPE_NUMERIC:
                                System.out.print(cell.getNumericCellValue() + "\t\t");
                                break;
                            case Cell.CELL_TYPE_STRING:
                                System.out.print(cell.getStringCellValue() + "\t\t");
                                break;
                        }
                      
                    }
                    System.out.println("");
                }
                file.close();
                FileOutputStream out = new FileOutputStream(new File("d:\\employeeInfoOutput.xlsx"));
                workbook.write(out);
                out.close();
            }
           

            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
       
       SampleDAO dd = new SampleDAO();
       dd.insertData(list);

    }

}






SampleBO.java



public class SampleBO {

    private double eid;
    private String fname,mname,lname;
   
   
    public  SampleBO(){
       
        this.eid =eid;
        this.fname =fname;
        this.mname = mname;
        this.lname = lname;
       
       
       
       
    }
   
    public double getEid() {
        return eid;
    }
    public void setEid(double eid) {
        this.eid = eid;
    }
    public String getFname() {
        return fname;
    }
    public void setFname(String fname) {
        this.fname = fname;
    }
    public String getMname() {
        return mname;
    }
    public void setMname(String mname) {
        this.mname = mname;
    }
    public String getLname() {
        return lname;
    }
    public void setLname(String lname) {
        this.lname = lname;
    }
   
   

}
SampleDAO.java


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;


public class SampleDAO {

   
    public String insertData(List<SampleBO> ss){
        String result ="Error".toLowerCase();
       
        System.out.println("Enter the SampleDAO#############");
        Connection con=getConnections();
        Statement st = null;
        for(SampleBO s : ss){
           
            String sql ="INSERT INTO `sample` (`eid`, `fname`, `mname`, `lname`) VALUES ("+s.getEid()+", '"+s.getFname()+"', '"+s.getMname()+"', '"+s.getLname()+"')";
           
           
            System.out.println("Insert query is :"+sql);
            try {
                st=con.createStatement();
                st.executeUpdate(sql);
               
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                if(!e.getMessage().contains("eidAlreadyExisted"))
                {
                    e.printStackTrace();   
                }
               
            }
           
           
        }
       
       
        return result;
       
       
       
    }
   
   
   
   
    public Connection getConnections()
    {
        System.out.println("-------- MySQL JDBC Connection Testing ------------");
       
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println("Where is your MySQL JDBC Driver?");
            e.printStackTrace();
           
        }
   
        System.out.println("MySQL JDBC Driver Registered!");
        Connection connection = null;
   
        try {
            connection = DriverManager
            .getConnection("jdbc:mysql://localhost:3306/test","root", "root");
           
          
        } catch (SQLException e) {
            System.out.println("Connection Failed! Check output console");
            e.printStackTrace();
       
        }
   
        if (connection != null) {
            System.out.println("Data base is connected");
        } else {
            System.out.println("Failed to make connection!");
        }
       
        return connection;
    }
   
    /*public static void main(String[] argv) {
       
        System.out.println("-------- MySQL JDBC Connection Testing ------------");
   
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println("Where is your MySQL JDBC Driver?");
            e.printStackTrace();
            return;
        }
   
        System.out.println("MySQL JDBC Driver Registered!");
        Connection connection = null;
   
        try {
            connection = DriverManager
            .getConnection("jdbc:mysql://localhost:3306/test","root", "root");
           
          
        } catch (SQLException e) {
            System.out.println("Connection Failed! Check output console");
            e.printStackTrace();
            return;
        }
   
        if (connection != null) {
            System.out.println("Data base is connected");
        } else {
            System.out.println("Failed to make connection!");
        }
      }
*/
}


Share This Post →

No comments:

Post a Comment

Powered By Blogger |   Design By Srikanth Ganji
Join Our Community
×
blogger tipsblogger templatesFOLLOW