lundi 13 juin 2016

How to save the excel file data into mysql database table as a record by record using apache POI

I am not able to store excel file information into a database table. Can any one help me how to save a file data into a table which have different datatypes like bigint, varchar and date.Here I am providing my programe on saving excel file into database record by record?

import java.io.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.util.*;
import java.sql.*; 

public class XLToDB {  

    public static final String INSERT_RECORDS = "INSERT INTO RECORDS" + "(SNO, ID, NAME, VALUE1, VALUE2, VALUE3, VALUE4, VALUE5) VALUES" + "(?,?,?,?,?,?,?,?)";
    private static String GET_COUNT = "SELECT COUNT(*) FROM RECORDS";

    public static void main(String[] args) throws Exception{
        XLToDB obj = new XLToDB();
        obj.insertRecords("E:/test.xlsx");



    }
          public void insertRecords(String filePath){

            /* Create Connection objects */
        Connection con = null;
        PreparedStatement prepStmt = null;
        java.sql.Statement stmt = null;
        int count = 0;
        ArrayList<String> mylist = new ArrayList<String>();

        try{
            con = DBHelper.getConnection();
            System.out.println("Connection :: ["+con+"]");
            prepStmt = con.prepareStatement(INSERT_RECORDS);
            stmt = con.createStatement();
            ResultSet result = stmt.executeQuery(GET_COUNT);
            while(result.next()) {

                int val = result.getInt(1);
                System.out.println(val);
                count = val+1;

            }


            prepStmt.setInt(1,count);

            /* We should now load excel objects and loop through the worksheet data */
            FileInputStream fis = new FileInputStream(new File(filePath));
             /* Load workbook */
            XSSFWorkbook workbook = new XSSFWorkbook (fis);
            /* Load worksheet */
            XSSFSheet sheet = workbook.getSheetAt(0);
               // we loop through and insert data
            Iterator ite = sheet.rowIterator();

               while(ite.hasNext()) {
                        Row row = (Row) ite.next(); 
                        Iterator<Cell> cellIterator = row.cellIterator();
                                while(cellIterator.hasNext()) {
                                        Cell cell = cellIterator.next();
                                      switch(cell.getCellType()) { 
                                        case Cell.CELL_TYPE_STRING: //handle string columns
                                                prepStmt.setString(1, cell.getStringCellValue());                                                                                     
                                                break;
                                        case Cell.CELL_TYPE_NUMERIC: //handle double data
                                                prepStmt.setDouble(2,cell.getNumericCellValue() );
                                                break;
                                        }                                       
                                }
                //we can execute the statement before reading the next row
                prepStmt.executeUpdate();
                }
               /* Close input stream */
               fis.close();
               /* Close prepared statement */
               prepStmt.close();
               /* COMMIT transaction */
               con.commit();
               /* Close connection */
               con.close();

        }catch(Exception e){
            e.printStackTrace();            
        }

        }
}

Aucun commentaire:

Enregistrer un commentaire