1.今天来写一个简单的增删改查功能,这个项目还是接着在昨天写的 --JavaWeb实现简单登录注册-- 来写的。

登录成功进来后对学生信息的增删改查

JavaWeb 增删改查功能-LMLPHP

2.项目文件为:

JavaWeb 增删改查功能-LMLPHPJavaWeb 增删改查功能-LMLPHP

3.好了,直接贴上今天新写的代码,昨天的代码就不重复贴了。

src文件下的代码:

controller:

 package com.maike.controller;

 import java.io.IOException;
import java.util.List; 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 com.maike.model.Student;
import com.maike.model.StudentDto;
import com.maike.service.UserService;
import com.maike.service.UserServiceImpl; /**
* Servlet implementation class AddStuServlet
*/
@WebServlet("/AddStuServlet")
public class AddStuServlet extends HttpServlet {
UserService userService = new UserServiceImpl();
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public AddStuServlet() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String stuName = request.getParameter("stuName");
String stuSex = request.getParameter("stuSex");
String stuAge = request.getParameter("stuAge");
String stuClass = request.getParameter("stuClass"); StudentDto studentDto = new StudentDto();
studentDto.setStuName(stuName);
studentDto.setStuSex(stuSex);
studentDto.setStuAge(stuAge);
studentDto.setStuClass(stuClass); int k = userService.addStu(studentDto);
if(k > 0) {
List<Student> list = userService.seleStuAll();
request.setAttribute("stuList", list);
request.getRequestDispatcher("showStu.jsp").forward(request, response);
}else {
response.getWriter().append("添加失败").append(request.getContextPath());
request.getRequestDispatcher("addStu.jsp").forward(request, response);
}
} }

AddStuServlet

 package com.maike.controller;

 import java.io.IOException;
import java.util.List; 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 com.maike.model.Student;
import com.maike.service.UserService;
import com.maike.service.UserServiceImpl; /**
* Servlet implementation class DeleteStuServlet
*/
@WebServlet("/DeleteStuServlet")
public class DeleteStuServlet extends HttpServlet {
UserService userService = new UserServiceImpl();
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public DeleteStuServlet() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String stuName = request.getParameter("stuName");
int k = userService.deleteStu(stuName);
List<Student> list = userService.seleStuAll();
request.setAttribute("stuList", list);
request.getRequestDispatcher("showStu.jsp").forward(request, response);
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
} }

DeleteStuServlet

 package com.maike.controller;

 import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Servlet implementation class ToAddStuServlet
*/
@WebServlet("/ToAddStuServlet")
public class ToAddStuServlet extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public ToAddStuServlet() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.getRequestDispatcher("addStu.jsp").forward(request, response); } /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
} }

ToAddStuServlet

 package com.maike.controller;

 import java.io.IOException;
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 com.maike.model.Student;
import com.maike.service.UserService;
import com.maike.service.UserServiceImpl; /**
* Servlet implementation class ToUpdateStuServlet
*/
@WebServlet("/ToUpdateStuServlet")
public class ToUpdateStuServlet extends HttpServlet {
UserService userService = new UserServiceImpl();
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public ToUpdateStuServlet() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String stuName = request.getParameter("stuName");
Student student = userService.getStuByName(stuName);
request.setAttribute("student", student);
request.getRequestDispatcher("updateStu.jsp").forward(request, response);
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
} }

ToUpdateStuServlet

 package com.maike.controller;

 import java.io.IOException;
import java.util.List; 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 com.maike.model.Student;
import com.maike.service.UserService;
import com.maike.service.UserServiceImpl; /**
* Servlet implementation class UpdateStuServlet
*/
@WebServlet("/UpdateStuServlet")
public class UpdateStuServlet extends HttpServlet {
UserService userService = new UserServiceImpl();
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public UpdateStuServlet() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
String stuName = request.getParameter("stuName");
String stuSex = request.getParameter("stuSex");
String stuClass = request.getParameter("stuClass");
Student student = new Student();
student.setStuName(stuName);
student.setStuSex(stuSex);
student.setStuClass(stuClass);
int k = userService.updateStu(student);
List<Student> list = userService.seleStuAll();
request.setAttribute("stuList", list);
request.getRequestDispatcher("showStu.jsp").forward(request, response);
} }

UpdateStuServlet

dao:

 package com.maike.dao;

 import java.util.List;

 import com.maike.model.Student;

 public interface StudentDao {

     List<Student> selectAll();
Student selectByName(String stuName);
int insert(Student student);
int update(Student student);
int delete(String stuName); }

StudentDao

 package com.maike.dao;

 import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; import com.maike.model.Student;
