org.apache.jasper.JasperException: Index: 0, Size: 0
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:372)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
java.util.ArrayList.RangeCheck(ArrayList.java:547)
java.util.ArrayList.get(ArrayList.java:322)
org.apache.jsp.updateuser_jsp._jspService(updateuser_jsp.java:63)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
note The full stack trace of the root cause is available in the Apache Tomcat/5.0.28 logs.
package bean;
public class User {
private int uid;
private String username;
private String password;
private String usersex;
private String userage;
private String useremail;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUserage() {
return userage;
}
public void setUserage(String userage) {
this.userage = userage;
}
public String getUseremail() {
return useremail;
}
public void setUseremail(String useremail) {
this.useremail = useremail;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUsersex() {
return usersex;
}
public void setUsersex(String usersex) {
this.usersex = usersex;
}
}
package db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import bean.User;
public class Conndenglu {
public static Connection start() {
Connection conn = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc:odbc:xiangmu", "", "");
} catch (ClassNotFoundException e) {
System.out.println("类文件未找到!!");
e.printStackTrace();
} catch (SQLException e) {
System.out.println("数据库初始化出现异常!");
e.printStackTrace();
}
return conn;
}
public static int insert(String uname, String upwd, String usex,
String uage, String uemail) {
int i = 0;
Connection conn = null;
PreparedStatement ps = null;
try {
conn = start();
String sql = "insert into user(username,userpassword,usersex,userage,useremail) values(?,?,?,?,?)";
ps = conn.prepareStatement(sql);
ps.setString(1, uname);
ps.setString(2, upwd);
ps.setString(3, usex);
ps.setString(4, uage);
ps.setString(5, uemail);
i = ps.executeUpdate();
} catch (SQLException e) {
System.out.println("用户注册时出现异常!");
e.printStackTrace();
}
finally {
close(ps, conn);
}
return i;
}
public static ArrayList select(String uname, String upwd) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
User u = null;
ArrayList list = new ArrayList();
try {
conn = start();
String sql = "select * from user where username =? and userpassword =?";
ps = conn.prepareStatement(sql);
ps.setString(1, uname);
ps.setString(2, upwd);
rs = ps.executeQuery();
u = new User();
while (rs.next()) {
u = new User();
u.setUid(rs.getInt("uid"));
u.setUsername(rs.getString("username"));
u.setPassword(rs.getString("userpassword"));
list.add(u);
}
} catch (Exception e) {
System.out.println("用户注册时出现异常11111!");
e.printStackTrace();
}
finally {
close(rs, ps, conn);
}
return list;
}
public static int One(String uname) throws Exception{
Connection conn = null;
Statement st = null;
ResultSet rs = null;
int i ;
conn = start();
String sql = "select count(*) from user where username='"+uname+"' ";
System.out.println(sql);
st = conn.createStatement();
rs = st.executeQuery(sql);
if(rs.next()&&rs.getInt(1)>0){
i=1;
return i;
}else{
i= 0;
return i;
}
}
public static void changeUnameAndUpwd(String uname, String upwd,String uid) {
Connection conn = null;
Statement st = null;
conn = start();
try {
conn = start();
String sql = "update user set username='"+uname+"',userpassword='"+upwd+"' where uid ="+uid;
System.out.println(sql);
st = conn.createStatement();
st.executeUpdate(sql);
} catch (Exception e) {
System.out.println("修改用户信息的时候发生异常!");
e.printStackTrace();
}
finally{
}
}
public static void close(ResultSet rs, PreparedStatement ps, Connection conn) {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (ps != null) {
ps.close();
ps = null;
}
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
System.out.println("关闭时出现异常1");
e.printStackTrace();
}
}
public static void close(PreparedStatement ps, Connection conn) {
try {
if (ps != null) {
ps.close();
ps = null;
}
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
System.out.println("关闭时出现异常2");
e.printStackTrace();
}
}
public static void close(Connection conn) {
try {
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
System.out.println("关闭时出现异常3");
e.printStackTrace();
}
}
}
<%@ page language="java" import="java.util.*,db.Conndenglu,bean.User" pageEncoding="gbk"%>
<%
request.setCharacterEncoding("gbk");
String uname = request.getParameter("uname");
String upwd = request.getParameter("upwd");
String usex = request.getParameter("usex");
String uage =request.getParameter("uage");
String uemail = request.getParameter("uemail");
int i = Conndenglu.One(uname);
if(i==0){
Conndenglu.insert(uname,upwd,usex,uage,uemail);}
%>
<%
ArrayList list = Conndenglu.select(uname,upwd);
User u = (User) list.get(0);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<style type="text/css">
<!--
#Layer1 {
position:absolute;
left:181px;
top:75px;
width:566px;
height:313px;
z-index:1;
font-size:14px;
}
-->
</style>
</head>
<body>
<div id="Layer1">
<table width="566" height="278" border="1" cellpadding="0" cellspacing="0" bordercolor="#0099FF">
<tr>
<td width="566"><center><%if(i==0){%> 恭喜您,注册成功!<%}else{ %>该用户已被注册<%} %></center></td>
</tr>
<tr>
<td><center>您的会员名是:<%=u.getUsername() %></center></td>
</tr>
<tr>
<td><center><a href="xiugai.jsp?uid=<%=u.getUid() %>">[修改您的资料]</a></center></td>
</tr>
<tr>
<td><center>[回到首页]</center></td>
</tr>
</table>
</div>
</body>
</html>
8 个解决方案
#1
#2
User u = (User) list.get(0);
你的list里有东西么?
你的list里有东西么?
#3
刚才只是大概看了一下,
好像是
ArrayList list = Conndenglu.select(uname,upwd);
User u = (User) list.get(0);
这里的错误
你改成这样试试
User u = new User();
ArrayList list = Conndenglu.select(uname,upwd);
if(list!=null && list.size()>0){
u = (User) list.get(0);
}
好像是
ArrayList list = Conndenglu.select(uname,upwd);
User u = (User) list.get(0);
这里的错误
你改成这样试试
User u = new User();
ArrayList list = Conndenglu.select(uname,upwd);
if(list!=null && list.size()>0){
u = (User) list.get(0);
}
#4
有的 是登陆界面传过来的uname 和 upwd
我想判断一下用户名不能重复的问题,一开始没有加
int i = Conndenglu.One(uname);
if(i==0){
Conndenglu.insert(uname,upwd,usex,uage,uemail);} <%if(i==0){%> 恭喜您,注册成功!<%}else{ %>该用户已被注册<%} %>
没有报错 一加上就报错了
我想判断一下用户名不能重复的问题,一开始没有加
int i = Conndenglu.One(uname);
if(i==0){
Conndenglu.insert(uname,upwd,usex,uage,uemail);} <%if(i==0){%> 恭喜您,注册成功!<%}else{ %>该用户已被注册<%} %>
没有报错 一加上就报错了
#5
发表于:2008-07-30 11:53:433楼 得分:0
刚才只是大概看了一下,
好像是
ArrayList list = Conndenglu.select(uname,upwd);
User u = (User) list.get(0);
这里的错误
你改成这样试试
User u = new User();
ArrayList list = Conndenglu.select(uname,upwd);
if(list!=null && list.size()>0){
u = (User) list.get(0);
}
啊 谢谢了 已经解决了
刚才只是大概看了一下,
好像是
ArrayList list = Conndenglu.select(uname,upwd);
User u = (User) list.get(0);
这里的错误
你改成这样试试
User u = new User();
ArrayList list = Conndenglu.select(uname,upwd);
if(list!=null && list.size()>0){
u = (User) list.get(0);
}
啊 谢谢了 已经解决了
#6
是怎样都出错吗?
还是户已被注册的时候出错呢?
还是户已被注册的时候出错呢?
#7
额 大概是这样的user是数据库保留关键字 你直接select * from user where username =? and userpassword =?
可能他不认为user是一个表 你把user改写成为[user] 还有更新那里也是大概就这样 我以前MS也出现过 还有你这样写会有注入漏洞吧
可能他不认为user是一个表 你把user改写成为[user] 还有更新那里也是大概就这样 我以前MS也出现过 还有你这样写会有注入漏洞吧
public static ArrayList select(String uname, String upwd) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
User u = null;
ArrayList list = new ArrayList();
try {
conn = start();
String sql = "select [userpassword] from user where username =?";
ps = conn.prepareStatement(sql);
ps.setString(1, uname);
rs = ps.executeQuery();
if(rs.next()) {
u = new User();
u.setUid(rs.getInt("uid"));
u.setUsername(rs.getString("username"));
u.setPassword(rs.getString("userpassword"));
if(upwd.equals(u.getPassword()){
list.add(u);
}
}
} catch (Exception e) {
System.out.println("用户注册时出现异常11111!");
e.printStackTrace();
}
finally {
close(rs, ps, conn);
}
return list;
}
#8
一个也没有select出来,所有get(0)有问题
#1
#2
User u = (User) list.get(0);
你的list里有东西么?
你的list里有东西么?
#3
刚才只是大概看了一下,
好像是
ArrayList list = Conndenglu.select(uname,upwd);
User u = (User) list.get(0);
这里的错误
你改成这样试试
User u = new User();
ArrayList list = Conndenglu.select(uname,upwd);
if(list!=null && list.size()>0){
u = (User) list.get(0);
}
好像是
ArrayList list = Conndenglu.select(uname,upwd);
User u = (User) list.get(0);
这里的错误
你改成这样试试
User u = new User();
ArrayList list = Conndenglu.select(uname,upwd);
if(list!=null && list.size()>0){
u = (User) list.get(0);
}
#4
有的 是登陆界面传过来的uname 和 upwd
我想判断一下用户名不能重复的问题,一开始没有加
int i = Conndenglu.One(uname);
if(i==0){
Conndenglu.insert(uname,upwd,usex,uage,uemail);} <%if(i==0){%> 恭喜您,注册成功!<%}else{ %>该用户已被注册<%} %>
没有报错 一加上就报错了
我想判断一下用户名不能重复的问题,一开始没有加
int i = Conndenglu.One(uname);
if(i==0){
Conndenglu.insert(uname,upwd,usex,uage,uemail);} <%if(i==0){%> 恭喜您,注册成功!<%}else{ %>该用户已被注册<%} %>
没有报错 一加上就报错了
#5
发表于:2008-07-30 11:53:433楼 得分:0
刚才只是大概看了一下,
好像是
ArrayList list = Conndenglu.select(uname,upwd);
User u = (User) list.get(0);
这里的错误
你改成这样试试
User u = new User();
ArrayList list = Conndenglu.select(uname,upwd);
if(list!=null && list.size()>0){
u = (User) list.get(0);
}
啊 谢谢了 已经解决了
刚才只是大概看了一下,
好像是
ArrayList list = Conndenglu.select(uname,upwd);
User u = (User) list.get(0);
这里的错误
你改成这样试试
User u = new User();
ArrayList list = Conndenglu.select(uname,upwd);
if(list!=null && list.size()>0){
u = (User) list.get(0);
}
啊 谢谢了 已经解决了
#6
是怎样都出错吗?
还是户已被注册的时候出错呢?
还是户已被注册的时候出错呢?
#7
额 大概是这样的user是数据库保留关键字 你直接select * from user where username =? and userpassword =?
可能他不认为user是一个表 你把user改写成为[user] 还有更新那里也是大概就这样 我以前MS也出现过 还有你这样写会有注入漏洞吧
可能他不认为user是一个表 你把user改写成为[user] 还有更新那里也是大概就这样 我以前MS也出现过 还有你这样写会有注入漏洞吧
public static ArrayList select(String uname, String upwd) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
User u = null;
ArrayList list = new ArrayList();
try {
conn = start();
String sql = "select [userpassword] from user where username =?";
ps = conn.prepareStatement(sql);
ps.setString(1, uname);
rs = ps.executeQuery();
if(rs.next()) {
u = new User();
u.setUid(rs.getInt("uid"));
u.setUsername(rs.getString("username"));
u.setPassword(rs.getString("userpassword"));
if(upwd.equals(u.getPassword()){
list.add(u);
}
}
} catch (Exception e) {
System.out.println("用户注册时出现异常11111!");
e.printStackTrace();
}
finally {
close(rs, ps, conn);
}
return list;
}
#8
一个也没有select出来,所有get(0)有问题