使用JSP从MS ACCESS数据库中的excel文件插入数据

时间:2013-06-14 10:24:39

标签: jsp ms-access-2007 netbeans-7

此代码正确运行,可以使用“APACHE POI库”将数据从excel文件插入MSACCESS表,但不会插入值。

代码没有显示任何错误,但tt正在运行时打印“无法插入.......”:

在这里,我使用DSN连接到MSACCESS数据库 NETBEANS IDE

DSN:amandsn
MS Office 2007访问表:学生
MS Office 2007 Excel文件:“myexcel.xls”由3列组成,即名称,等级和主题,以及我想要插入MSACCESS表的数据。

    <%@ page language="java" import="java.sql.*" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

    <%@ page import ="java.util.Date" %>
    <%@ page import ="java.io.*" %>
    <%@ page import ="java.io.FileNotFoundException" %>
    <%@ page import ="java.io.IOException" %>
    <%@ page import ="java.util.Iterator" %>
    <%@ page import ="java.util.ArrayList" %>

    // Using Apache POI Libraries
    <%@ page import ="org.apache.poi.hssf.usermodel.HSSFCell" %>
    <%@ page import ="org.apache.poi.hssf.usermodel.HSSFRow" %>
    <%@ page import ="org.apache.poi.hssf.usermodel.HSSFSheet" %>
    <%@ page import ="org.apache.poi.hssf.usermodel.HSSFWorkbook" %>
    <%@ page import ="org.apache.poi.poifs.filesystem.POIFSFileSystem" %>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>

    <%

   Connection con=null;
   Statement st =null;
   PreparedStatement ps=null;    


   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   con=DriverManager.getConnection("jdbc:odbc:amandsn");
   st=con.createStatement();


   %>  


   <%

   try {      
        String fileName="myexcel.xls";
        //Read an Excel File and Store in a ArrayList
        ArrayList dataHolder=readExcelFile(fileName);
        // Print the data read
        // PrintCellDataToConsole(dataHolder);

        String query="insert into Student values(?,?,?)";

        ps=con.prepareStatement(query);
        int count=0;
        ArrayList cellStoreArrayList=null;
        //For inserting into database
        for (int i=1;i < dataHolder.size(); i++) {
          cellStoreArrayList=(ArrayList)dataHolder.get(i);
          ps.setString(1,((HSSFCell)cellStoreArrayList.get(0)).toString());
          ps.setString(2,((HSSFCell)cellStoreArrayList.get(1)).toString());
          ps.setString(3,((HSSFCell)cellStoreArrayList.get(2)).toString());
          count= ps.executeUpdate();
          out.println(((HSSFCell)cellStoreArrayList.get(2)).toString() + "\t");
       }

//For checking data is inserted or not?
if(count>0)
   { %>
           Following details from Excel file have been inserted in student table of database
               <table>
                   <tr>
                       <th>Name</th>
                       <th>Grade</th>
                       <th>Subject</th>
                   </tr>

     <% for (int i=1;i < dataHolder.size(); i++) {
        cellStoreArrayList=(ArrayList)dataHolder.get(i);
     %>
        <tr>
           <td><%=((HSSFCell)cellStoreArrayList.get(0)).toString() %></td>
           <td><%=((HSSFCell)cellStoreArrayList.get(1)).toString() %></td>
           <td><%=((HSSFCell)cellStoreArrayList.get(2)).toString() %></td>
        </tr>
     <%}
   }
else
   { %>
      <center> Not able to insert.......</center>

<% }
    }
    catch(Exception e1){e1.printStackTrace(); }

%>

    <%!

   public static ArrayList readExcelFile(String fileName)
   {
       /** --Define a ArrayList
           --Holds ArrayList Of Cells
       */
       ArrayList cellArrayLisstHolder = new ArrayList();

       try{
           /** Creating Input Stream**/
           FileInputStream myInput = new FileInputStream(fileName);

           /** Create a POIFSFileSystem object**/
           POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);

           /** Create a workbook using the File System**/
           HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);

           /** Get the first sheet from workbook**/
           HSSFSheet mySheet = myWorkBook.getSheetAt(0);           


           /** We now need something to iterate through the cells.**/
           Iterator rowIter = mySheet.rowIterator();
           while(rowIter.hasNext()){
              HSSFRow myRow = (HSSFRow) rowIter.next();
              Iterator cellIter = myRow.cellIterator();
              ArrayList cellStoreArrayList=new ArrayList();
              while(cellIter.hasNext()){
                 HSSFCell myCell = (HSSFCell) cellIter.next();
                 cellStoreArrayList.add(myCell);

           }
          cellArrayLisstHolder.add(cellStoreArrayList);
       }
    }catch (Exception e){e.printStackTrace(); }
    return cellArrayLisstHolder;
    }%>   
   </table>
  </body>
</html>

1 个答案:

答案 0 :(得分:0)

count= ps.executeUpdate();

将始终将count指定为0,这就是为什么其他部分会被执行并显示Not able to insert...

的原因

相反,请将上述语句包含在try-catch中,如下所示,并在插入成功时更新count

try {
     ps.executeUpdate();
     count++; 
} catch(Exception e) {
}