SMBMS超市订单管理系统-笔记

000000
数据库:
SMBMS超市订单管理系统-笔记_第1张图片
SMBMS超市订单管理系统-笔记_第2张图片
SMBMS超市订单管理系统-笔记_第3张图片
BaseDao.java

package cn.smbms.dao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

/**
 * 操作数据库的基类--静态类
 * @author Administrator
 *
 */
public class BaseDao {
     
	
	static{
     //静态代码块,在类加载的时候执行
		init();
	}
	
	private static String driver;
	private static String url;
	private static String user;
	private static String password;
	
	//初始化连接参数,从配置文件里获得
	public static void init(){
     
		Properties params=new Properties();
		String configFile = "database.properties";
		InputStream is=BaseDao.class.getClassLoader().getResourceAsStream(configFile);
		try {
     
			params.load(is);
		} catch (IOException e) {
     
			e.printStackTrace();
		}
		driver=params.getProperty("driver");
		url=params.getProperty("url");
		user=params.getProperty("user");
		password=params.getProperty("password");

	}   
	
	
	/**
	 * 获取数据库连接
	 * @return
	 */
	public static Connection getConnection(){
     
		Connection connection = null;
		try {
     
			Class.forName(driver);
			connection = DriverManager.getConnection(url, user, password);
		} catch (Exception e) {
     
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return connection;
	}
	/**
	 * 查询操作
	 * @param connection
	 * @param pstm
	 * @param rs
	 * @param sql
	 * @param params
	 * @return
	 */
	public static ResultSet execute(Connection connection,PreparedStatement pstm,ResultSet rs,
			String sql,Object[] params) throws Exception{
     
		pstm = connection.prepareStatement(sql);
		for(int i = 0; i < params.length; i++){
     
			pstm.setObject(i+1, params[i]);
		}
		rs = pstm.executeQuery();
		return rs;
	}
	/**
	 * 更新操作
	 * @param connection
	 * @param pstm
	 * @param sql
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public static int execute(Connection connection,PreparedStatement pstm,
			String sql,Object[] params) throws Exception{
     
		int updateRows = 0;
		pstm = connection.prepareStatement(sql);
		for(int i = 0; i < params.length; i++){
     
			pstm.setObject(i+1, params[i]);
		}
		updateRows = pstm.executeUpdate();
		return updateRows;
	}
	
	/**
	 * 释放资源
	 * @param connection
	 * @param pstm
	 * @param rs
	 * @return
	 */
	public static boolean closeResource(Connection connection,PreparedStatement pstm,ResultSet rs){
     
		boolean flag = true;
		if(rs != null){
     
			try {
     
				rs.close();
				rs = null;//GC回收
			} catch (SQLException e) {
     
				// TODO Auto-generated catch block
				e.printStackTrace();
				flag = false;
			}
		}
		if(pstm != null){
     
			try {
     
				pstm.close();
				pstm = null;//GC回收
			} catch (SQLException e) {
     
				// TODO Auto-generated catch block
				e.printStackTrace();
				flag = false;
			}
		}
		if(connection != null){
     
			try {
     
				connection.close();
				connection = null;//GC回收
			} catch (SQLException e) {
     
				// TODO Auto-generated catch block
				e.printStackTrace();
				flag = false;
			}
		}
		
		return flag;
	}

}

SMBMS超市订单管理系统-笔记_第4张图片
在这里插入图片描述
SMBMS超市订单管理系统-笔记_第5张图片
SMBMS超市订单管理系统-笔记_第6张图片

