我必须通过单击按钮基本上删除该行,因此当单击“删除”按钮时,变量id将获取该行。 变量id获取的行没有我选择并使用for循环我试图获取用户在单击按钮时选择的行,然后从行中提取userid然后使用从数据库中删除它的预准备语句。
// int id=Integer.parseInt(request.getParameter("user"));
// System.out.println("row"+" "+id);
List<registerValues> RegList = (ArrayList<registerValues>)session.getAttribute("regList");
int sizeOfList=RegList.size();
System.out.println("Size of List :"+sizeOfList);
List<registerValues> list = new ArrayList<registerValues>();
for(int i = 0 ; i<=sizeOfList ; i++) {
System.out.println(RegList.get(id-1).toString());
}
PreparedStatement ps=con.prepareStatement("delete from registration where userid=?");
ps.setInt(1,id);
int i=ps.executeUpdate();
if(i>0)
{
RequestDispatcher rd=request.getRequestDispatcher("welcome.jsp");
rd.include(request,response);
}
else
{
RequestDispatcher rd=request.getRequestDispatcher("error.jsp");
rd.include(request,response);
}
}
public class registerValues {
public String name;
public String city;
public String gender;
public String username;
public String password;
public String email;
public String contact_no;
public int userid;
public int getUserid() {
return userid;
}
public void setUserid(int userid) {
this.userid = userid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getContact_no() {
return contact_no;
}
public void setContact_no(String contact_no) {
this.contact_no = contact_no;
}
}
这是从数据库中选择我的值的地方 public static List Registervalues() {
Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;
values.registerValues registerobject=null;
List<registerValues> list = new ArrayList<registerValues>();
try {
conn = getDbConnection();
pst = conn.prepareStatement("Select name,city,gender,username,password,email,contact_no,userid from registration");
rs = pst.executeQuery();
while(rs!=null&&rs.next()) {
registerobject= new registerValues();
registerobject.setUserid(rs.getInt("userid"));
registerobject.setName(rs.getString("name"));
registerobject.setCity(rs.getString("city"));
registerobject.setGender(rs.getString("gender"));
registerobject.setUsername(rs.getString("username"));
registerobject.setPassword(rs.getString("password"));
registerobject.setEmail(rs.getString("email"));
registerobject.setContact_no(rs.getString("contact_no"));
list.add(registerobject);
System.out.println("REGISTRATION values present");
}
conn.close();
}
答案 0 :(得分:0)
您不能像以下那样获取RegList的值:
System.out.println(RegList.get(id-1).toString()); //wrong
RegList是registerValues
的列表,因此您需要使用getter来获取值:
如果你想获得用户ID:
System.out.println(RegList.getUserid());
如果有帮助,请告诉我。