首先创建项目结构:大体思路还是先写接口在写实现,添加逻辑层:

代码如下:

dao:(接口)

package com.bdqn.dao;public interface UserDao {import com.bdqn.pojo.User;import java.util.List;//查询所有的方法List selectAll();//根据id删除用户的方法int deleteUserById(int id);//根据id修改用户信息int updateUserById(int id,User user);//添加用户信息int insertUser(User user);//根据用户名查询信息User SelectUserByUserName(String name);}

dao(实现类:impl):

package com.bdqn.dao.impl;import com.bdqn.dao.UserDao;import com.bdqn.pojo.User;import com.bdqn.util.BaseDao;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;public class UserDaoImpl extends BaseDao implements UserDao {@Overridepublic List selectAll() {String sql="select * from user";ResultSet rs = this.executeQuery(sql, null);List userList=new ArrayList();try {while(rs.next()){User user=new User();user.setUserId(rs.getInt("userId"));user.setUserName(rs.getString("userName"));user.setUserPwd(rs.getString("userPwd"));//把User对象添加进集合userList.add(user);}} catch (SQLException e) {e.printStackTrace();}return userList;}@Overridepublic int deleteUserById(int id) {String sql="delete from User where userId=?";Object [] objects={id};return this.executeUpdate(sql,objects);}@Overridepublic int updateUserById(int id, User user) {String sql="UPDATEUSER SET userName=? ,userPwd=? WHERE userId=?";Object[] objects={user.getUserName(),user.getUserPwd(),id};return this.executeUpdate(sql,objects);}@Overridepublic int insertUser(User user) {String sql="insert into user(userName,userPwd) values(?,?)";Object [] objects={user.getUserName(),user.getUserPwd()};return this.executeUpdate(sql,objects);}@Overridepublic User SelectUserByUserName(String name) {String sql="SELECT * FROM USER WHERE userName=?";Object []objects={name};ResultSet rs = this.executeQuery(sql, objects);User user=null;try {//如果查询有此人if(rs.next()){user=new User();user.setUserId(rs.getInt("userId"));user.setUserName(rs.getString("userName"));user.setUserPwd(rs.getString("userPwd"));}} catch (SQLException e) {e.printStackTrace();}return user;}}

pojo:{写连接数据库的列}+get/set方法:

service:(接口):

package com.bdqn.service;import com.bdqn.pojo.User;import java.util.List;public interface UserService {//查询所有的方法List selectAll();//根据id删除用户的方法boolean deleteUserById(int id);//根据id修改用户信息boolean updateUserById(int id,User user);//添加用户信息boolean insertUser(User user);//根据用户名查询信息User SelectUserByUserName(String name);}

service:(实现impl):

package com.bdqn.service.impl;import com.bdqn.dao.UserDao;import com.bdqn.dao.impl.UserDaoImpl;import com.bdqn.pojo.User;import com.bdqn.service.UserService;import java.util.List;public class UserServiceImpl implements UserService {//创建数据访问层的对象UserDao ud=new UserDaoImpl();@Overridepublic List selectAll() {return ud.selectAll();}@Overridepublic boolean deleteUserById(int id) {int count = ud.deleteUserById(id);if(count>0){//删除成功返回truereturn true;}return false;}@Overridepublic boolean updateUserById(int id, User user) {User user1 = ud.SelectUserByUserName(user.getUserName());//查询数据库中是否存在这个人if(user1==null){//如果数据库中没有这个人的信息可以修改int count = ud.updateUserById(id,user);if(count>0){returntrue;}}return false;}@Overridepublic boolean insertUser(User user) {User user1 = ud.SelectUserByUserName(user.getUserName());//查询数据库中是否存在这个人if(user1==null){//如果数据库中没有这个人的信息可以添加int count = ud.insertUser(user);if(count>0){returntrue;}}return false;}@Overridepublic User SelectUserByUserName(String name) {return ud.SelectUserByUserName(name);}}

servlet:

package com.bdqn.servlet;import com.bdqn.pojo.User;import com.bdqn.service.UserService;import com.bdqn.service.impl.UserServiceImpl;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import java.io.IOException;import java.io.PrintWriter;import java.util.List;@WebServlet(name = "UserServlet",urlPatterns = "/UserServlet")public class UserServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {this.doPost(req,resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {String answer=req.getParameter("answer");//创建业务逻辑层的对象UserService us=new UserServiceImpl();//创建sessionHttpSession session=req.getSession();System.out.println(answer+"11111");System.out.println(req.getParameter("ids")+"32323232323");//第一次访问 展示所有信息if(answer==null){//获取查询到的信息List userList = us.selectAll();//把查询到的集合存入session在show.jsp中取出session.setAttribute("userList",userList);resp.sendRedirect("show.jsp");}else if(answer.equals("add")){//添加req.setCharacterEncoding("utf-8");//获取页面的用户名和密码String name= req.getParameter("userName");String pwd= req.getParameter("userPwd");User user=new User();user.setUserName(name);user.setUserPwd(pwd);boolean flag = us.insertUser(user);if(flag){//添加成功,重新给session赋值List userList = us.selectAll();session.setAttribute("userList",userList);resp.setContentType("text/html;charset=utf-8");PrintWriter pw=resp.getWriter();pw.println("alert('添加成功');location.href='show.jsp'");pw.flush();pw.close();}else{resp.setContentType("text/html;charset=utf-8");PrintWriter pw=resp.getWriter();pw.println("alert('添加失败');location.href='show.jsp'");pw.flush();pw.close();}}else if(answer.equals("delete")){//删除//接收从show页面传的参数idreq.setCharacterEncoding("utf-8");String idStr = req.getParameter("ids");Integer id = Integer.valueOf(idStr);boolean flag = us.deleteUserById(id);if(flag){//删除成功,重新给session赋值List userList = us.selectAll();session.setAttribute("userList",userList);resp.setContentType("text/html;charset=utf-8");PrintWriter pw=resp.getWriter();pw.println("alert('删除成功');location.href='show.jsp'");pw.flush();pw.close();}else{resp.setContentType("text/html;charset=utf-8");PrintWriter pw=resp.getWriter();pw.println("alert('删除失败');location.href='show.jsp'");pw.flush();pw.close();}}else if(answer.equals("update")){//修改req.setCharacterEncoding("utf-8");//获取页面的用户名和密码String name= req.getParameter("userName");String pwd= req.getParameter("userPwd");int id = (int)session.getAttribute("id");//创建User对象User user=new User();user.setUserName(name);user.setUserPwd(pwd);//调用修改的方法boolean flag = us.updateUserById(id, user);if(flag){//修改成功,重新给session赋值List userList = us.selectAll();session.setAttribute("userList",userList);resp.setContentType("text/html;charset=utf-8");PrintWriter pw=resp.getWriter();pw.println("alert('修改成功');location.href='show.jsp'");pw.flush();pw.close();}else{resp.setContentType("text/html;charset=utf-8");PrintWriter pw=resp.getWriter();pw.println("alert('修改失败');location.href='show.jsp'");pw.flush();pw.close();}}}}

util:{连接数据库的工具类}

再写页码:(web下):

显示全部(show.jsp):

用户信息展示<%List userList =(List)session.getAttribute("userList");%>

用户信息列表

编号昵称密码操作
">修改 ">删除

add.jsp:

Title

昵称

密码

update.jsp:

Title

修改后的名称

修改后的密码