	@Override
	public User getLoginUser(Connection connection, String userCode)
			throws Exception {
     
		// TODO Auto-generated method stub
		PreparedStatement pstm = null;
		ResultSet rs = null;
		User user = null;
		if(null != connection){
     
			String sql = "select * from smbms_user where userCode=?";
			Object[] params = {
     userCode};
			rs = BaseDao.execute(connection, pstm, rs, sql, params);
			if(rs.next()){
     
				user = new User();
				user.setId(rs.getInt("id"));
				user.setUserCode(rs.getString("userCode"));
				user.setUserName(rs.getString("userName"));
				user.setUserPassword(rs.getString("userPassword"));
				user.setGender(rs.getInt("gender"));
				user.setBirthday(rs.getDate("birthday"));
				user.setPhone(rs.getString("phone"));
				user.setAddress(rs.getString("address"));
				user.setUserRole(rs.getInt("userRole"));
				user.setCreatedBy(rs.getInt("createdBy"));
				user.setCreationDate(rs.getTimestamp("creationDate"));
				user.setModifyBy(rs.getInt("modifyBy"));
				user.setModifyDate(rs.getTimestamp("modifyDate"));
			}
			BaseDao.closeResource(null, pstm, rs);
		}
		return user;
	}

SMBMS超市订单管理系统-笔记_第7张图片

public class UserServiceImpl implements UserService{
     
@Override
	public User login(String userCode, String userPassword) {
     
		// TODO Auto-generated method stub
		Connection connection = null;
		User user = null;
		try {
     
			connection = BaseDao.getConnection();
			user = userDao.getLoginUser(connection, userCode);
		} catch (Exception e) {
     
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
     
			BaseDao.closeResource(connection, null, null);
		}
		
		//匹配密码
		if(null != user){
     
			if(!user.getUserPassword().equals(userPassword))
				user = null;
		}
		
		return user;
	}

}

在这里插入图片描述

public class LoginServlet extends HttpServlet {
     


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
     

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
     

		System.out.println("login ============ " );
		//获取用户名和密码
		String userCode = request.getParameter("userCode");
		String userPassword = request.getParameter("userPassword");
		//调用service方法,进行用户匹配
		UserService userService = new UserServiceImpl();
		User user = userService.login(userCode,userPassword);
		if(null != user){
     //登录成功
			//放入session
			request.getSession().setAttribute(Constants.USER_SESSION, user);
			//页面跳转(frame.jsp)
			response.sendRedirect("jsp/frame.jsp");
		}else{
     
			//页面跳转(login.jsp)带出提示信息--转发
			request.setAttribute("error", "用户名或密码不正确");
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}

	}


}

SMBMS超市订单管理系统-笔记_第8张图片

    <servlet>
    <description>This is the description of my J2EE componentdescription>
    <display-name>This is the display name of my J2EE componentdisplay-name>
    <servlet-name>LoginServletservlet-name>
    <servlet-class>cn.smbms.servlet.user.LoginServletservlet-class>
  servlet>
  <servlet-mapping>
    <servlet-name>LoginServletservlet-name>
    <url-pattern>/login.dourl-pattern>
  servlet-mapping>

在这里插入图片描述
SMBMS超市订单管理系统-笔记_第9张图片

public class LogoutServlet extends HttpServlet {
     

	public LogoutServlet() {
     
		super();
	}

	public void destroy() {
     
		super.destroy();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
     
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
     
		//清除session
		request.getSession().removeAttribute(Constants.USER_SESSION);
		response.sendRedirect(request.getContextPath()+"/login.jsp");
	}

注册xml

  <servlet>
    <description>This is the description of my J2EE componentdescription>
    <display-name>This is the display name of my J2EE componentdisplay-name>
    <servlet-name>LogoutServletservlet-name>
    <servlet-class>cn.smbms.servlet.user.LogoutServletservlet-class>
  servlet>
    <servlet-mapping>
    <servlet-name>LogoutServletservlet-name>
    <url-pattern>/jsp/logout.dourl-pattern>
  servlet-mapping>

在这里插入图片描述

public class SysFilter implements Filter {
     

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
     

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
     

		System.out.println("SysFilter doFilter()===========");
		HttpServletRequest rq = (HttpServletRequest)request;
		HttpServletResponse rp = (HttpServletResponse)response;
		User userSession = (User)rq.getSession().getAttribute("userSession");
		if(null == userSession){
     
			rp.sendRedirect("/SMBMS/error.jsp");
		}else{
     
			chain.doFilter(request, response);
		}
	}

