smbms超市管理系统之登录(查询)
作者:互联网
在搭好数据库以及基础项目以后,现在开始进入登录功能的实现
以下是自己根据实体类将数据库表生成:
https://blog.csdn.net/weixin_44760375/article/details/120214314?spm=1001.2014.3001.5501
在涉及sql访问数据库时,一个bug折磨了我一整天
最后发现是 重复传入sql导致!!executeQuery()方法不需要再传入sql
UserDaoImpl:
public class UserDaoImpl implements UserDao{
public User getUser(Connection connecton, String userCode,String userPassword) {
PreparedStatement s=null;
ResultSet re=null;
User user=null;
String sql="select * from smbms_user where user_code=? and user_password=?";
Object[] params={userCode,userPassword};
ResultSet rs = BaseDao.execute(connecton, sql, params, re, s);
try {
if(rs.next()){
user=new User();
user.setUserCode(rs.getString("user_code"));
user.setId((long) rs.getLong("id"));
user.setAddress(rs.getString("address"));
user.setBirthday(rs.getDate("birthday"));
user.setGender(rs.getInt("gender"));
user.setUserName(rs.getString("user_name"));
user.setUserPassword(rs.getString("user_password"));
}
BaseDao.closeResource(connecton,null,rs);
} catch (SQLException e) {
e.printStackTrace();
}
return user;
}
}
UserDao:
public interface UserDao {
//得到要登录的用户
public User getUser(Connection connecton, String useCode,String userCode);
}
UserServiceImpl :
public class UserServiceImpl implements UserService{
//业务层都调用dao层
private UserDao userDao;
public UserServiceImpl() {
userDao = new UserDaoImpl();
}
public User login(String useCode, String password) {
User use= null;
Connection connecton=null;
try {
connecton= BaseDao.getConnection();
use = userDao.getUser(connecton,useCode,password);
} catch (Exception e) {
e.printStackTrace();
} finally {
BaseDao.closeResource(connecton,null,null);
}
return use;
}
/*
@Test
public void test(){
UserServiceImpl us=new UserServiceImpl();
User admin = us.login("admin", "123");
System.out.println(admin.toString());
}
*/
}
UserService :
public interface UserService {
//用户登录
public User login(String userName,String password);
}
LoginServlet :
public class LoginServlet extends HttpServlet {
//service 控制层调业务层
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String userCode = req.getParameter("userCode");
String userPassword = req.getParameter("userPassword");
//调用业务层查数据
UserService user = new UserServiceImpl();
User login = user.login(userCode, userPassword);
if(login!=null){//查到有此人,放入session
req.getSession().setAttribute(Constants.SUCCESS_USER,login);
//登录成功到主页
resp.sendRedirect("jsp/frame.jsp");
}else{
//未查到
req.setAttribute("error","用户名或密码错误!");
req.getRequestDispatcher("login.jsp").forward(req,resp);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="4.0"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
metadata-complete="true">
<!--字符编码过滤器-->
<filter>
<filter-name>filter</filter-name>
<filter-class>com.henry.filter.ChacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>com.henry.servlet.user.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login.do</url-pattern>
</servlet-mapping>
</web-app>
运行:
标签:req,String,登录,管理系统,rs,smbms,user,login,public 来源: https://blog.csdn.net/weixin_44760375/article/details/120246464