本应为空的result却不为空,为什么?
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException,
IOException {
// TODO Auto-generated method stub
String name= "";
//name = new String (request.getParameter("username").getBytes("iso-8859-1"),"utf-8");
Connection C = DataBase.getConnection();
try{
Statement state = C.createStatement();
ResultSet result = state.executeQuery("select * from userpass where username=" + "'" +name+ "'");
if (result != null){
System.out.println("用户名存在");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
while(result.next()){
out.print("用户名:"+name+"的密码为: "+result.getString("password"));
}
}else{
PrintWriter out = response.getWriter();
out.print("用户名不存在");
}
}catch(Exception e){
System.out.println(e.getMessage());
}
}
上面是一个简单的SERVLET ,功能是根据接收到的name去查询表中对应的密码,然后输出。现在情况是这样:如果name存在,正确输出密码;如果name不存在,ResultSet result结果集不为空,但输出为空。name不存在的情况下,查询结果集应该是为空的,这是为什么?难道是我的查询串构造有问题吗?数据库为sql server,多谢!
------解决方案--------------------
boolean flag = false;
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
while (result.next()) {
out.print("用户名:" + name + "的密码为: " + result.getString("password"));