编写一个学生成绩查询成绩页面,要求能够根据分数段与班级查询学生成绩。要求数据库采用h2,数据库路径为d:/temp/teaching,表名为student,表结构为:no(学号,字符串),name(姓名,字符串),score(成绩,整数),clazz(班级,字符串)
时间: 2024-05-08 19:20:48 浏览: 118
以下是一个简单的学生成绩查询页面示例,使用Java Servlet和JSP技术实现。其中,使用了H2数据库和JDBC连接数据库,实现了根据分数段与班级查询学生成绩。
首先,需要在项目中引入H2数据库的依赖。可以在Maven中添加以下依赖:
```xml
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
</dependency>
```
接下来创建一个名为`Student`的Java类,表示学生信息:
```java
public class Student {
private String no; // 学号
private String name; // 姓名
private int score; // 成绩
private String clazz; // 班级
// 省略getter和setter方法
}
```
然后,创建一个名为`StudentDAO`的类,负责与数据库交互,实现学生成绩查询功能:
```java
public class StudentDAO {
private Connection conn;
public StudentDAO() throws SQLException {
// 连接到H2数据库
conn = DriverManager.getConnection("jdbc:h2:d:/temp/teaching");
}
public List<Student> queryByScoreAndClass(int minScore, int maxScore, String clazz) throws SQLException {
List<Student> students = new ArrayList<>();
String sql = "SELECT * FROM student WHERE score BETWEEN ? AND ? AND clazz = ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, minScore);
stmt.setInt(2, maxScore);
stmt.setString(3, clazz);
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
Student student = new Student();
student.setNo(rs.getString("no"));
student.setName(rs.getString("name"));
student.setScore(rs.getInt("score"));
student.setClazz(rs.getString("clazz"));
students.add(student);
}
}
}
return students;
}
// 省略关闭数据库连接的方法
}
```
最后,创建一个JSP页面`query.jsp`,实现查询表单和显示查询结果的功能:
```html
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ page import="javax.servlet.*" %>
<%@ page import="javax.servlet.http.*" %>
<%@ page import="com.example.Student,com.example.StudentDAO" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生成绩查询</title>
</head>
<body>
<h1>学生成绩查询</h1>
<form action="query" method="get">
最低分数:<input type="number" name="minScore" required><br>
最高分数:<input type="number" name="maxScore" required><br>
班级:<input type="text" name="clazz" required><br>
<input type="submit" value="查询">
</form>
<%
String minScoreStr = request.getParameter("minScore");
String maxScoreStr = request.getParameter("maxScore");
String clazz = request.getParameter("clazz");
if (minScoreStr != null && maxScoreStr != null && clazz != null) {
int minScore = Integer.parseInt(minScoreStr);
int maxScore = Integer.parseInt(maxScoreStr);
try {
StudentDAO dao = new StudentDAO();
List<Student> students = dao.queryByScoreAndClass(minScore, maxScore, clazz);
if (!students.isEmpty()) {
%>
<h2>查询结果:</h2>
<table>
<tr>
<th>学号</th>
<th>姓名</th>
<th>成绩</th>
<th>班级</th>
</tr>
<% for (Student student : students) { %>
<tr>
<td><%= student.getNo() %></td>
<td><%= student.getName() %></td>
<td><%= student.getScore() %></td>
<td><%= student.getClazz() %></td>
</tr>
<% } %>
</table>
<%
} else {
%>
<h2>没有查询到符合条件的学生。</h2>
<%
}
} catch (SQLException e) {
%>
<h2>查询失败: <%= e.getMessage() %> </h2>
<%
}
}
%>
</body>
</html>
```
在`web.xml`中配置Servlet,将查询页面`query.jsp`作为默认页面:
```xml
<servlet>
<servlet-name>QueryServlet</servlet-name>
<servlet-class>com.example.QueryServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>QueryServlet</servlet-name>
<url-pattern>/query</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>query.jsp</welcome-file>
</welcome-file-list>
```
最后,创建一个名为`QueryServlet`的Servlet,用于处理查询请求,将查询结果传递给`query.jsp`页面处理:
```java
public class QueryServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
RequestDispatcher dispatcher = req.getRequestDispatcher("query.jsp");
dispatcher.forward(req, resp);
}
}
```
这样,就完成了一个简单的学生成绩查询页面的开发。用户在页面输入查询条件后,点击查询按钮,Servlet会调用`StudentDAO`类查询数据库,将查询结果传递给JSP页面进行显示。
阅读全文