import com.maike.util.ConnectionUtil; public class StudentDaoImpl implements StudentDao {
Student student;
Connection con = null;
PreparedStatement pt = null;
ResultSet rs = null;
String sql; /**
* 查询所有学生
*/
@Override
public List<Student> selectAll() {
// TODO Auto-generated method stub
List<Student> list = new ArrayList<Student>();
sql = "select * from student";
try {
con = ConnectionUtil.getConnection();
pt = con.prepareStatement(sql);
rs = pt.executeQuery(); while(rs.next()) {
student = new Student();
student.setStuId(rs.getInt("stu_id"));
student.setStuName(rs.getString("stu_name"));
student.setStuSex(rs.getString("stu_sex"));
student.setStuAge(rs.getInt("stu_age"));
student.setStuClass(rs.getString("stu_class")); list.add(student);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
ConnectionUtil.closeRe(con, pt, rs);
}
return list;
} /**
* 通过名字查询学生
*/
@Override
public Student selectByName(String stuName) {
// TODO Auto-generated method stub
sql = "select * from student where stu_name = ?";
try {
con = ConnectionUtil.getConnection();
pt = con.prepareStatement(sql);
pt.setString(1, stuName);
rs = pt.executeQuery(); while(rs.next()) {
student = new Student();
student.setStuId(rs.getInt("stu_id"));
student.setStuName(rs.getString("stu_name"));
student.setStuSex(rs.getString("stu_sex"));
student.setStuAge(rs.getInt("stu_age"));
student.setStuClass(rs.getString("stu_class"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
ConnectionUtil.closeRe(con, pt, rs);
}
return student;
} /**
* 添加学生
*/
@Override
public int insert(Student student) {
// TODO Auto-generated method stub
int k = 0;
sql = "insert into student values(null,?,?,?,?)";
try {
con = ConnectionUtil.getConnection();
pt = con.prepareStatement(sql);
pt.setString(1, student.getStuName());
pt.setString(2, student.getStuSex());
pt.setInt(3, student.getStuAge());
pt.setString(4, student.getStuClass()); k = pt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
ConnectionUtil.closeRe(con, pt);
}
return k;
} /**
* 更新学生
*/
@Override
public int update(Student student) {
// TODO Auto-generated method stub
int k = 0;
sql = "update student set stu_sex = ? ,stu_class = ? where stu_name = ?";
try {
con = ConnectionUtil.getConnection();
pt = con.prepareStatement(sql);
pt.setString(1, student.getStuSex());
pt.setString(2, student.getStuClass());
pt.setString(3, student.getStuName()); k = pt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
ConnectionUtil.closeRe(con, pt);
}
return k;
} /**
* 通过名字删除学生
*/
@Override
public int delete(String stuName) {
// TODO Auto-generated method stub
int k = 0;
sql = "delete from student where stu_name = ?";
try {
con = ConnectionUtil.getConnection();
pt = con.prepareStatement(sql);
pt.setString(1, stuName); k = pt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
ConnectionUtil.closeRe(con, pt);
}
return k;
} }

StudentDaoImpl

model:

 package com.maike.model;

 public class Student {

     private int stuId;

     private String stuName;

     private String stuSex;

     private int stuAge;

     private String stuClass;

     public int getStuId() {
return stuId;
} public void setStuId(int stuId) {
this.stuId = stuId;
} public String getStuName() {
return stuName;
} public void setStuName(String stuName) {
this.stuName = stuName;
} public String getStuSex() {
return stuSex;
} public void setStuSex(String stuSex) {
this.stuSex = stuSex;
} public int getStuAge() {
return stuAge;
} public void setStuAge(int stuAge) {
this.stuAge = stuAge;
} public String getStuClass() {
return stuClass;
} public void setStuClass(String stuClass) {
this.stuClass = stuClass;
} @Override
public String toString() {
return "Student [stuId=" + stuId + ", stuName=" + stuName + ", stuSex=" + stuSex + ", stuAge=" + stuAge
+ ", stuClass=" + stuClass + "]";
} }

Student

 package com.maike.model;

 public class StudentDto {

     private String stuId;

     private String stuName;

     private String stuSex;

     private String stuAge;

     private String stuClass;

     public String getStuId() {
return stuId;
} public void setStuId(String stuId) {
this.stuId = stuId;
} public String getStuName() {
return stuName;
} public void setStuName(String stuName) {
this.stuName = stuName;
} public String getStuSex() {
return stuSex;
} public void setStuSex(String stuSex) {
this.stuSex = stuSex;
} public String getStuAge() {
return stuAge;
} public void setStuAge(String stuAge) {
this.stuAge = stuAge;
} public String getStuClass() {
return stuClass;
} public void setStuClass(String stuClass) {
this.stuClass = stuClass;
} @Override
public String toString() {
return "StudentDto [stuId=" + stuId + ", stuName=" + stuName + ", stuSex=" + stuSex + ", stuAge=" + stuAge
+ ", stuClass=" + stuClass + "]";
} }

StudentDto

service:

 package com.maike.service;

 import java.util.List;

 import com.maike.model.Student;
import com.maike.model.StudentDto; public interface UserService { int insertUser(String userName,String userPwd,String userRePwd);
int judgeLogin(String userName,String userPwd); List<Student> seleStuAll();
int addStu(StudentDto studentDto);
int updateStu(Student student);
int deleteStu(String stuName);
Student getStuByName(String stuName); }

UserService

     /**
* seleStuAll:查询所有学生信息. <br/>
*/
@Override
public List<Student> seleStuAll() {
// TODO Auto-generated method stub
return studentDao.selectAll();
} /**
* addStu:添加学生信息 .<br/>
*/
@Override
public int addStu(StudentDto studentDto) {
// TODO Auto-generated method stub
int k = 0;
String stuName = studentDto.getStuName();
String stuAge = studentDto.getStuAge();
if(stuName == null || stuAge == null) {
return k;
}
int IntAge = Integer.parseInt(stuAge);
Student student = new Student();
student.setStuName(stuName);
student.setStuSex(studentDto.getStuSex());
student.setStuAge(IntAge);
student.setStuClass(studentDto.getStuClass());
k = studentDao.insert(student);
return k;
} /**
* updateStu:修改学生信息 .<br/>
*/
@Override
public int updateStu(Student student) {
// TODO Auto-generated method stub
return studentDao.update(student);
} /**
* deleteStu :删除学生信息 .<br/>
*/
@Override
public int deleteStu(String stuName) {
// TODO Auto-generated method stub
return studentDao.delete(stuName);
} /**
* 通过名字查询学生
*/
@Override
public Student getStuByName(String stuName) {
// TODO Auto-generated method stub
return studentDao.selectByName(stuName);
}

UserServiceImpl

jsp页面:

 <%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>学生信息</title>
</head>
<body>
<br/>
<br/>
<br/>
<h2 align = "center">学生信息列表</h2>
<table align = "center" width="15%">
<tr>
<th align = "center"><a href = "ToAddStuServlet"><button style = "color:blue">添加学生</button></a></th>
</tr>
</table>
<table align = "center">
<tr>
<th>姓名</th><th>性别</th><th>年龄</th><th>班级</th><th colspan="2">管理</th>
</tr>
<c:forEach var = "list" items="${stuList}">
<tr>
<td>${list.stuName }</td><td>${list.stuSex}</td><td>${list.stuAge }</td><td>${list.stuClass}</td>
<td><a href = "DeleteStuServlet?stuName=${list.stuName }" ><button style = "color:blue">删除</button></a></td><td><a href = "ToUpdateStuServlet?stuName=${list.stuName }"><button style = "color:blue">修改</button></a></td>
</tr>
</c:forEach>
</table>
</body>
</html>

showStu

 <%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>添加页面</title>
</head>
<body>
<br>
<br>
<br>
<h2 align = "center">添加页面</h2>
<form action="AddStuServlet" method = "post">
<table align = "center">
<tr>
<td>名字</td><td><input type = "text" name = "stuName" ></td>
</tr>
<tr>
<td>性别</td><td><input type = "text" name = "stuSex" ></td>
</tr>
<tr>
<td>年龄</td><td><input type = "text" name = "stuAge" placeholder ="请输入整数" ></td>
</tr>
<tr>
<td>班级</td><td><input type = "text" name = "stuClass" ></td>
</tr>
<tr>
<td colspan="2" align = "center"><input type="submit" value = "添加" style="color:blue" ></td>
</tr>
</table>
</form>
</body>
</html>

addStu

 <%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>修改页面</title>
</head>
<body>
<br>
<br>
<br>
<h2 align = "center">修改页面</h2>
<form action="UpdateStuServlet" method = "post" align = "center">
<table align = "center">
<tr>
<td>名字</td><td><input type = "text" name = "stuName" value = "${student.stuName }" readonly="readonly" ></td>
</tr>
<tr>
<td>性别</td><td><input type = "text" name = "stuSex" value = "${student.stuSex }"></td>
</tr>
<tr>
<td>年龄</td><td><input type = "text" name = "stuAge" value = "${student.stuAge }" disabled="disabled"></td>
</tr>
<tr>
<td>班级</td><td><input type = "text" name = "stuClass" value = "${student.stuClass }"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value = "修改" style="color:blue" ></td>
</tr>
</table>
</form>
</body>
</html>

updateStu

数据库创建语句:

 CREATE TABLE nanfeng.student (
stu_id INT NOT NULL AUTO_INCREMENT,
stu_name varchar(100) NULL,
stu_sex varchar(100) NULL,
stu_age INT NULL,
stu_class varchar(100) NULL,
CONSTRAINT student_pk PRIMARY KEY (stu_id)
)
ENGINE=InnoDB
DEFAULT CHARSET=utf8
COLLATE=utf8_general_ci;
05-11 22:15