异常来了java.lang.IndexOutOfBoundsException:索引:7,大小:插入时为7

时间:2018-05-31 10:54:05

标签: oracle jsp

我正在创建一个应用程序,用户可以在以前浏览并保存数据库中的Excel数据用于测试目的我在excel中只使用了一列并且工作正常但现在我正在尝试在数据库中插入总共11列8是在表中,但问题是我面临java.lang.IndexOutOfBoundsException:索引:7,大小:7异常

 <%@page import="org.apache.commons.fileupload.FileItemStream"%>
<%@page import="java.util.List"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="org.omg.PortableServer.Servant"%>
<%@page import="org.apache.commons.fileupload.FileItemFactory"%>
<%@page import="java.nio.file.Paths"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
     <%@page import="java.sql.*" %>
    <%@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" %> 
    <%@page import="org.apache.poi.hssf.usermodel.*" %>
    <%@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" %>
    <%@page import="org.apache.poi.ss.usermodel.Cell" %>
    <%@page import ="org.apache.poi.ss.usermodel.Row"%>
    <%@page import="org.apache.poi.ss.usermodel.Sheet" %>
    <%@page import="org.apache.poi.ss.usermodel.Workbook" %>
    <%@page import="com.oreilly.servlet.MultipartRequest" %>
    <%@page import="org.apache.poi.xssf.usermodel.*"%>
    <%@page import="org.apache.poi.xssf.usermodel.XSSFWorkbook"%>
    <%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>


     <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>

        <%!
        private String getFileName(Part p){
        String header=p.getHeader("content-disposition");
        String filename = header.substring(header.indexOf("filename=\"")).split("\"")[1];  //getting filename

        return filename;
}


        %>   
  <%


 ArrayList CellArrayListHolder=new ArrayList();
 Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection con=DriverManager.getConnection("jdbc:oracle:thin:@172.18.114.213:1821:pk","xe","se");
      String contentType = request.getContentType();

if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0))
{
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead,formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
String saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
out.println(saveFile);
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;

FileOutputStream fileOut = new FileOutputStream(saveFile);

fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();




%>
<%
 File file1 = new File(saveFile);
FileInputStream file_inut=new FileInputStream(file1);

if(saveFile.indexOf(".")>0)
{
 saveFile=saveFile.substring(0,saveFile.lastIndexOf("."));
}
    String query="Select File_name,to_char(FILE_UPLOAD_DATE,'DD-MON-YYYY') from DYNAMIC_INSERT where File_name='"+saveFile+"'";
    Statement st1=con.createStatement();
    ResultSet rs=st1.executeQuery(query);
    if(rs.next())
    {
          String file_name=rs.getString(1);
          String FILE_UPLOAD_DATE=rs.getString(2);


                       out.println("<script type=\"text/javascript\">");
 out.println("alert('File name " + file_name + " already exists File upload Date is "+FILE_UPLOAD_DATE+"')");
 out.println("location='xlsUpload.html';");
   out.println("</script>");


    }
  else
    { 

XSSFWorkbook workbook=new XSSFWorkbook(file_inut);
Sheet firstsheet=workbook.getSheetAt(0);
Iterator<Row> iterator=firstsheet.iterator();
int count=0;
int search_id=0;    
while(iterator.hasNext())
{
   XSSFRow nextrow=(XSSFRow)iterator.next();
    ArrayList rowarraylist=new ArrayList();
    Iterator<Cell> cellIterator=nextrow.cellIterator();

    while(cellIterator.hasNext())
    {
        XSSFCell cell=(XSSFCell)cellIterator.next();
        rowarraylist.add(cell);
    }
    CellArrayListHolder.add(rowarraylist);
}
       out.println(CellArrayListHolder);
       ArrayList rowarraylist=null;
       PreparedStatement st=con.prepareStatement("insert into DYNAMIC_INSERT values(?,?,?,?,?,?,?,?,?,?,?)");

      st.setString(1, saveFile);
      st.setInt(2, search_id);
        st.setDate(3,new java.sql.Date(System.currentTimeMillis()));
for(int i=0;i<CellArrayListHolder.size()-1;i++)
{
    rowarraylist=(ArrayList)CellArrayListHolder.get(i);

    st.setString(4, rowarraylist.get(0).toString());
    st.setString(5, rowarraylist.get(1).toString());
   st.setString(6, rowarraylist.get(2).toString());
   st.setString(7, rowarraylist.get(3).toString());
   st.setString(8, rowarraylist.get(4).toString());
    st.setString(9, rowarraylist.get(5).toString());
   st.setString(10, rowarraylist.get(6).toString());
  st.setString(11, rowarraylist.get(7).toString());



}
 count=st.executeUpdate();
if(count>0)
{
       out.println("<script type=\"text/javascript\">");
   out.println("alert('File added successfully');");
      out.println("location='xlsUpload.html';");
   out.println("</script>");    
}
    }
}



  %>
    </body>
</html>







 i done some research there i found, for loop we have to do -1 like 
    this for(int i=0;i<CellArrayListHolder.size()-1;i++) but still i'm facing same error when i'm change for loop like for(int i=0;i<CellArrayListHolder.size()-2;i++) then excel header inserted in table not data

1 个答案:

答案 0 :(得分:0)

这里是我得到解决方案,因为我尝试输入空值因此我遇到问题表中的数据上传

PreparedStatement ps=con.prepareStatement("insert into DYNAMIC_INSERT values(?,?,?,?,?,?,?,?,?,?,?)");

      ps.setString(1, saveFile);
      ps.setInt(2, search_id);
        ps.setDate(3,new java.sql.Date(System.currentTimeMillis()));

for(int i=1;i<CellArrayListHolder.size();i++)
{
    rowarraylist=(ArrayList)CellArrayListHolder.get(i);

    ps.setString(4, (rowarraylist.get(0)).toString());
   ps.setString(5, (rowarraylist.get(1)).toString());
   ps.setString(6, (rowarraylist.get(2)).toString());
    ps.setString(7, (rowarraylist.get(3)).toString());
    ps.setString(8, (rowarraylist.get(4)).toString());
    ps.setString(9, (rowarraylist.get(5)).toString());
    ps.setString(10, (rowarraylist.get(6)).toString());
    ps.setString(11, (rowarraylist.get(7)).toString());
 count=ps.executeUpdate();
}