首先创建项目结构:大体思路还是先写接口在写实现,添加逻辑层:
代码如下:
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 {
@Override
public 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;
}
@Override
public int deleteUserById(int id) {
String sql="delete from User where userId=?";
Object [] objects={id};
return this.executeUpdate(sql,objects);
}
@Override
public int updateUserById(int id, User user) {
String sql="UPDATE USER SET userName=? ,userPwd=? WHERE userId=?";
Object [] objects={user.getUserName(),user.getUserPwd(),id};
return this.executeUpdate(sql,objects);
}
@Override
public int insertUser(User user) {
String sql="insert into user(userName,userPwd) values(?,?)";
Object [] objects={user.getUserName(),user.getUserPwd()};
return this.executeUpdate(sql,objects);
}
@Override
public 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();
@Override
public List selectAll() {
return ud.selectAll();
}
@Override
public boolean deleteUserById(int id) {
int count = ud.deleteUserById(id);
if(count>0){
//删除成功 返回true
return true;
}
return false;
}
@Override
public boolean updateUserById(int id, User user) {
User user1 = ud.SelectUserByUserName(user.getUserName());//查询数据库中是否存在这个人
if(user1==null){
//如果数据库中没有这个人的信息 可以修改
int count = ud.updateUserById(id,user);
if(count>0){
return true;
}
}
return false;
}
@Override
public boolean insertUser(User user) {
User user1 = ud.SelectUserByUserName(user.getUserName());//查询数据库中是否存在这个人
if(user1==null){
//如果数据库中没有这个人的信息 可以添加
int count = ud.insertUser(user);
if(count>0){
return true;
}
}
return false;
}
@Override
public 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 {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String answer=req.getParameter("answer");
//创建业务逻辑层的对象
UserService us=new UserServiceImpl();
//创建session
HttpSession 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("");
pw.flush();
pw.close();
}else{
resp.setContentType("text/html;charset=utf-8");
PrintWriter pw=resp.getWriter();
pw.println("");
pw.flush();
pw.close();
}
}else if(answer.equals("delete")){//删除
//接收从show页面传的参数id
req.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("");
pw.flush();
pw.close();
}else{
resp.setContentType("text/html;charset=utf-8");
PrintWriter pw=resp.getWriter();
pw.println("");
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("");
pw.flush();
pw.close();
}else{
resp.setContentType("text/html;charset=utf-8");
PrintWriter pw=resp.getWriter();
pw.println("");
pw.flush();
pw.close();
}
}
}
}
util:{连接数据库的工具类}
再写页码:(web下):
显示全部(show.jsp):
<%@ page import="java.util.List" %>
<%@ page import="com.bdqn.pojo.User" %><%--
Created by IntelliJ IDEA.
User: 12626
Date: 2022/4/26
Time: 14:58
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
用户信息展示
<%
List userList =(List)session.getAttribute("userList");
%>
用户信息列表
add.jsp:
<%--
Created by IntelliJ IDEA.
User: 12626
Date: 2022/4/26
Time: 14:58
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
update.jsp:
<%--
Created by IntelliJ IDEA.
User: 12626
Date: 2022/4/26
Time: 14:59
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
<%
//接收从show页面传的参数id
request.setCharacterEncoding("utf-8");
String idStr = request.getParameter("id");
Integer id = Integer.valueOf(idStr);
//把id存入session
session.setAttribute("id",id);
%>