public class ActionServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
StudentDao dao = new StudentDao();
String url=request.getRequestURI();
String action=url.substring(url.lastIndexOf('/')+1,url.lastIndexOf('.'));
if (action.equals("add")) {
//获取页面中输入的相关内容
String name=request.getParameter("name");
name = new String(name.getBytes("iso-8859-1"),"utf-8");
int age=Integer.parseInt(request.getParameter("age"));
String gender = request.getParameter("gender");
int state=Integer.parseInt(request.getParameter("state"));
Student stu = new Student(name,age,gender,state);
dao.add(stu);
response.sendRedirect("list.do");
}
if (action.equals("delete")) {
//获取页面提交的id信息
int id=Integer.parseInt(request.getParameter("id"));
dao.delete(id);
response.sendRedirect("list.do");
}
if(action.equals("toUpdate")) {
int id=Integer.parseInt(request.getParameter("id"));
System.out.println(id);
Student s = dao.findById(id);
request.setAttribute("student", s);
request.getRequestDispatcher("update.jsp").forward(request, response);
}
if (action.equals("update")) {
int id=Integer.parseInt(request.getParameter("id"));
//获取修改之后提交的内容
String name=request.getParameter("name");
name = new String(name.getBytes("iso-8859-1"),"utf-8");
int age=Integer.parseInt(request.getParameter("age"));
String gender = request.getParameter("gender");
int state=Integer.parseInt(request.getParameter("state"));
Student s = new Student(id,name,age,gender,state);
dao.update(s);
response.sendRedirect("list.do");
}
if (action.equals("list")) {
List<Student> students=dao.findAll();
request.setAttribute("students",students );
request.getRequestDispatcher("StudentList.jsp").forward(request, response);
}
}
}
public class StudentDao {
Connection con = null;
PreparedStatement ptmt = null;
ResultSet rs = null;
{
}
public void add(Student s) {
con = DBUtil.getConnection();
String sql = "insert into student(name,age,gender,state) values(?,?,?,?)";
try {
ptmt = con.prepareStatement(sql);
ptmt.setString(1, s.getName());
ptmt.setInt(2, s.getAge());
ptmt.setString(3, s.getGender());
ptmt.setInt(4, s.getState());
ptmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.closeConnection(con);
}
}
public void delete(int id) {
con = DBUtil.getConnection();
String sql = "delete from student where id=?";
try {
ptmt = con.prepareStatement(sql);
ptmt.setInt(1, id);
ptmt.executeUpdate();
} catch (SQLException e) {
DBUtil.closeConnection(con);
}finally {
DBUtil.closeConnection(con);
}
}
public void update(Student s) {
con = DBUtil.getConnection();
String sql = "update student set name=?,age=?,gender=? ,state=? where id=?";
try {
ptmt = con.prepareStatement(sql);
ptmt.setString(1, s.getName());
ptmt.setInt(2, s.getAge());
ptmt.setString(3, s.getGender());
ptmt.setInt(4, s.getState());
ptmt.executeUpdate();
} catch (SQLException e) {
DBUtil.closeConnection(con);
}finally {
DBUtil.closeConnection(con);
}
}
public List<Student> findAll() {
con = DBUtil.getConnection();
List<Student> students = new ArrayList<>();
String sql = "select * from student";
try {
ptmt = con.prepareStatement(sql);
rs = ptmt.executeQuery();
while (rs.next()) {
int id1 = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
String gender = rs.getString("gender");
int state = rs.getInt("state");
Student student = new Student(id1,name,age,gender,state);
students.add(student);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return students;
}
public Student findById(int id) {
con = DBUtil.getConnection();
String sql = "select * from student where id=?";
Student student = null;
try {
ptmt = con.prepareStatement(sql);
ptmt.setInt(1, id);
rs = ptmt.executeQuery();
if (rs.next()) {
int id1 = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
String gender = rs.getString("gender");
int state = rs.getInt("state");
student = new Student(id1,name,age,gender,state);
}
} catch (SQLException e) {
DBUtil.closeConnection(con);
}finally {
DBUtil.closeConnection(con);
}
return student;
}
}
public class Student {
private int id;
private String name;
private int age;
private String gender;
private int state;
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
public Student() {
}
public Student(String name, int age, String gender, int state) {
super();
this.name = name;
this.age = age;
this.gender = gender;
this.state = state;
}
public Student(int id, String name, int age, String gender, int state) {
super();
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.state = state;
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
public class DBUtil {
private static String driver="oracle.jdbc.driver.OracleDriver";
private static String url="jdbc:oracle:thin:@localhost:1521";
private static String username="system";
private static String password="root";
private static Connection con=null;
//连接数据库的方法
public static Connection getConnection(){
try {
Class.forName(driver);
con=DriverManager.getConnection(url,username,password);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
//关闭数据库的方法
public static void closeConnection(Connection con){
if (con!=null) {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("关闭资源失败");
}
}
}
public static void main(String[] args) {
Connection con=DBUtil.getConnection();
System.out.println(con);
}
}
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html>
<html>
<head>
<title>添加学生</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form action="add.do" method="get">
姓名:<input type="text" name="name"><br>
年龄:<input type="text" name="age"><br>
性别:<select name="gender">
<option value="m">male</option>
<option value="f">female</option>
</select><br>
状态:<select name="state">
<option value="0">0</option>
<option value="1">1</option>
</select><br>
<input type="submit" value="提交">
</form>
</body>
</html>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>员工管理系统</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="css/style.css" />
</head>
<body>
<table>
<thead>
<tr>
<th>id</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>状态</th>
<th>操作</th>
</tr>
</thead>
<c:forEach items="${students}" var="s">
<tr>
<td>${s.id}</td>
<td>${s.name}</td>
<td>${s.age}</td>
<td>${s.gender}</td>
<td>${s.state}</td>
<td><a href="delete.do?id=${s.id}" >删除</a><a href="toUpdate.do?id=${s.id}">修改</a></td>
</tr>
</c:forEach>
</table>
<input type="button" value="添加学生" οnclick="add()">
</body>
<script type="text/javascript">
function add(){
window.location.href = "add.jsp";
}
</script>
</html>
<%@page import="java.text.SimpleDateFormat"%>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html>
<html>
<head>
<title>修改员工</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css"
href="css/style.css" />
</head>
<script type="text/javascript" src="jquery-3.3.1.js"></script>
<body>
<form action="update.do">
<input type="hidden" name="id" value="${student.id }">
姓名:<input type="text" name="name" value="${student.name}"><br>
年龄:<input type="text" name="age" value="${student.age}"><br>
性别:<select id="gender" name="gender">
<option value="m">male</option>
<option value="f">female</option>
</select>
<br>
状态:<select id="state" name="state">
<option value="0">0</option>
<option value="1">1</option>
</select>
<br>
<input type="submit" value="修改">
</form>
</body>
<script type="text/javascript">
$('#gender option[value="${student.gender}"]').prop("selected",true);
$('#state option[value="${student.state}"]').prop("selected",true);
</script>
</html>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>action.ActionServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>