1.导入项目需要的依赖,分层

注意:如果你的数据库是5.5的版本,依赖要用低版本的,高版本不稳定,新增的内容不识别,会报各种各样奇葩的错误
2.创建实体类
它的属性要和数据库字段对应
package com.oa.entity;public class UserInfo {private int id;private String user_name;private String user_password;private String user_sex;private String user_department;private String user_role;private String create_date;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getUser_name() {return user_name;}public void setUser_name(String user_name) {this.user_name = user_name;}public String getUser_password() {return user_password;}public void setUser_password(String user_password) {this.user_password = user_password;}public String getUser_sex() {return user_sex;}public void setUser_sex(String user_sex) {this.user_sex = user_sex;}public String getUser_department() {return user_department;}public void setUser_department(String user_department) {this.user_department = user_department;}public String getUser_role() {return user_role;}public void setUser_role(String user_role) {this.user_role = user_role;}public String getCreate_date() {return create_date;}public void setCreate_date(String create_date) {this.create_date = create_date;}public UserInfo() {super();// TODO Auto-generated constructor stub}}
3.DAO层
接口,要声明实体类的方法(抽象方法)
package com.oa.dao;import com.oa.entity.UserInfo;public interface UserDao {public UserInfo login(UserInfo user);
}
4.DAOImpl层
是DAO层接口的实现类,写具体的的功能实现
package com.oa.daoImpl;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;import com.oa.dao.UserDao;
import com.oa.entity.UserInfo;
import com.oa.tool.*;public class UserDaoImpl implements UserDao {@Overridepublic UserInfo login(UserInfo user) {Connection con =null;PreparedStatement ps =null;ResultSet rs=null;UserInfo users=new UserInfo();try{//1.连接数据库con=C3p0Util.getConnection();System.out.print(con);//测试数据库是否连接成功//2.查询语句String sql ="select * from t_user where user_name =? and user_password=?";ps=con.prepareStatement(sql);//预编译//3.给问号处设置值ps.setString(1, user.getUser_name());ps.setString(2, user.getUser_password());//4.执行查询语句rs=ps.executeQuery();if(rs.next()){//5.从数据库中获取值,设置到实体类的setter方法中users.setUser_name(rs.getString("user_name"));users.setUser_password(rs.getString("user_password"));System.out.print(users+"sssssssssssssssssssssss");//测试数据}}catch(Exception e){e.printStackTrace();}
//6.将实体类返回,以便service层接收return users;}}
4.Service层
用于联系数据库和Servlet,接收DAO层传来的返回值,Servlet调用service层获取
package com.oa.service;
import com.oa.entity.UserInfo;public interface UserService {public UserInfo login(UserInfo user);
}
5.serviceImpl层
返回实体类
package com.oa.serviceImpl;import com.oa.dao.UserDao;
import com.oa.daoImpl.UserDaoImpl;
import com.oa.entity.UserInfo;
import com.oa.service.UserService;public class UserServiceImpl implements UserService {//调用dao层private UserDao userdao =new UserDaoImpl();@Overridepublic UserInfo login(UserInfo user) {// TODO Auto-generated method stubreturn userdao.login(user);}}
6.Servlet
连接界面和服务层的
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {UserInfo user =new UserInfo();//获取Login.html输入的用户名和密码String name=request.getParameter("users.userName");String password=request.getParameter("users.userPassword");//测试数据System.out.print("name:"+name+"password"+password);//将输入的数据设置到实体类中user.setUser_name(name);user.setUser_password(password);//引入数据交互层UserDao dao = new UserDaoImpl();UserInfo us=dao.login(user);//测试返回的值System.out.println(us+"hhhhhhhhhhhhhhhhhhhhhhhhhh");//设置日期Date date =new Date();SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");String str = sdf.format(date);Calendar cal=Calendar.getInstance(); int h=cal.get(Calendar.HOUR_OF_DAY); int mi=cal.get(Calendar.MINUTE); String str1=h+":"+mi;HttpSession session =request.getSession();session.setAttribute("showdate", str);session.setAttribute("showtime", str1);session.setAttribute("showname", name);session.setAttribute("showpassword", password);if(us!=null){request.setAttribute("info", "登陆成功");request.getRequestDispatcher("mainframe.jsp").forward(request, response);}else{request.setAttribute("info", "登录失败");request.getRequestDispatcher("login.jsp").forward(request, response);}}
7.JSP显示界面
%@ page language="java" contentType="text/html; charset=utf-8"pageEncoding="utf-8"%><%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head><title>Office Anywhere 2009 网络智能办公系统</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><link rel="stylesheet"href="styles/style.css" type="text/css;charset=gb2312" /><link rel="stylesheet" type="text/css"href="styles/index.css"><script type="text/javascript"src="js/index.js">
</script></head><body><div class="login_div"><form action="UserLoginServlet" method="post" name="myform" id="form"onSubmit="return check()"><div align="center"><b>用户名</b><input class="text" onMouseOver="this.focus()"onFocus="this.select()" maxlength="20" value=""name="users.userName" /> <b>密码</b><input class="text" onMouseOver="this.focus()"onFocus="this.select()" type="password" maxlength="30"name="users.userPassword" /> <input name="submit" type="submit" class="newButton" value=" 登 录 " /></div></form></div></body>
</html>
其他:用到数据库连接池C3p0工具类,过滤器是我用来处理编码的,可以不用



















