写一个jsp文件,调用MySQL数据库,输入学号,输出其他信息
时间: 2024-05-29 09:10:36 浏览: 11
<%@ page import="java.sql.*" %>
<%
//获取学号参数
String studentId = request.getParameter("studentId");
//连接MySQL数据库
String url = "jdbc:mysql://localhost:3306/mydb";
String user = "root";
String password = "123456";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, user, password);
stmt = conn.createStatement();
//查询学生信息
String sql = "SELECT * FROM student WHERE student_id='" + studentId + "'";
rs = stmt.executeQuery(sql);
if (rs.next()) {
//输出学生信息
out.print("姓名:" + rs.getString("name") + "<br>");
out.print("性别:" + rs.getString("gender") + "<br>");
out.print("年龄:" + rs.getInt("age") + "<br>");
out.print("班级:" + rs.getString("class") + "<br>");
} else {
out.print("未找到该学生信息!");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭连接
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
%>