jsp+servlet学子商城项目--servlet、dao层的各项练习

本文档介绍了使用jsp和servlet构建学子商城项目的过程,涉及Login模块的登录功能实现,包括登录传值和SELECT查询。同时讲解了DAO层的更新操作,以及如何在DAO层处理带状态的更改和返回List数组的方法。
摘要由CSDN通过智能技术生成

Login的登录项目完成传值,select语句索引

UserDao.java

/**
	 * 根据用户名查询
	 * @param name:表示用户名
	 * @return如果用户名存在,封装成对象返回
	 * 否则返回null;
	 */
	public User selectByName(String name){
		User user = null;
		Connection conn =C3P0Util.getConn();
		String sql="select * from tb_user where name=?";
		PreparedStatement pstmt;
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, name);
			ResultSet rs = pstmt.executeQuery();
			if(rs.next()){
				user= new User();
				user.setId(rs.getInt(1));
				user.setName(rs.getString(2));
				user.setPassword(rs.getString(3));
				user.setEmail(rs.getString(4));
				user.setImage(rs.getString(5));
				user.setPhone(rs.getString(6));
				user.setGender(rs.getInt(7));
				}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return user;
	
	}

UserService.java

/**
	 * 登录
	 * @param name:用户名
	 * @param pw:密码
	 * @return:如果用户错误,抛出异常NameNotExistException
	 * 如果密码错误,抛出异常:PasswordErrorException
	 * 如果正确,返回该对象
	 */
	public User login(String name,String pw){
		User user =userDao.selectByName(name);
		if(user==null){
			throw new NameNotExistException();
		}else{
			if(user.getPassword().equals(DigestUtils.md5Hex(pw))){
				return user;
			}else{
				throw new PasswordErrorException();
			}
		}
		
	}

LoginServlet.java

package cn.tedu.web;
/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		String  name =request.getParameter("lname");
		String pw=request.getParameter("lwd");
		try{
		User user =new UserService().login(name,pw);
		
		request.getSession().setAttribute("user", user);
		response.getWriter().print(new Gson().toJson(new ResponseResult(1,"登陆成功")));
		
		}catch(Exception e){
			response.getWriter().print(new Gson().toJson(new ResponseResult(0,e.getMessage())));
		}
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}


修改个人信息—update
UserDao.java

//修改个人信息
	public void update (User user){
		Connection conn =C3P0Util.getConn();
		String sql="update tb_user set name=?,email=?,phone=?,gender=?,image=? where id=?";
		PreparedStatement pstmt;
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, user.getName());
			pstmt.setString(2, user.getEmail());
			pstmt.setString(3, user.getPhone());
			pstmt.setInt(4, user.getGender());
			pstmt.setString(5, user.getImage());
			pstmt.setInt(6, user.getId());
			pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			//return null;
		}
	}

UserService.java

User user1=userDao.selectByName(user.getName());
		//根据用户名查询,如果null,可以进行修修改
		if(user1!=null) {
			User user2=userDao.selectById(user.getId());
			//如果结果不为null,没有修改用户名,可以进行修改,否则用户名重复
			if(!user.getName().equals(user2.getName())) {
				throw new NameHasExistException();
			}			
		}
		userDao.update(user);
		return userDao.selectById(user.getId());

UpdateUserServlet.java

package cn.tedu.web;
import java.io.IOException;
/**
 * Servlet implementation class UpdateUserServlet
 */
@WebServlet("/web/UpdateUserServlet")
@MultipartConfig
public class UpdateUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//1.图片上传
		Part p =request.getPart("iconPic");
		String fileName=p.getSubmittedFileName();
		if(fileName==null||fileName.equals("")){
			fileName=request.getParameter("image");
		}else{
			//之后要进行不同页面的数据分析后才可以进行接下来的分析
			//d:/....\teduStore
			//返回web应用的真实路径
			String path=request.getServletContext().getRealPath("/");
			/*p.write(path+"/images/"+fileName);*/
			p.write(path+"images"+fileName);
			System.out.println(path+"images"+fileName);
		}
		//修改个人信息
		HttpSession session =request.getSession();
		User user=(User)session.getAttribute("user");
		int id=user.getId();
		String name=request.getParameter("name");
		String phone=request.getParameter("phone");
		String email=request.getParameter("email");
		int gender =Integer.parseInt(request.getParameter("gender"));
		
		user = new User();
		user.setEmail(email);
		user.setGender(gender);
		user.setImage(fileName);
		user.setPhone(phone);
		user.setName(name);
		user.setId(id);
		//返回一个数据集合,对应的Void表示一个类的名字,表示没有返回的值
		ResponseResult<Void> rr;
		try{
		User user1 =new UserService().updateUser(user);
		
		session.setAttribute("user", user1);
		rr=new ResponseResult<>(1,"ok");
		}catch(Exception e){
			rr=new ResponseResult<>(0,e.getMessage());
		}
		response.getWriter().print(new Gson().toJson(rr));
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
}


带有状态的更改的dao层

OrderDao.java

/**
	 * 将商品加入到item里面
	 * @param item
	 */
	public void insertOrderitem(OrderItem item){
		Connection conn=null;
		try {
			conn=C3P0Util.getConn();
			String sql="insert into tb_orderitem (goodsid,image ,title,price,count, "
					+ "paymentstatus,orderstatus,orderid) values(?,?,?,?,?,?,?,?)";
			PreparedStatement pre=conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
			pre.setString(1, item.getGoodsid());
			pre.setString(2, item.getImage());
			pre.setString(3, item.getTitle());
			pre.setInt(4, item.getPrice());
			pre.setInt(5, item.getCount());
			pre.setInt(6, 0);
			pre.setInt(7, 0);
			pre.setInt(8, item.getOrderid());
			pre.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

带有list数组返回值的Dao层–select

public List<Order> selectOrder(int uid){
		List<Order> list =new ArrayList<>();
		Connection conn=null;
		PreparedStatement pre=null;
		
		try {
			conn=C3P0Util.getConn();
			String sql="select * from tb_order where uid=?";
			pre=conn.prepareStatement(sql);
			pre.setInt(1, uid);
			ResultSet rs=pre.executeQuery();
			while(rs.next()){
				Order order=new Order();
				order.setId(rs.getInt(1));
				order.setTradetime(rs.getDate(2));
				order.setUid(rs.getInt(3));
				list.add(order);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return list;
		
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值