简体   繁体   中英

writing excel code to database

I am trying to read an excel file and then write the contents of the excel to database. I am able to read the excel and it display below output if I print. I would like to know how to separate or split this data to columns in Database and store the data in database. I have a table in DB that can hold the below data if I seperate into columns.


//CODE OUTPUT
1.0     Henry       Rey     Staff       IT      Software Engg       New Hire        TigoAdmin       11-Oct-2012     11-Oct-2012     555.0
2.0 Nick Murry Staff IT Administrator New Hire TigoAdmin 11-Oct-2012 11-Oct-2012 555.0

// BELOW is the CODE

package com.project.bulk; import java.io.FileInputStream; import java.util.Iterator; import java.util.Vector; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.POIFSFileSystem; public class ReadExcelFile { public static void main(String[] args) { String fileName = "C:\\excelFile.xls"; Vector dataHolder = ReadCSV(fileName); printCellDataToConsole(dataHolder); } public static Vector ReadCSV(String fileName) { Vector cellVectorHolder = new Vector(); try { FileInputStream myInput = new FileInputStream(fileName); POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput); HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem); HSSFSheet mySheet = myWorkBook.getSheetAt(0); Iterator rowIter = mySheet.rowIterator(); while (rowIter.hasNext()) { HSSFRow myRow = (HSSFRow) rowIter.next(); Iterator cellIter = myRow.cellIterator(); Vector cellStoreVector = new Vector(); while (cellIter.hasNext()) { HSSFCell myCell = (HSSFCell) cellIter.next(); cellStoreVector.addElement(myCell); } cellVectorHolder.addElement(cellStoreVector); } } catch (Exception e) { e.printStackTrace(); } return cellVectorHolder; } private static void printCellDataToConsole(Vector dataHolder) { for (int i = 0; i < dataHolder.size(); i++) { Vector cellStoreVector = (Vector) dataHolder.elementAt(i); for (int j = 0; j < cellStoreVector.size(); j++) { HSSFCell myCell = (HSSFCell) cellStoreVector.elementAt(j); String stringCellValue = myCell.toString(); System.out.print(stringCellValue + "\t\t"); } System.out.println(); } } }

If you don't know how many arguments you will send to the statement, you can create a method to handle it. I'll give you a very basic example, have in mind that this approach can be heavily improved.

public void executeSQLUpdate(String sql, List<Object> arguments) {
    Connection con = null;
    PreparedStatement pstmt = null;
    try {
        con = getConnection(); //a method that returns a java.sql.Connection to your database
        pstmt = con.prepareStatement(sql);
        if (arguments != null) {
            int i = 1;
            for(Object o : arguments) {
                pstmt.setObject(i++, o);
            }
        }
        //method to execute insert, update, delete statements...
        pstmt.executeUpdate();
    } catch(SQLException e) {
        //handle the error...
    } finally {
        //closing the resources (always in finally block, not in the try!)
        try {
            if (pstmt != null) {
                pstmt.close();
            }
            if (con != null) {
                con.close();
            }
        } catch (SQLException e) {
        }
    }
}

//calling the method to execute a sql insert statement
public void yourMethodToData(List<Object> arguments) {
    String sql = "INSERT INTO YOUR TABLE VALUES(?)";
    executeSQLUpdate(sql, arguments);
}

The Vector class implements the List interface, so you can pass your Vector through these methods.

As a side note, you should not use a Vector , instead use a List<Object> implemented with an ArrayList<Object> . More info: Why is Java Vector class considered obsolete or deprecated?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM