java.lang.ArrayIndexOutOfBoundsException: 0 解决不了

彭小位 发布于 2014/03/06 08:50
阅读 25K+
收藏 2

解读下一代网络:算力网络正从理想照进现实!>>>

源码:

<%@ page contentType="text/html" pageEncoding="GBK"%>

<%@ page import="java.sql.*"%>


<html>

<head>

<title>员工列表</title>

</head>

<script language="javascript">

function changeColor(obj,color){

obj.bgColor = color;

}

//跳到某一页

function go(num){

document.getElementById("cp").value = num ;

document.spform.submit() ; // 表单提交

}

</script>

<body>

<%!

public static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver" ;

public static final String DBURL = "jdbc:oracle:thin:@localhost :1521:orcl" ;

public static final String DBUSER = "scott" ;

public static final String DBPASSWORD = "admin" ;

public static final String URL = "emp_listpage.jsp";

%>

<%

Connection conn = null;

PreparedStatement pstmt = null;

ResultSet rs = null;

%>

<%

int currentPage = 1; //当前页,默认为1

int lineSize = 3; //每次显示的记录数

int allRecords = 0; //所有的记录数

int pageSize = 1; //表示全部的页数(尾页)

int lsData[] = {1,3,5,7,9,10,15,20,25,30,50,100};

%>

<%

try{

String cp =  request.getParameter("cp");

if( cp != null && !"".equals(cp) ){

currentPage = Integer.parseInt(cp) ;

}

}catch( Exception e ){

e.printStackTrace();

}

try{

String ls = request.getParameter("ls");

if( ls != null && !"".equals(ls) ){

lineSize = Integer.parseInt(ls);

}

}catch( Exception e ){

e.printStackTrace();

}

%>

<%

try{

Class.forName(DBDRIVER);

conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);

String sql = "select count(empno) from employer ";

pstmt = conn.prepareStatement(sql);

rs = pstmt.executeQuery();

//取得全部记录数

if( rs.next() ){

allRecords = rs.getInt(1);

}

}catch( Exception e ){

e.printStackTrace();

}

%>

<%

//计算有多少页

pageSize = (allRecords + lineSize -1) / lineSize;

%>

<%

try{

String sql = "SELECT * FROM ( " + 

" SELECT empno,ename,job,hiredate,sal,comm,ROWNUM rn " +

" FROM employer WHERE ROWNUM<=? ORDER BY empno) temp " + 

" WHERE temp.rn>? " ;

pstmt.setInt(1,currentPage * lineSize);

pstmt.setInt(2,(currentPage-1)*lineSize);

pstmt = conn.prepareStatement(sql);

rs = pstmt.executeQuery();

%>

<center>

<h1>雇员列表</h1>

<form name="spform" action="<%=URL%>" method="post">

<input type="button" value="首页" onclick="go(1)" <%=currentPage==1?"disabled":""%> />

<input type="button" value="上一页" onclick="go(currentPage-1)"  <%=currentPage==1?"disabled":""%> />

<input type="button" value="下一页" onclick="go(currentPage+1)"  <%=currentPage==pageSize?"disabled":""%> />

<input type="button" value="尾页" onclick="go(pageSize)"  <%=currentPage==pageSize?"disabled":""%> />

跳转到第

<select name="selcp" onchange="go(this.value)">

<%

for( int i=1; i <= pageSize; i++ ){

%>

<option value="<%=i%>" <%=i==currentPage?"selected":""%>><%=i%></option>

<%

}

%>

</select>页

每页显示

<select name="ls" onchange="go(1)">

<%

for( int i=0; i<lsData.length; i++ ){

%>

<option value="<%=lsData[i]%>" <%=i==currentPage?"selected":""%>><%=lsData[i]%></option>

<%

}

%>

</select>条

<input type="hidden" name="cp" id="cp" value="1">

</form>

</center>

<table border="1" cellspacing="0" cellpadding="0" align="center" width="500">

<tr align="center">

<td>雇员编号</td>

<td>雇员名称</td>

<td>雇员工作</td>

<td>雇佣日期</td>

<td>基本工资</td>

<td>奖金</td>

</tr>

<%

while( rs.next() ){

int empno = rs.getInt(1);

String ename = rs.getString(2);

String job = rs.getString(3);

Date date = rs.getDate(4);

double sal = rs.getDouble(5);

double comm = rs. getDouble(6);

%>

<tr align="center" onMouseOver="changeColor(this,'f0f0f0')" onMouseOut="changeColor(this,'white')">

<td><%=empno%></td>

<td><%=ename%></td>

<td><%=job%></td>

<td><%=date%></td>

<td><%=sal%></td>

<td><%=comm%></td>

</tr>

<%

}

%>

</table>

<%

}catch( Exception e ){

e.printStackTrace();

}finally{

if(rs != null){

try{

rs.close();

}catch( Exception e ){

e.printStackTrace();

}

}

if( pstmt != null ){

try{

pstmt.close();

}catch( Exception e ){

e.printStackTrace();

}

}

if( conn != null ){

try{

conn.close();

}catch( Exception e ){

e.printStackTrace();

}

}

}

%>

</body>

