写一个java server page文件,调用mysql数据库,输入学号,输出其他信息
时间: 2024-06-02 10:14:23 浏览: 60
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Student Information</title>
</head>
<body>
<h1>Student Information</h1>
<form method="post">
<label for="studentId">Student ID:</label>
<input type="text" id="studentId" name="studentId">
<input type="submit" value="Submit">
</form>
<%
String studentId = request.getParameter("studentId");
if (studentId != null && !studentId.isEmpty()) {
try {
// Connect to MySQL database
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "username", "password");
// Query student information
String query = "SELECT * FROM students WHERE id = ?";
PreparedStatement stmt = conn.prepareStatement(query);
stmt.setString(1, studentId);
ResultSet rs = stmt.executeQuery();
// Output student information
if (rs.next()) {
out.println("<h2>Student Information:</h2>");
out.println("<ul>");
out.println("<li>Student ID: " + rs.getString("id") + "</li>");
out.println("<li>Name: " + rs.getString("name") + "</li>");
out.println("<li>Major: " + rs.getString("major") + "</li>");
out.println("<li>GPA: " + rs.getDouble("gpa") + "</li>");
out.println("</ul>");
} else {
out.println("<p>Student not found.</p>");
}
// Close database connection
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
out.println("<p>Error: " + e.getMessage() + "</p>");
}
}
%>
</body>
</html>
阅读全文