1.index.jsp
<%--
Created by IntelliJ IDEA.
User: jiachenglin
Date: 2022/10/24
Time: 19:30
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加界面</title>
<h1 style="color: red;text-align: center">添加界面</h1>
</head>
<body>
<form action="${pageContext.request.contextPath}/Servlet" method="post">
<p>姓名:</p><br>
<input type="text" name="name" id="ne" width="200px"><span id="ne_err" style="color: red;display: none">输入错误</span>
<p>学号:</p><br>
<input type="text" name="id" id="id1" width="200px"><span id="id1_err" style="color: red;display: none">输入错误</span>
<p>性别:</p><br>
<input type="radio" name="gender" id="gen" value="男">男<br>
<input type="radio" name="gender" id="gen1" value="女">女<br>
<p>地址:</p><br>
<input type="text" name="address" id="add" width="200px"><span id="add_err" style="color: red;display: none">输入错误</span>
<input value="add" name="method" type="hidden">
<input type="submit" value="提交">
</form>
</body>
</html>
2.add.jsp
<%--
Created by IntelliJ IDEA.
User: jiachenglin
Date: 2022/10/24
Time: 19:30
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加界面</title>
<h1 style="color: red;text-align: center">添加界面</h1>
</head>
<body>
<form action="${pageContext.request.contextPath}/Servlet" method="post">
<p>姓名:</p><br>
<input type="text" name="name" id="ne" width="200px"><span id="ne_err" style="color: red;display: none">输入错误</span>
<p>学号:</p><br>
<input type="text" name="id" id="id11" width="200px"><span id="id1_err" style="color: red;display: none">输入错误</span>
<p>性别:</p><br>
<input type="radio" name="gender" id="gen" value="男">男<br>
<input type="radio" name="gender" id="gen1" value="女">女<br>
<p>地址:</p><br>
<input type="text" name="address" id="add" width="200px"><span id="add_err" style="color: red;display: none">输入错误</span>
<input value="add" name="method" type="hidden">
<input type="submit" value="提交">
<script>
let name1=document.getElementById("ne")
name1.onblur=function (){
let name2=name1.value.trim();
if(name2.length==0){
document.getElementById("ne_err").style.display='';//中间一定无空格,有空格也不显示
}
else {
document.getElementById("ne_err").style.display='none';
}
}
let id1=document.getElementById("id11")
id1.onblur=function (){
let id2=id1.value.trim();
if(id2.length==0){
document.getElementById("id1_err").style.display='';
}
else
document.getElementById("id1_err").style.display='none';
}
let address1=document.getElementById("add")
address1.onblur=function (){
let address2=address1.value.trim();
if(address2.length==0){
document.getElementById("add_err").style.display='';
}
else
document.getElementById("add_err").style.display='none';
}
</script>
</form>
</body>
</html>
3.update.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: jiachenglin
Date: 2022/10/25
Time: 12:20
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<% int num= Integer.parseInt(request.getParameter("num"));%>
<title>更新界面</title>
<h1 style="color: red;text-align: center">更改信息</h1>
</head>
<body>
<c:forEach items="${list}" var="student">
<form action="${pageContext.request.contextPath}/Servlet" method="post">
<p>学号:</p><br>
<input type="text" name="id" id="id11" width="200px" value="${student.id}"><span id="id1_err" style="color: red;display: none">输入错误</span>
<p>性别:</p><br>
<input type="radio" name="gender" id="gen" value="男" checked>男<br>
<input type="radio" name="gender" id="gen1" value="女" >女<br>
<p>地址:</p><br>
<input type="text" name="address" id="add" width="200px"value="${student.address}"><span id="add_err" style="color: red;display: none">输入错误</span>
<input value="update" name="method" type="hidden">
<input value="<%=num%>" name="num" type="hidden">
<input type="submit" value="提交">
</c:forEach>
<script>
let id2=document.getElementById("id11");
id2.onblur=function (){
let id3=id2.value.trim();
if(id3.length==0){
document.getElementById("id1_err").style.display='';//中间无空格,有空格也不显示
}
else
document.getElementById("id1_err").style.display='none';
}
let address1=document.getElementById("add");
address1.onblur=function (){
let address2=address1.value.trim();
if(address2.length==0){
document.getElementById("add_err").style.display='';
}
else
document.getElementById("add_err").style.display='none';
}
</script>
</form>
</body>
</html>
4.err_find.jsp
<%--
Created by IntelliJ IDEA.
User: jiachenglin
Date: 2022/10/25
Time: 12:37
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>错误</title>
<h1 style="text-align: center;color:red;">输入错误</h1>
<h2 ><a href="find.jsp">点击返回</a> </h2>
</head>
<body>
</body>
</html>
5.find.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: jiachenglin
Date: 2022/10/25
Time: 12:29
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/Servlet" method="get">
<p>编号:</p><br>
<input type="text" name="num" id="nu" width="200px"><span id="nu_err" style="color: red;display: none">输入错误</span>
<p>姓名:</p><br>
<input type="text" name="name" id="ne" width="200px"><span id="ne_err" style="color: red;display: none">输入错误</span>
<p>学号:</p><br>
<input type="text" name="id" id="id1" width="200px"><span id="id1_err" style="color: red;display: none">输入错误</span>
<p>性别:</p><br>
<input type="radio" name="gender" id="gen" value="男">男<br>
<input type="radio" name="gender" id="gen1" value="女">女<br>
<p>地址:</p><br>
<input type="text" name="address" id="add" width="200px"><span id="add_err" style="color: red;display: none">输入错误</span>
<input value="find1" name="method" type="hidden">
<input type="submit" value="提交">
<table width="auto" style="margin-top: auto" border="1" cellspacing="0">
<tr>
<th>编号</th>
<th>姓名</th>
<th>学号</th>
<th>性别</th>
<th>地址</th>
</tr>
<c:forEach items="${list}" var="student">
<tr>
<td>${student.num}</td>
<td>${student.name}</td>
<td>${student.id}</td>
<td>${student.gender}</td>
<td>${student.address}</td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>
6.liulan.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: jiachenglin
Date: 2022/10/24
Time: 20:43
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>浏览界面</title>
<h1 style="color: red;text-align: center"></h1>
</head>
<body>
<table width="auto" style="margin-top: auto" border="1" cellspacing="0">
<tr>
<th>编号</th>
<th>姓名</th>
<th>学号</th>
<th>性别</th>
<th>地址</th>
</tr>
<c:forEach items="${list}" var="student">
<tr>
<td>${student.num}</td>
<td>${student.name}</td>
<td>${student.id}</td>
<td>${student.gender}</td>
<td>${student.address}</td>
<td><a href="${pageContext.request.contextPath}/Servlet?method=update&num=${student.num}">修改</a> </td>
<td><a href="${pageContext.request.contextPath}/Servlet?method=delete&num=${student.num}">删除</a> </td>
</tr>
</c:forEach>
</table>
</body>
</html>
7.student
package Student;
import java.util.*;
public class student {
private String name;
private int num;
public student(int num,String name, String id, String gender, String address) {
this.name = name;
this.num = num;
this.id = id;
this.gender = gender;
this.address = address;
}
public student() {
}
public void setNum(int num) {
this.num = num;
}
public int getNum() {
return num;
}
private String id;
private String gender;
private String address;
public String getName() {
return name;
}
public String getId() {
return id;
}
public String getGender() {
return gender;
}
public String getAddress() {
return address;
}
public void setName(String name) {
this.name = name;
}
public void setId(String id) {
this.id = id;
}
public void setGender(String gender) {
this.gender = gender;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "student{" +
"name='" + name + '\'' +
", id='" + id + '\'' +
", gender='" + gender + '\'' +
", address='" + address + '\'' +
'}';
}
}
8.JDBCTools
package util;
import java.sql.*;
public class JDBCTools {
private static Connection connection = null;
private static String url = "jdbc:mysql://localhost:3306/demo";
private static String usrname = "root";
private static String password = "QQ1003392478pp15";
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
try {
connection = DriverManager.getConnection(url, usrname, password);
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
public static void release(Connection connection, Statement statement, ResultSet resultSet) {
try {
if (connection != null) {
connection.close();
}
if (statement != null) {
statement.close();
}
if (resultSet != null) {
resultSet.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
9.StudentDao
package Dao;
import jdk.nashorn.internal.scripts.JD;
import util.JDBCTools;
import Student.student;
import java.sql.*;
import java.util.*;
public class StudentDao {
public List<student> findAll() {
List<student> list = new ArrayList<>();
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
String sql = "select *from study ";
connection = JDBCTools.getConnection();
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
int num = Integer.parseInt(resultSet.getString(1));
String name = resultSet.getString(2);
String id = resultSet.getString(3);
String gender = resultSet.getString(4);
String address = resultSet.getString(5);
student student = new student(num, name, id, gender, address);
list.add(student);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCTools.release(connection, preparedStatement, resultSet);
}
return list;
}
public void add(String name, String id, String gender, String address) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = JDBCTools.getConnection();
String sql = "insert into study(name,id,gender,address) values(?,?,?,?)";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, name);
preparedStatement.setString(2, id);
preparedStatement.setString(3, gender);
preparedStatement.setString(4, address);
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCTools.release(connection, preparedStatement, null);
}
}
public void delete(int num) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = JDBCTools.getConnection();
String sql = "delete from study where num=?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, String.valueOf(num));
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCTools.release(connection, preparedStatement, null);
}
}
public List<student> find(String num, String name, String id, String gender, String address) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
List<student> list = new ArrayList<>();
try {
connection = JDBCTools.getConnection();
StringBuffer sb = new StringBuffer();
List<Object> l = new ArrayList<>();
sb.append("select * from study where 1=1");
if (name != null && name != "") {
sb.append(" and name like '%' ? '%' ");
l.add(name);
}
if (gender != null && gender != "") {
sb.append(" and gender like '%' ? '%' ");
l.add(gender);
}
if (id != null && id != "") {
sb.append(" and id like '%' ? '%' ");
l.add(id);
}
if (num != null && num != "") {
sb.append(" and num like '%' ? '%' ");
l.add(num);
}
if (address != null && address != "") {
sb.append(" and address like '%' ? '%' ");
l.add(address);
}
preparedStatement = connection.prepareStatement(sb.toString());
for (int i = 1; i <= l.size(); i++) {
preparedStatement.setObject(i, l.get(i - 1));
}
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
int num1 = Integer.parseInt(resultSet.getString(1));
String name1 = resultSet.getString(2);
String id1 = resultSet.getString(3);
String gender1 = resultSet.getString(4);
String address1 = resultSet.getString(5);
student student = new student(num1, name1, id1, gender1, address1);
list.add(student);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCTools.release(connection, preparedStatement, resultSet);
}
return list;
}
public void update(int num, String id, String gender, String address) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = JDBCTools.getConnection();
String sql = "update study set id=?, gender=? ,address=? where num=?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, id);
preparedStatement.setString(2, gender);
preparedStatement.setString(3, address);
preparedStatement.setString(4, String.valueOf(num));
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCTools.release(connection, preparedStatement, null);
}
}
}
10.Servlet
package StudentServlet;标签:javaweb,gender,request,完整,文档,student,address,id,String From: https://www.cnblogs.com/joranger/p/16826036.html
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import Student.student;
import Dao.StudentDao;
@WebServlet("/Servlet")
public class Servlet extends HttpServlet {
private StudentDao student = new StudentDao();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String method = request.getParameter("method");
switch (method) {
case "add":
request.getRequestDispatcher("add.jsp").forward(request, response);
break;
case "delete":
int num = Integer.parseInt(request.getParameter("num"));
student.delete(num);
List<student> list = student.findAll();
request.setAttribute("list", list);
request.getRequestDispatcher("liulan.jsp").forward(request, response);
break;
case "update":
String num1 = request.getParameter("num");
List<student> list3 = student.find(num1,null,null,null,null);
request.setAttribute("list",list3);
request.getRequestDispatcher("update.jsp").forward(request, response);
break;
case "liulan":
List<student> list1 = student.findAll();
request.setAttribute("list", list1);
request.getRequestDispatcher("liulan.jsp").forward(request, response);
break;
case "find":
request.getRequestDispatcher("find.jsp").forward(request,response);
case "find1":
String num2 = request.getParameter("num");
String name = request.getParameter("name");
String id = request.getParameter("id");
String gender = request.getParameter("gender");
String address = request.getParameter("address");
List<student> list2 = student.find(num2, name, id, gender, address);
request.setAttribute("list", list2);
if (list2.isEmpty()) {
request.getRequestDispatcher("err_find.jsp").forward(request, response);
} else
request.getRequestDispatcher("find.jsp").forward(request, response);
break;
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String method = request.getParameter("method");
switch (method) {
case "add":
String name = request.getParameter("name");
String id = request.getParameter("id");
String gender = request.getParameter("gender");
String address = request.getParameter("address");
student.add(name, id, gender, address);
break;
case "update":
int num = Integer.parseInt(request.getParameter("num"));
id = request.getParameter("id");
gender = request.getParameter("gender");
address = request.getParameter("address");
student.update(num,id,gender,address);
break;
}
response.sendRedirect("index.jsp");
}
}