我正在尝试读取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();
}
}
}
答案 0 :(得分: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
。
作为旁注,不应使用Vector
,而是使用List<Object>
实施的ArrayList<Object>
。更多信息:Why is Java Vector class considered obsolete or deprecated?