	@Override
	public void destroy() {
     

	}

}
  <filter>
  	<filter-name>SysFilterfilter-name>
  	<filter-class>cn.smbms.filter.SysFilterfilter-class>
  filter>
  <filter-mapping>
  	<filter-name>SysFilterfilter-name>
  	<url-pattern>/jsp/*url-pattern>
  filter-mapping>

测试、登录、注销、权限,都要保证OK
SMBMS超市订单管理系统-笔记_第10张图片

<li><a href="${pageContext.request.contextPath }/jsp/pwdmodify.jsp">密码修改</a></li>

SMBMS超市订单管理系统-笔记_第11张图片
SMBMS超市订单管理系统-笔记_第12张图片
SMBMS超市订单管理系统-笔记_第13张图片

SMBMS超市订单管理系统-笔记_第14张图片
3、

public int updatePwd(Connection connection, int id, String pwd)throws Exception;

4、

	@Override
	public int updatePwd(Connection connection, int id, String pwd)
			throws Exception {
     
		// TODO Auto-generated method stub
		int flag = 0;
		PreparedStatement pstm = null;
		if(connection != null){
     
			String sql = "update smbms_user set userPassword= ? where id = ?";
			Object[] params = {
     pwd,id};
			flag = BaseDao.execute(connection, pstm, sql, params);
			BaseDao.closeResource(null, pstm, null);
		}
		return flag;
	}

SMBMS超市订单管理系统-笔记_第15张图片
5、

	/**
	 * 根据userId修改密码
	 * @param id
	 * @param pwd
	 * @return
	 */
	public boolean updatePwd(int id, String pwd);

6、

	@Override
	public boolean updatePwd(int id, String pwd) {
     
		boolean flag = false;
		Connection connection = null;
		try{
     
			connection = BaseDao.getConnection();
			if(userDao.updatePwd(connection,id,pwd) > 0)
				flag = true;
		}catch (Exception e) {
     
			e.printStackTrace();
		}finally{
     
			BaseDao.closeResource(connection, null, null);
		}
		return flag;
	}

SMBMS超市订单管理系统-笔记_第16张图片
SMBMS超市订单管理系统-笔记_第17张图片
8、测试
SMBMS超市订单管理系统-笔记_第18张图片
2、

	private void updatePwd(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
     
		
		Object o = request.getSession().getAttribute(Constants.USER_SESSION);
		String newpassword = request.getParameter("newpassword");
		boolean flag = false;
		if(o != null && !StringUtils.isNullOrEmpty(newpassword)){
     
			UserService userService = new UserServiceImpl();
			flag = userService.updatePwd(((User)o).getId(),newpassword);
			if(flag){
     
				request.setAttribute(Constants.SYS_MESSAGE, "修改密码成功,请退出并使用新密码重新登录!");
				request.getSession().removeAttribute(Constants.USER_SESSION);//session注销
			}else{
     
				request.setAttribute(Constants.SYS_MESSAGE, "修改密码失败!");
			}
		}else{
     
			request.setAttribute(Constants.SYS_MESSAGE, "修改密码失败!");
		}
		request.getRequestDispatcher("pwdmodify.jsp").forward(request, response);
	}

SMBMS超市订单管理系统-笔记_第19张图片
SMBMS超市订单管理系统-笔记_第20张图片
SMBMS超市订单管理系统-笔记_第21张图片
SMBMS超市订单管理系统-笔记_第22张图片
SMBMS超市订单管理系统-笔记_第23张图片
SMBMS超市订单管理系统-笔记_第24张图片
SMBMS超市订单管理系统-笔记_第25张图片
SMBMS超市订单管理系统-笔记_第26张图片
2、
SMBMS超市订单管理系统-笔记_第27张图片
SMBMS超市订单管理系统-笔记_第28张图片

SMBMS超市订单管理系统-笔记_第29张图片

你可能感兴趣的:(JavaWeb,java,mysql)