</html>

java.lang.ArrayIndexOutOfBoundsException: 0
at oracle.jdbc.dbaccess.DBDataSetImpl._getDBItem(DBDataSetImpl.java:378)
at oracle.jdbc.dbaccess.DBDataSetImpl._createOrGetDBItem(DBDataSetImpl.java:781)
at oracle.jdbc.dbaccess.DBDataSetImpl.setBytesBindItem(DBDataSetImpl.java:2399)
at oracle.jdbc.driver.OraclePreparedStatement.setItem(OraclePreparedStatement.java:1134)
at oracle.jdbc.driver.OraclePreparedStatement.setInt(OraclePreparedStatement.java:1440)
at org.apache.jsp.emp_005flistpage_jsp._jspService(emp_005flistpage_jsp.java:154)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:291)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:662)
解决不了,求帮助

加载中
0
Lukewxl
Lukewxl

at org.apache.jsp.emp_005flistpage_jsp._jspService(emp_005flistpage_jsp.java:154)

在tomcat的work目录,找到这个java文件,看看154行的代码再说吧

彭小位
彭小位
能不能说明白点,帮我解决一下呢?
0
优雅先生
优雅先生

1、首先”java.lang.ArrayIndexOutOfBoundsException: 0“这句话的异常在什么情况会发生呢?在数组是一个空数组,注意不是null的情况下会发生,比如下面的代码就会抛出这个异常,本质上你这个问题也是相同的,即你对一个空数组进行了索引为0的访问导致数组越界:

int[] a = new int[0];   // 或者 int[] a = new int[] {};
System.out.println(a[0]);

2、根据后面的异常信息:

at oracle.jdbc.driver.OraclePreparedStatement.setItem(OraclePreparedStatement.java:1134)
at oracle.jdbc.driver.OraclePreparedStatement.setInt(OraclePreparedStatement.java:1440)
at org.apache.jsp.emp_005flistpage_jsp._jspService(emp_005flistpage_jsp.java:154)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)

找到对应的代码:

String sql = "select count(empno) from employer ";

pstmt = conn.prepareStatement(sql);
pstmt.setInt(1,currentPage * lineSize);

有没有看出问题呢?预编译语句中根本就没有参数占位符,你对它调用setInt自然会导致越界了(想象下PreparedStatement后面通过一个数组保存参数)。

至于怎么改,由于不清楚你的业务,所以也不好多说。但你至少要为预编译语句提供几个占位符。占位符怎么用,我举个例子,注意updateString中的?就是参数占位符:

PreparedStatement updateSales = null;
String updateString =
        "update " + dbName + ".COFFEES " +
        "set SALES = ? where COF_NAME = ?";

try {
        updateSales = con.prepareStatement(updateString);

        for (Map.Entry<String, Integer> e : salesForWeek.entrySet()) {
            updateSales.setInt(1, e.getValue().intValue());
            updateSales.setString(2, e.getKey());
            updateSales.executeUpdate();
            updateTotal.setInt(1, e.getValue().intValue());
            updateTotal.setString(2, e.getKey());
            updateTotal.executeUpdate();
            con.commit();
        }
} catch (SQLException e ) {
   // handle SQLException
}


0
彭小位
彭小位
我要实现一个分页,那两个参数前面不是定义了吗? 
优雅先生
优雅先生
回复 @美好的2014 : pstmt = conn.prepareStatement(sql);
优雅先生
优雅先生
在这两行代码: String sql = "SELECT * FROM ( " + 。。。 pstmt.setInt(1,currentPage * lineSize); 之间加一行: psmt = conn.preparedStatement(sql);
0
Mr_Zhou
Mr_Zhou
String sql = "select count(empno) from employer ";
你这条语句能分页?


优雅先生
优雅先生
回复 @_Sole : 在下面那条SQL语句下加上: psmt = conn.preparedStatement(sql); 我跟你说了那么多都白说了。。。
彭小位
彭小位
这是查所有记录 下面还有一天sql语句咧
0
顺其自然001
顺其自然001
String sql = "SELECT * FROM ( " + 

" SELECT empno,ename,job,hiredate,sal,comm,ROWNUM rn " +

" FROM employer WHERE ROWNUM<=? ORDER BY empno) temp " + 

" WHERE temp.rn>? " ;

pstmt.setInt(1,currentPage * lineSize);

pstmt.setInt(2,(currentPage-1)*lineSize);

pstmt = conn.prepareStatement(sql);

rs = pstmt.executeQuery();




顺序反了
pstmt = conn.prepareStatement(sql);

pstmt.setInt(1,currentPage * lineSize);


pstmt.setInt(2,(currentPage-1)*lineSize);




0
Lukewxl
Lukewxl
jsp会编义为class文件,
0
Brin想写程序
Brin想写程序

pstmt.setInt(1,currentPage * lineSize);

pstmt.setInt(2,(currentPage-1)*lineSize);

pstmt = conn.prepareStatement(sql);

。。写反了。。

pstmt = conn.prepareStatement(sql);

pstmt.setInt(1,currentPage * lineSize);

pstmt.setInt(2,(currentPage-1)*lineSize);


返回顶部
顶部