2013-06-14 1 views
0

이 코드는 "APACHE POI 라이브러리"를 사용하여 Excel 파일의 데이터를 MSACCESS 테이블에 삽입 할 때 올바르게 실행되지만 값을 삽입하지 않습니다.JSP를 사용하여 MSACCESS 데이터베이스의 Excel 파일에서 데이터 삽입

코드는 오류를 표시하지 않고, TT는 실행에 "....... 삽입 할 수 없음"인쇄되어

를 자, 내가 MSACCESS 데이터베이스에 연결하는 DSN을 사용했습니다.
넷빈즈 IDE는

DSN :
는 MS 오피스 2007 액세스 표 amandsn 학생
MS 오피스 2007 엑셀 파일 : 3columns 즉, 이름, 학년과 주제로 구성 "myexcel.xls"거기에 데이터 내가 할 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> 
+0

는 – Mubin

답변

0
count= ps.executeUpdate(); 

항상 0-count를 할당하고 그 Not able to insert... 대신

는 아래와 같이 try-catch에 위의 문을 감싸는 다른 부분이 실행 얻을 것이다 이유는 이성과 표시하고 count 값의 경우 업데이트됩니다 삽입

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

내가 지금 이런 짓을 한 완전한 스택 추적을 보여 성공 : 대한 (I = 1 int로, 내가 다를 < taHolder.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()); 시도 { ps.executeUpdate(); 카운트 ++; } catch (예외 e) { } out.println (((HSSFCell) cellStoreArrayList.get (2)). toString() + "\ t"); } –

+0

결과는 무엇입니까? – Mubin

+0

결과는 @Mubin ...이 삽입되지 않았습니다. –

관련 문제