0

我正在尝试读取一个 excel 文件,然后将 excel 的内容写入数据库。我能够阅读 excel,如果我打印,它会显示在输出下方。我想知道如何将这些数据分离或拆分到数据库中的列并将数据存储在数据库中。如果我分成列,我在 DB 中有一个表可以保存以下数据。


//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(); } } }
4

1 回答 1

1

如果您不知道将向语句发送多少个参数,您可以创建一个方法来处理它。我会给你一个非常基本的例子,记住这种方法可以大大改进。

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);
}

该类Vector实现了List接口,因此您可以Vector通过这些方法传递您的。

作为旁注,您不应使用 a Vector,而应使用 aList<Object>实现的 a ArrayList<Object>。更多信息:为什么 Java Vector 类被认为已过时或不推荐使用?

于 2012-10-14T22:09:37.510 回答