JSP(Java Server Pages)是一种基于Java技术的服务器端编程技术,用于创建动态网页。一个成绩管理系统的JSP代码通常涉及以下几个方面:
以下是一个简单的JSP成绩管理系统示例,包括学生信息展示和成绩录入功能。
CREATE TABLE students (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
age INT,
gender VARCHAR(10)
);
CREATE TABLE scores (
id INT AUTO_INCREMENT PRIMARY KEY,
student_id INT,
subject VARCHAR(100),
score DECIMAL(5, 2),
FOREIGN KEY (student_id) REFERENCES students(id)
);
<%@ page import="java.sql.*" %>
<html>
<head>
<title>学生信息</title>
</head>
<body>
<h1>学生信息</h1>
<table border="1">
<tr>
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
</tr>
<%
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM students");
while (rs.next()) {
%>
<tr>
<td><%= rs.getInt("id") %></td>
<td><%= rs.getString("name") %></td>
<td><%= rs.getInt("age") %></td>
<td><%= rs.getString("gender") %></td>
</tr>
<%
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (conn != null) conn.close();
}
%>
</table>
</body>
</html>
<%@ page import="java.sql.*" %>
<html>
<head>
<title>录入成绩</title>
</head>
<body>
<h1>录入成绩</h1>
<form action="add_score_process.jsp" method="post">
学生ID: <input type="text" name="student_id"><br>
科目: <input type="text" name="subject"><br>
成绩: <input type="text" name="score"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
<%@ page import="java.sql.*" %>
<html>
<head>
<title>成绩录入处理</title>
</head>
<body>
<%
int student_id = Integer.parseInt(request.getParameter("student_id"));
String subject = request.getParameter("subject");
double score = Double.parseDouble(request.getParameter("score"));
Connection conn = null;
PreparedStatement pstmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
String sql = "INSERT INTO scores (student_id, subject, score) VALUES (?, ?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, student_id);
pstmt.setString(2, subject);
pstmt.setDouble(3, score);
pstmt.executeUpdate();
out.println("成绩录入成功!");
} catch (Exception e) {
out.println("成绩录入失败:" + e.getMessage());
} finally {
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
}
%>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
。通过以上示例和解决方案,您可以构建一个基本的JSP成绩管理系统。根据实际需求,可以进一步扩展和优化功能。
领取专属 10元无门槛券
手把手带您无忧上云