日期:2014-05-20  浏览次数:20795 次

求助~~无法从数据库取出数据
代码:

  public User selectInformation(User u){
  User u1=new User();
  try{
  Connsql connsql=new Connsql();
  Connection con=connsql.getConnection();
  Statement stmt=con.createStatement();
  ResultSet rs=stmt.executeQuery("select * from user where name='"+u.getUserName()+"';");
  if(rs!=null){
  while(rs.next()){
  String str1=rs.getString(1);
  String str2=rs.getString(2);
  u1.setUserName(str1);
  u1.setUserPassword(str2);
  }
  }
  stmt.close();
  con.close();
  }
  catch(Exception e){
  }
  return u1;
  }

public class User {
private String userName;
private String userPassword;
public void setUserName(String userName){
this.userName=userName;
}
public void setUserPassword(String userPassword){
this.userPassword=userPassword;
}
public String getUserName(){
return userName;
}
public String getUserPassword(){
return userPassword;
}

}


ublic void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("gbk");
request.setCharacterEncoding("gbk");
HttpSession session=request.getSession();
  String userName=request.getParameter("userName");
String userPassword=request.getParameter("userPassword");
User u=null;
UserWorksql us=null;
if(userName.equals("")||userPassword.equals("")){
session.setAttribute("userloginstr","请填写完整");
request.getRequestDispatcher("/userloginfail.jsp").forward(request,response);
}else{
u=new User();
us=new UserWorksql();
u.setUserName(userName);
u.setUserPassword(userPassword);
User u1=us.selectInformation(u);
if(userName.equals(u1.getUserName())&&userPassword.equals(u1.getUserPassword()))
{
session.setAttribute("userloginname",userName);
request.getRequestDispatcher("/usermanage.jsp").forward(request,response);
}else{
if(!userName.equals(u1.getUserName())){
session.setAttribute("userloginstr","没有此管理员");
//session.setAttribute("userloginstr",q);
request.getRequestDispatcher("/userloginfail.jsp").forward(request,response);
}
else{
session.setAttribute("userloginstr","密码或管理员名不正确");
request.getRequestDispatcher("/userloginfail.jsp").forward(request,response);
}
}
}


}



试了下if(userName.equals(u1.getUserName())&&userPassword.equals(u1.getUserPassword()))里getUserName和getUserPassword都是空值 求教到底哪里出来问题~~~谢谢各位!!!


------解决方案--------------------
你有个习惯很不好:
catch(Exception e){ 
}

请修改为:
catch(Exception e){ 
e.printStackTrace();
}

否则出了错都完全不知道。
------解决方案--------------------
"select * from user where name='"+u.getUserName()+"'"括号里的sql语句不要分号
------解决方案--------------------
探讨

"select * from user where name='"+u.getUserName()+"'"括号里的sql语句不要分号

------解决方案--------------------