在我的项目中我有一个excel表,其中包含大量的标记,ID,信用...我需要从excel文件上传到数据库....我使用apache poi来读取和更新但是我得到了错误...
DB_Connection.java
package DB;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DB_Connection
{
// private String datasize;
private Connection con;
public DB_Connection()
{
try
{
String conUrl="jdbc:oracle:thin:@localhost:1521:xe";
String userName="SYSTEM";
String pass="raje";
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection(conUrl,userName,pass);
}
catch(Exception s)
{
System.out.println(s);
}
}
public Connection getConn()
{
return con;
}
public void setConn(Connection con)
{
this.con = con;
}
}
readExcel.jsp:
<%@page language="java" import="java.sql.*" contentType="text/html" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<%@ 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.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=UTF-8">
<title>JSP Page</title>
</head>
<body>
<jsp:useBean id="connection" class="DB.DB_Connection" scope="application">
<jsp:setProperty name="connection" property="*"/>
</jsp:useBean>
<%!
Connection con;
PreparedStatement ps=null;
public static ArrayList readExcelFile(String fileName)
{
/** --Define a ArrayList
--Holds ArrayList Of Cells
*/
ArrayList cellArrayListHolder = new ArrayList();
try{
/** Creating Input Stream**/
FileInputStream myInput = new FileInputStream(fileName);
// System.out.print("myInput");
/** 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);
}
cellArrayListHolder.add(cellStoreArrayList);
}
}
catch (Exception e)
{
e.printStackTrace();
}
return cellArrayListHolder;
}%>
<%
String fileName="C://Documents and Settings//raje//Desktop//JGRESULTS1.xls";
ArrayList dataHolder=readExcelFile(fileName);
//Print the data read
//printCellDataToConsole(dataHolder);
con= connection.getConn();
ps=con.prepareStatement("insert into sample2 values(?,?,?,?)");
int count=1;
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());
ps.setString(4,((HSSFCell)cellStoreArrayList.get(3)).toString());
count= ps.executeUpdate();
System.out.print(((HSSFCell)cellStoreArrayList.get(3)).toString() + "\t");
}
count++;
//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>Student's Name</th>
<th>Class</th>
<th>external</th>
<th>credits</th>
</tr>
<% for (int i=1;i < dataHolder.size(); i++) {
cell StoreArrayList=(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>
<td><%=((HSSFCell)cellStoreArrayList.get(3)).toString() %></td>
</tr>
<%}
}
else
{%>
<% out.print("not successfull");
} %>
</table>
</body>
</html>
输出:
输入例外报告
消息
description服务器遇到内部错误(),导致无法完成此请求。
例外
org.apache.jasper.JasperException: An exception occurred processing JSP page /readExcel.jsp at line 98
95: {
96: cellStoreArrayList=(ArrayList)dataHolder.get(i);
97: ps.setString(1,((HSSFCell)cellStoreArrayList.get(0)).toString());
98: ps.setString(2,((HSSFCell)cellStoreArrayList.get(1)).toString());
99: ps.setString(3,((HSSFCell)cellStoreArrayList.get(2)).toString());
100: ps.setString(4,((HSSFCell)cellStoreArrayList.get(3)).toString());
101: count= ps.executeUpdate();
堆栈跟踪:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:553)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:457)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:3 93)
root cause
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
java.util.ArrayList.RangeCheck(ArrayList.java:547)
java.util.ArrayList.get(ArrayList.java:322)
org.apache.jsp.readExcel_jsp._jspService(readExcel_jsp.java:175)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
答案 0 :(得分:1)
如果您实际发布错误,您将获得更多更好的帮助。
这段代码很乱。它做得太多了:
我建议将它分解为单独的类,你可以开发,测试和放在一边。
没有人应该用scriptlet编写JSP。这是一种不可信的1999年风格。最好学习JSTL并添加一些servlet。
更新:
原因如下:
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
java.util.ArrayList.RangeCheck(ArrayList.java:547)
您假设存在数组元素,但JVM不同意您的意见。
这是你做错了什么的本质:
List<String> values = new ArrayList<String>();
values.add("element one");
System.out.println(values.size()); // this will print 1
System.out.println(values.get(100)); // there's only one element in the list, but you tried to access 100.
错误!弄清楚你的代码在哪里做了这样的事情你会得到它。
答案 1 :(得分:0)
有人指出:你所做的是一种非常糟糕的做法......
但是,当前的问题很可能是,您尝试从中读取单元格的行不够长,因此(注意:数组支持)列表超出范围。
为这种情况做好准备,我会这样做:
cellStoreArrayList=(ArrayList)dataHolder.get(i);
for(int i=1;i<4;i++) { // I'd use a loop even for fixed number of iterations, I vigorously hate duplication
if(cellStoreArrayList.size()>=i) { // check for row to be long enough --> no ArrayOutOfBounds anymore HSSFCell cell = (HSSFCell)cellStoreArrayList.get(i-1);
if(cell!=null) { // check for null: no NullPointerException anymore...
ps.setString(i,cell.toString());
}
else {
ps.setString(i,"");
}
//ps.setString(i,cell==null?"":cell.toString()); //this is good too, but a bit probably less readable
}
}
备注强>:
<HSSFCell>
泛型类型,不需要演员,并且可以解决错误和错误的一些可能性cellStoreArrayList
=糟糕! cellStoreList
=好答案 2 :(得分:0)
从查看您的代码和堆栈跟踪看起来您得到的是IndexOutOfBoundsException
,因为cellStoreArrayList
中的单元格行的长度只有1.我建议您查看电子表格确保您正在读取的每一行都有4列,因为此约束是硬编码的。
在相关的说明中,如果要对数组索引进行硬编码,则应将其包围在某种异常捕获机制中。