思考 : 需要创建实体类吗? 需要创建表吗
|----User 存在、不需要创建了!表同理、也不需要了
1.设计dao接口
package cn.javabs.usermanager.dao;import cn.javabs.usermanager.entity.User;/*** 用户的dao接口的设计* @author Mryang**/
public interface UserDao {/*** 用户登录功能* @param username 参数 为用户名* @param password 参数 为密码* @return user*/User login(String username , String password);/*** 用户注册* @param user 参数是 用户对象!!!!!!!* @return 记录条数 受影响的行数*/int register(User user);}
2.UserDaoImpl 实现类
package cn.javabs.usermanager.dao.impl;import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;import cn.javabs.usermanager.dao.UserDao;
import cn.javabs.usermanager.entity.User;
import cn.javabs.usermanager.exception.UserLoginException;
import cn.javabs.usermanager.exception.UserRegisterException;
import cn.javabs.usermanager.util.JdbcUtil;
/*** userdao的实现类* @author Mryang* 调用 jdbc**静态方法优于构造方法先执行**/
public class UserDaoImpl implements UserDao {@Overridepublic User login(String username, String password) {try {Connection con = JdbcUtil.getConnection();// 通过con 链接创建一个执行SQL语句的对象StatementStatement st = con.createStatement();ResultSet rs = st.executeQuery("select * from user where username = '"+username+"' and password = '"+password+"';");if(rs.next()){// 有数据才执行以下// 类型 对象 = 。。。User user = new User();// rs.getString();//columnIndex 数据库中表的列号 从0计数
// rs.getString(columnLabel)//columnLabel 数据库中表的列名// String name = rs.getString("username");//name admin
// String pwd = rs.getString("password");//name admin
// System.out.println("name:"+name);
// System.out.println("pwd:"+pwd);
// user.setUsername(name);// 封装。注入user.setUsername(rs.getString("username"));user.setPassword(rs.getString("password"));System.out.println("userDao中的user的内容是"+ user);return user;// 已经有了 username和password}else{//没数据才执行以下return null;}} catch (SQLException e) {throw new UserLoginException();}}@Overridepublic int register(User user) {try {// 获取链接Connection conn = JdbcUtil.getConnection();// 创建执行SQL语句的对象Statement st = conn.createStatement();int row = st.executeUpdate("insert into user(username,password,sex) values ('"+user.getUsername()+"','"+user.getPassword()+"','"+user.getSex()+"')");return row;} catch (SQLException e) {throw new UserRegisterException();}}}
3.service接口设计
package cn.javabs.usermanager.service;import cn.javabs.usermanager.entity.User;public interface UserService {/*** 用户登录功能* @param username 参数 为用户名* @param password 参数 为密码* @return user*/User userLogin(String username , String password);/*** 用户注册* @param user* @return*/int userRegist(User user);}
serviceImpl
package cn.javabs.usermanager.service.impl;import cn.javabs.usermanager.dao.UserDao;
import cn.javabs.usermanager.dao.impl.UserDaoImpl;
import cn.javabs.usermanager.entity.User;
import cn.javabs.usermanager.service.UserService;public class UserServiceImpl implements UserService {// 采用多态的形式进行实例化dao UserDao dao = new UserDaoImpl();@Overridepublic User userLogin(String username, String password) {//dao代表的是UserDao 这个接口
// .login 用这个接口中的用户登录功能 并且传了两个参数给你return dao.login(username, password);}@Overridepublic int userRegist(User user) {return dao.register(user);}}
UserServlet
package cn.javabs.usermanager.web.servlet;import java.io.IOException;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import cn.javabs.usermanager.entity.User;
import cn.javabs.usermanager.service.UserService;
import cn.javabs.usermanager.service.impl.UserServiceImpl;public class UserLoginServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {/** 1 set encoding*/response.setContentType("text/html");response.setCharacterEncoding("utf-8");request.setCharacterEncoding("utf-8");/** 2. get 前台 的 参数*/String username = request.getParameter("username");String password = request.getParameter("password");/** 3. 将获取到的用户名和密码传递给 userService!* 所以 得有 userService 没有 怎么 办? 实例化 就有了 */UserService userService = new UserServiceImpl();User u = userService.userLogin(username, password);//u可能为前面传递过来的user或是nullif(u == null){response.getWriter().write("您的用户名或密码有误,请检查!");response.setHeader("Refresh", "5;Url="+ request.getContextPath());}else{request.setAttribute("mark", "用户登录成功!");request.getRequestDispatcher("/message.jsp").forward(request, response);}}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet( request, response);}}
register.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><base href="<%=basePath%>"><title>注册</title></head><body><center><form action="<%=basePath%>servlet/UserRegistServlet" method="post" ><table border="1px" width="438px" ><tr align="center"><td colspan="2"><a href="<%=basePath%>login.jsp">如已有用户,点击去登录</a></td></tr><tr align="center"><td>用户名</td><td><input type="text" name="username" ></td></tr><tr align="center"><td>密码</td><td><input type="password" name="password" ></td></tr><tr align="center"><td>性别</td><td>男<input type="radio" name="sex" value="男" checked="checked" >女<input type="radio" name="sex" value="女" ></td></tr><tr align="center"><td colspan="2" ><input type="submit" value="免费注册" ><input type="reset" value="重置内容" ></td></tr></table> </form></center></body>
</html>