java - 将excel代码写入数据库

标签 java servlets

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


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

<p>// BELOW is the CODE</p>

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

最佳答案

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

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接口(interface),这样你就可以传递你的Vector通过这些方法。

顺便说一句,您不应该使用Vector ,而是使用 List<Object>使用 ArrayList<Object> 实现。更多信息:Why is Java Vector class considered obsolete or deprecated?

关于java - 将excel代码写入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12886678/

相关文章:

java - String#equals 和 String#contentEquals 方法的区别

java.lang.VerifyError : Expecting a stackmap frame

java - 按位或未产生预期结果

java - Objects.requireNonNullElse() 和 Optional.ofNullable().orElse() 有什么区别?

java - tomcat 服务器上的 Intellij Web 应用程序显示 http ://localhost:8080/index. jsp 而不是 http ://localhost:8080/myapp/index. jsp

java - 未找到类异常,但它位于同一个包中

java - 如何根据事件更新多个 session

java - Java Servlet 中的异步行为?

java - servlet jsp 的严重 : Servlet. service() 抛出异常 java.lang.LinkageError:加载器约束冲突

Java-Fake 系统时钟启动