<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
首页
${user.name},欢迎您
add.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
添加用户
添加联系人页面
list.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
用户信息管理系统
login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
管理员登录
管理员登录
${login_msg}
druid.properties
数据库连接属性
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///student
username=root
password=123
initialSize=5
maxActive=10
maxWait=3000
JDBCUtils.java
连接数据库
package utils;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
/**
* @author DAJ
* @version 1.0
* @ClassName JDBCUtils
* @date 2019/7/17 14:01
*/
public class JDBCUtils {
private static DataSource ds;
static {
Properties pro =new Properties();
InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
try {
pro.load(is);
ds = DruidDataSourceFactory.createDataSource(pro);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static DataSource getDs(){
return ds;
}
public static Connection getConn() throws SQLException {
return ds.getConnection();
}
}
update.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
修改用户
修改联系人
dao
先创建底层接口和实现方法
UserDao.java
package dao;
import entity.Person;
import java.util.List;
public interface UserDao {
/**
*功能描述 添加用户
* @author DAJ
* @date 2019/7/17
* @param person
* @return void
*/
void addPerson(Person person);
/**
*功能描述 根据ID查询用户
* @author DAJ
* @date 2019/7/17
* @param id
* @return entity.Person
*/
Person queryPersonById(int id);
/**
*功能描述 查询全部信息
* @author DAJ
* @date 2019/7/17
* @param null
* @return
*/
List queryonly();
/**
*功能描述 修改用户
* @author DAJ
* @date 2019/7/17
* @param person
* @return void
*/
void updatePerson(Person person);
/**
*功能描述 删除用户
* @author DAJ
* @date 2019/7/17
* @param id
* @return void
*/
void delete(int id);
}
UserDaoImlpl.java
package dao.impl;
import dao.UserDao;
import entity.Person;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import utils.JDBCUtils;
import java.util.List;
/**
* @author DAJ
* @version 1.0
* @ClassName UserDaoImpl
* @date 2019/7/17 15:19
*/
public class UserDaoImpl implements UserDao {
private static JdbcTemplate jt=null;
static {
jt = new JdbcTemplate(JDBCUtils.getDs());
}
@Override
public void addPerson(Person person) {
String sql="insert into web values (null,?,?,?,?,?,?)";
jt.update(sql, person.getName(), person.getgender(), person.getAge(), person.getaddress(), person.getQq(), person.getEmail());
}
@Override
public Person queryPersonById(int id) {
String sql="select * from web where id=?";
List query = jt.query(sql, new BeanPropertyRowMapper(Person.class), id);
return query.get(0);
}
@Override
public List queryonly() {
String sql = "select * from web";
List query = jt.query(sql, new BeanPropertyRowMapper(Person.class));
return query;
}
@Override
public void updatePerson(Person person) {
String sql = "update web set name=?,gender=?,age=?,address=?,qq=?,email=? where id=?";
jt.update(sql,person.getName(), person.getgender(), person.getAge(), person.getaddress(), person.getQq(), person.getEmail(),person.getId());
}
@Override
public void delete(int id) {
String sql="delete from web where id=?";
jt.update(sql, id);
}
}
Person.jsp
创建数据库类
package entity;
/**
* @author DAJ
* @version 1.0
* @ClassName Person
* @date 2019/7/17 13:59
*/
public class Person {
private int id;
private String name;
private String gender;
private int age;
private String address;
private int qq;
private String email;
public Person(int id, String name, String gender, int age, String address, int qq, String email) {
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
this.address = address;
this.qq = qq;
this.email = email;
}
public Person() {
}
public Person( String name, String gender, int age, String address, int qq, String email) {
this.name = name;
this.gender = gender;
this.age = age;
this.address = address;
this.qq = qq;
this.email = email;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getgender() {
return gender;
}
public void setgender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getaddress() {
return address;
}
public void setaddress(String address) {
this.address = address;
}
public int getQq() {
return qq;
}
public void setQq(int qq) {
this.qq = qq;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
", age=" + age +
", address='" + address + '\'' +
", qq=" + qq +
", email='" + email + '\'' +
'}';
}
}
service
IPersonService.java
package service;
import entity.Person;
import java.util.List;
public interface IPersonService {
/**
*功能描述 实现添加信息功能
* @author DAJ
* @date 2019/7/17
* @param person
* @return void
*/
void addPerson(Person person);
/**
*功能描述 修改信息
* @author DAJ
* @date 2019/7/17
* @param person
* @return void
*/
void updatePerson(Person person);
/**
*功能描述 显示全部信息
* @author DAJ
* @date 2019/7/17
* @param
* @return java.util.List
*/
List selectAll();
/**
*功能描述 通过ID来删除
* @author DAJ
* @date 2019/7/17
* @param id
* @return void
*/
void delete(int id);
/**
*功能描述 查询信息
* @author DAJ
* @date 2019/7/17
* @param id
* @return entity.Person
*/
Person selectAll(int id);
}
PersonServiceImpl
package service.impl;
import dao.UserDao;
import dao.impl.UserDaoImpl;
import entity.Person;
import service.IPersonService;
import java.util.List;
/**
* @author DAJ
* @version 1.0
* @ClassName PersonServiceImpl
* @date 2019/7/17 16:19
*/
public class PersonServiceImpl implements IPersonService {
private UserDao dao = new UserDaoImpl();
@Override
public void addPerson(Person person) {
dao.addPerson(person);
}
@Override
public void updatePerson(Person person) {
dao.updatePerson(person);
}
@Override//全部信息
public List selectAll() {
return dao.queryonly();
}
@Override
public void delete(int id) {
dao.delete(id);
}
@Override
public Person selectAll(int id) {
return dao.queryPersonById(id);
}
}
servlet
用注解来时间相关对应的操作
addUserServlet.java
package servlet;
import entity.Person;
import service.IPersonService;
import service.impl.PersonServiceImpl;
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 java.io.IOException;
@WebServlet( "/addUserServlet")
public class addUserServlet extends HttpServlet {
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 gender = request.getParameter("gender");
int age = Integer.parseInt(request.getParameter("age"));
String address = request.getParameter("address");
int qq=Integer.parseInt(request.getParameter("qq"));
String email = request.getParameter("email");
IPersonService is=new PersonServiceImpl();
Person person = new Person(name,gender,age,address,qq,email);
is.addPerson(person);
response.sendRedirect("findUserByPageServlet");
}
}
delSelectedServlet.java
package servlet;
import service.IPersonService;
import service.impl.PersonServiceImpl;
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 java.io.IOException;
@WebServlet( "/delSelectedServlet")
public class delSelectedServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
IPersonService is=new PersonServiceImpl();
String[] ids = request.getParameterValues("uid");
for (String id : ids) {
is.delete(Integer.parseInt(id));
}
response.sendRedirect("findUserByPageServlet");
}
}
delUserServlet.java
package servlet;
import service.IPersonService;
import service.impl.PersonServiceImpl;
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 java.io.IOException;
@WebServlet( "/delUserServlet")
public class delUserServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
IPersonService is = new PersonServiceImpl();
is.delete(id);
response.sendRedirect("findUserByPageServlet");
}
}
findUserByPageServlet.java
package servlet;
import entity.Person;
import service.IPersonService;
import service.impl.PersonServiceImpl;
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 java.io.IOException;
import java.util.List;
@WebServlet( "/findUserByPageServlet")
public class findUserByPageServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
IPersonService is = new PersonServiceImpl();
List list=is.selectAll();
request.setAttribute("list",list);
request.getRequestDispatcher("list.jsp").forward(request,response);
}
}
findUserServlet.java
package servlet;
import entity.Person;
import service.IPersonService;
import service.impl.PersonServiceImpl;
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 java.io.IOException;
@WebServlet( "/findUserServlet")
public class findUserServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
IPersonService is = new PersonServiceImpl();
int id = Integer.parseInt(request.getParameter("id"));
Person person = is.selectAll(id);
request.setAttribute("user", person);
request.getRequestDispatcher("update.jsp").forward(request,response);
}
}
updateUserServlet.java
package servlet;
import entity.Person;
import service.IPersonService;
import service.impl.PersonServiceImpl;
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 java.io.IOException;
@WebServlet("/updateUserServlet")
public class updateUserServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
IPersonService is=new PersonServiceImpl();
request.setCharacterEncoding("utf-8");
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
String gender = request.getParameter("gender");
int age = Integer.parseInt(request.getParameter("age"));
String address = request.getParameter("address");
int qq = Integer.parseInt(request.getParameter("qq"));
String email = request.getParameter("email");
Person person = new Person(id, name, gender, age, address, qq, email);
is.updatePerson(person);
response.sendRedirect("findUserByPageServlet");
}
}