读取mysql 判断对象是否存在
用户名不存在返回-1
密码错误返回-2
登录成功返回0
部分代码如下:
AccountSverlet
@WebServlet("/account")
public class AccountServlet extends HttpServlet {
private SystemAdminService systemAdminService = new SystemAdminServiceImpl();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
SystemAdminDto systemAdminDto = this.systemAdminService.login(username,password);
switch (systemAdminDto.getCode()){
case -1:
req.setAttribute("usernameError","用户名不存在");
req.getRequestDispatcher("login.jsp").forward(req,resp);
break;
case -2:
req.setAttribute("passwordError","密码错误");
req.getRequestDispatcher("login.jsp").forward(req,resp);
break;
case 0:
System.out.println("登录成功!");
break;
}
}
}
public class SystemAdminDaoImpl implements SystemAdminDao {
@Override
public SystemAdmin findBYUsername(String username) {
Connection connection = JDBCUtil.getConnection();
String sql = "select * from system_admin where username = '"+username+"'";
PreparedStatement statement = null;
ResultSet resultSet = null;
try {
statement = connection.prepareStatement(sql);
resultSet = statement.executeQuery();
if (resultSet.next()) {
int id = resultSet.getInt(1);
username = resultSet.getString(2);
String password = resultSet.getString(3);
String gender = resultSet.getString(4);
String Purview = resultSet.getString(5);
String Telephone = resultSet.getString(6);
return new SystemAdmin(id,username,password,gender,Purview,Telephone);
}
} catch (SQLException e) {
throw new RuntimeException(e);
}finally {
JDBCUtil.release(connection,statement,resultSet);
}
return null;
}
}
运行的时候不论是输入正确的用户名或者是错误密码 返回的都是用户名不存在
这种情况是问题出在哪里哇?是数据库没读取到的与原因吗?