编写4个JSP页面: searchStu. jsp、byNumber. jsp .byName. jspallStu.jsp页面。searchStu. jsp的具体要求: searchStu. jsp提供两个表单。其中一个表单允许用户输人要查询的学生的学号,即输人stuinfo表中xh字段的查询条件,然后将查询条件提交给要byXh.jsp,另一个表单允许用户输人要查询的学生的姓名,即输人stuinfo表中name字段的查询条件,然后将查询条件提交给要byName. jsp.
时间: 2024-02-10 17:10:33 浏览: 118
好的,我理解了你的要求。以下是四个JSP页面的代码:
1. searchStu.jsp
```
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>查询学生信息</title>
</head>
<body>
<h1>查询学生信息</h1>
<form action="byNumber.jsp" method="get">
<label for="xh">请输入学生的学号:</label>
<input type="text" name="xh" id="xh">
<input type="submit" value="查询">
</form>
<form action="byName.jsp" method="get">
<label for="name">请输入学生的姓名:</label>
<input type="text" name="name" id="name">
<input type="submit" value="查询">
</form>
</body>
</html>
```
2. byNumber.jsp
```
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>按学号查询学生信息</title>
</head>
<body>
<h1>按学号查询学生信息</h1>
<%
String xh = request.getParameter("xh");
String sql = "SELECT * FROM stuinfo WHERE xh=?";
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test?useSSL=false";
String user = "root";
String password = "root";
Connection conn = DriverManager.getConnection(url, user, password);
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, xh);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
out.println("学号:" + rs.getString("xh") + "<br>");
out.println("姓名:" + rs.getString("name") + "<br>");
out.println("性别:" + rs.getString("sex") + "<br>");
out.println("年龄:" + rs.getInt("age") + "<br>");
} else {
out.println("没有找到学号为 " + xh + " 的学生!");
}
rs.close();
ps.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
%>
<br>
<a href="searchStu.jsp">返回</a>
</body>
</html>
```
3. byName.jsp
```
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>按姓名查询学生信息</title>
</head>
<body>
<h1>按姓名查询学生信息</h1>
<%
String name = request.getParameter("name");
String sql = "SELECT * FROM stuinfo WHERE name=?";
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test?useSSL=false";
String user = "root";
String password = "root";
Connection conn = DriverManager.getConnection(url, user, password);
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, name);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
out.println("学号:" + rs.getString("xh") + "<br>");
out.println("姓名:" + rs.getString("name") + "<br>");
out.println("性别:" + rs.getString("sex") + "<br>");
out.println("年龄:" + rs.getInt("age") + "<br><br>");
}
if (!rs.first()) {
out.println("没有找到姓名为 " + name + " 的学生!");
}
rs.close();
ps.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
%>
<br>
<a href="searchStu.jsp">返回</a>
</body>
</html>
```
4. allStu.jsp
```
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>查询所有学生信息</title>
</head>
<body>
<h1>查询所有学生信息</h1>
<%
String sql = "SELECT * FROM stuinfo";
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test?useSSL=false";
String user = "root";
String password = "root";
Connection conn = DriverManager.getConnection(url, user, password);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
out.println("学号:" + rs.getString("xh") + "<br>");
out.println("姓名:" + rs.getString("name") + "<br>");
out.println("性别:" + rs.getString("sex") + "<br>");
out.println("年龄:" + rs.getInt("age") + "<br><br>");
}
rs.close();
stmt.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
%>
<br>
<a href="searchStu.jsp">返回</a>
</body>
</html>
```
其中,我假设你使用MySQL数据库,并且数据库名为test,用户名和密码均为root。如果不是这样,请根据实际情况修改代码中的相关信息。另外,为了安全起见,建议将数据库连接的相关信息存放在配置文件中,并且使用连接池来管理数据库连接。
阅读全文