写一个jsp文件,调用数据库,输入学号,输出其他信息
时间: 2024-06-01 22:10:02 浏览: 10
<%@ page import="java.sql.*" %>
<%
//获取学号参数
String studentId = request.getParameter("studentId");
//连接数据库
String url = "jdbc:mysql://localhost:3306/db_name";
String username = "root";
String password = "password";
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(url, username, password);
//查询学生信息
String sql = "SELECT * FROM student WHERE student_id = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, studentId);
ResultSet rs = ps.executeQuery();
//输出学生信息
if (rs.next()) {
out.println("学号:" + rs.getString("student_id"));
out.println("姓名:" + rs.getString("name"));
out.println("性别:" + rs.getString("gender"));
out.println("年龄:" + rs.getInt("age"));
out.println("专业:" + rs.getString("major"));
} else {
out.println("该学生不存在");
}
//关闭数据库连接
rs.close();
ps.close();
conn.close();
%>
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)