本文主要是介绍Day19_学点儿JavaEE_学生表和班级表连接查询并显示、带查询班级表的学生信息添加、带查询班级并回显的学生信息修改,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1 目标效果预览
1.1 展示学生信息时,多添加一列展示所属班级(班级表是另一个表)
1.2 添加学生信息时,多一行班级选择(班级表是另一个表)
1.3 点击“编辑”后,修改学生信息时,查询其所属班级并默认选中
2 学生表和班级表连接查询并显示(Value Object)
重新封装要展示的信息为一个类,并编写相应的Servlet、Service、Dao
StudentBanjiVO
/** 适度编码益脑,沉迷编码伤身,合理安排时间,享受快乐生活。* Copyright @TangXJ* Created by TangXJ* Created&Used date: 2024/4/8 下午3:13 ~ 2024/4/8 下午3:18* Modified date: 2024/4/8 下午3:14*/package com.sdust.web.pojo.vo;//VO:Value Object
public class StudentBanjiVO {private Integer id;private String name;private Integer age;private String gender;private Integer banjiId;private String banjiName;public StudentBanjiVO() {}public StudentBanjiVO(Integer id, String name, Integer age, String gender, Integer banjiId, String banjiName) {this.id = id;this.name = name;this.age = age;this.gender = gender;this.banjiId = banjiId;this.banjiName = banjiName;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public Integer getBanjiId() {return banjiId;}public void setBanjiId(Integer banjiId) {this.banjiId = banjiId;}public String getBanjiName() {return banjiName;}public void setBanjiName(String banjiName) {this.banjiName = banjiName;}@Overridepublic String toString() {return "StudentBanjiVO{" +"id=" + id +", name='" + name + '\'' +", age=" + age +", gender='" + gender + '\'' +", banjiId=" + banjiId +", banjiName='" + banjiName + '\'' +'}';}
}
连接查询
学点儿数据库_Day11_多表、等值连接、内连接、模糊查找
SELECT s.id,s.`name`,s.age,s.gender,b.id as banji_id,b.`name` AS banji_name
FROM student AS s INNER JOIN banji AS b
ON s.banji_id=b.id
ORDER BY s.id
LIMIT 0,5
StudentDaoImply
:
@Overridepublic List<StudentBanjiVO> selectByPage(int offset, Integer pageSize) {Connection connection = null;PreparedStatement statement = null;ResultSet resultSet = null;List<StudentBanjiVO> list = new ArrayList<>();try {connection = JDBCUtil.getConnection();String sql = "SELECT s.id,s.`name`,s.age,s.gender,b.id as banji_id,b.`name` AS banji_name\n" +"FROM student AS s INNER JOIN banji AS b\n" +"ON s.banji_id=b.id\n" +"ORDER BY s.id\n" +"LIMIT ?,? ";statement = connection.prepareStatement(sql);statement.setInt(1, offset);statement.setInt(2, pageSize);System.out.println(statement);resultSet = statement.executeQuery();while (resultSet.next()) {int id = resultSet.getInt("id");String name = resultSet.getString("name");int age = resultSet.getInt("age");String gender = resultSet.getString("gender");int banjiId = resultSet.getInt("banji_id");String banjiName = resultSet.getString("banji_name");StudentBanjiVO student = new StudentBanjiVO(id, name, age, gender, banjiId, banjiName);list.add(student);}} catch (SQLException e) {throw new RuntimeException(e);}return list;}
3 带查询班级表的学生信息添加
写一套班级的servlet、service、dao,然后在StudentServlet
里边的toUpdate
调用一下班级的查询service,一起发送到student_add.jsp
即可
这就是三层架构、解耦合的“威力”
private void updateQuery(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("StudentServlet.updateQuery");String queryId = req.getParameter("id");Student student = studentService.selectById(Integer.parseInt(queryId));List<Banji> banjiList = banjiService.selectAll();//1.第一句//把数据放到req里面req.setAttribute("student", student);req.setAttribute("banjiList", banjiList);//2.第二句//转发到student_list.jsp页面进行展示req.getRequestDispatcher("student_update.jsp").forward(req, resp);}
student_add.jsp
:
<%@ page import="java.util.List" %>
<%@ page import="com.sdust.web.pojo.entity.Banji" %><%--~ 适度编码益脑,沉迷编码伤身,合理安排时间,享受快乐生活。~ Copyright @TangXJ~ Created by TangXJ~ Created&Used date: 2024/4/3 下午2:21 ~ 2024/4/8 下午4:00~ Modified date: 2024/4/8 下午4:00--%><%--Created by IntelliJ IDEA.User: TangXJDate: 2024/4/3Time: 14:21To 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="/student?method=add" method="get">--%><%--默认就是get ?method=就是get方法传参 会被覆盖 所以form表单一般不用--%><%List<Banji> banjiList = (List<Banji>) request.getAttribute("banjiList");%><form action="/student?method=add" method="post"><%--默认就是get ?method=就是get方法传参 会被覆盖 所以form表单一般不用--%>姓名:<input type="text" name="name"><br>年龄:<input type="text" name="age"><br>性别:<input type="text" name="gender"><br>班级:<select name="banjiId"><%for (Banji banji : banjiList) {%><option value="<%=banji.getId()%>"><%=banji.getName()%></option><%}%></select><br><input type="submit" value="添加"></form>
</body>
</html>
4 带查询班级并回显的学生信息修改
<select name="class"><%// 假设classList是从Servlet传递过来的班级列表,每个班级对象包含id和name属性for (ClassInfo classInfo : classList) {// 根据学生ID和班级ID是否一致来确定哪个选项应该被选中String selected = (student.getId() == classInfo.getId()) ? "selected" : "";%><option value="<%= classInfo.getId() %>" <%= selected %>><%= classInfo.getName() %></option><%}%>
</select>
student_update.jsp
:
<%@ page import="com.sdust.web.pojo.entity.Student" %>
<%@ page import="java.util.List" %>
<%@ page import="com.sdust.web.pojo.entity.Banji" %><%--~ 适度编码益脑,沉迷编码伤身,合理安排时间,享受快乐生活。~ Copyright @TangXJ~ Created by TangXJ~ Created&Used date: 2024/4/3 下午2:54 ~ 2024/4/8 下午4:19~ Modified date: 2024/4/8 下午4:19--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body><%//JSP脚本,这里面可以任意写java代码//request、response:JSP内置对象Student student = (Student) request.getAttribute("student");List<Banji> banjiList = (List<Banji>) request.getAttribute("banjiList");System.out.println(student);System.out.println(banjiList);%><%--<form action="/student?method=update&id=<%=student.getId()%>" method="post">--%><%--默认就是get ?method=就是get方法传参 会被覆盖 所以form表单一般不用--%><form action="/student?method=update" method="post" onsubmit="return update_confirm()"><%--默认就是get ?method=就是get方法传参 会被覆盖 所以form表单一般不用--%><input type="hidden" name="id" value="<%=student.getId()%>"/><%--<input type="hidden" name="id" value="<%=student.getId()%>"/>--%><%--readonly 可以删掉F12!! 以后学后端验证--%>姓名:<input type="text" name="name" value="<%=student.getName()%>"><br>年龄:<input type="text" name="age" value="<%=student.getAge()%>"><br>性别:<input type="text" name="gender" value="<%=student.getGender()%>"><br>班级:<select name="banjiId"><%for (Banji banji : banjiList) {String selected = (student.getBanjiId() == banji.getId()) ? "selected" : "";%><option value="<%=banji.getId()%>" <%=selected%>><%=banji.getName()%></option><%}%></select><br><input type="submit" value="更新"></form><script>function update_confirm(){return confirm("您确认要更新吗?");}</script>
</body>
</html>
这篇关于Day19_学点儿JavaEE_学生表和班级表连接查询并显示、带查询班级表的学生信息添加、带查询班级并回显的学生信息修改的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!