JDBC实现简单的增删改查(servlet实现)——登录
登录实现思路:
登录页面 发送 登录请求
— LoginServlet
–> EmpService
–>EmpDao
–>访问数据库
—>数据库的数据返回
前台jsp页面:
<%-- Created by IntelliJ IDEA. --%><%@ page contentType=\"text/html;charset=UTF-8\" language=\"java\" %><html><head><title></title></head><body><!-- {pageContext.request.contextPath}/ 获取当前工程的绝对路径 EmpServlet?method=login:交给 EmpServlet处理 提交名:login--><form action=\"${pageContext.request.contextPath}/EmpServlet?method=login\" method=\"post\"><table><tr><td>账号: <input name=\"name\" type=\"text\"></td></tr><tr><td>密码: <input name=\"password\" type=\"password\"></td></tr><tr><td><input type=\"submit\" value=\"登录\"></td></tr></table></form></body></html>
Empservlet:
@WebServlet(\"/EmpServlet\")public class EmpServlet extends HttpServlet {//员工业务层EmpService empService = new EmpServiceImp();protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request,response);}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request.setCharacterEncoding(\"UTF-8\"); //需要放到其他代码之上 (编码格式 设置提交的中文 )//获取前台输入的账号密码String name = request.getParameter(\"name\");String password = request.getParameter(\"password\");//调用service层的登录方法EmpService empService = new EmpServiceImp();Emp emp = empService.login(name,password);//后台检查是否拿到emp数据System.out.println(\"emp----------\"+emp);//用户信息存入sessionHttpSession session=request.getSession();request.getSession().setAttribute(\"emp\",emp);// 登录后直接查所有 list(request,response);
Empservice:
package com.hp.service;import com.hp.bean.Emp;import java.util.List;public interface EmpService {//登录方法Emp login(String name, String password);}
Empserviceimpl:
package com.hp.service.imp;import com.hp.bean.Emp;import com.hp.dao.EmpDao;import com.hp.dao.imp.EmpDaoImp;import com.hp.service.EmpService;import java.util.List;public class EmpServiceImp implements EmpService {EmpDao empDao = new EmpDaoImp();@Overridepublic Emp login(String name, String password) {return empDao.login(name,password);//调用dao层的实现方法}}
EmpDao:
package com.hp.dao;import com.hp.bean.Emp;import java.util.List;public interface EmpDao {Emp login(String name, String password);}
EmpDaoImp:
package com.hp.dao.imp;import cn.hutool.core.util.StrUtil;import com.hp.bean.Dept;import com.hp.bean.Emp;import com.hp.dao.EmpDao;import com.hp.utils.DBHelper;import java.sql.*;import java.util.ArrayList;import java.util.List;public class EmpDaoImp implements EmpDao {DBHelper helper = new DBHelper();@Overridepublic Emp login(String name, String password) {//建立数据库连接Connection conn = helper.getConnetion();Emp emp = new Emp();try {//预编译sql语句PreparedStatement ps = conn.prepareStatement(\"select * from emp where name=? and password=?\");//给占位符赋值ps.setString(1,name);ps.setString(2,password);//执行查询ResultSet rs = ps.executeQuery();//遍历拿到所有值if(rs.next()){emp.setId(rs.getInt(\"id\"));emp.setName(rs.getString(\"name\"));emp.setBirthday(rs.getString(\"birthday\"));emp.setSex(rs.getString(\"sex\"));}return emp;} catch (SQLException e) {e.printStackTrace();}return null;}}