编写4个JSP页面: searchStu. jsp、byNumber. jsp .byName. jspallStu.jsp页面。searchStu. jsp的具体要求: searchStu. jsp提供两个表单。其中一个表单允许用户输人要查询的学生的学号,即输人stuinfo表中xh字段的查询条件,然后将查询条件提交给要byXh.jsp,另一个表单允许用户输人要查询的学生的姓名,即输人stuinfo表中name字段的查询条件,然后将查询条件提交给要byName. jsp. byXh. jsp的具体要求: byXh. jsp首先获得searchStu. jsp提交的关于xh字段的查询条件然后使用JDBC查询。 byName. jsp的具体要求: byName. jsp首先获得searchStu. jsp提交的关于name字段的查询条件,然后使用JDBC查询。
时间: 2024-02-23 13:58:03 浏览: 135
好的,以下是四个JSP页面的代码。
1. searchStu.jsp 页面:
```
<!DOCTYPE html>
<html>
<head>
<title>Search Students</title>
</head>
<body>
<h1>Search Students</h1>
<form action="byNumber.jsp" method="get">
<label>Search by Number:</label>
<input type="text" name="number" />
<input type="submit" value="Search" />
</form>
<form action="byName.jsp" method="get">
<label>Search by Name:</label>
<input type="text" name="name" />
<input type="submit" value="Search" />
</form>
</body>
</html>
```
2. byNumber.jsp 页面:
```
<%@ page language="java" %>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<title>Search Students by Number</title>
</head>
<body>
<h1>Search Students by Number</h1>
<%
// Get the number parameter from the request
String number = request.getParameter("number");
// Connect to the database
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/students";
String username = "root";
String password = "password";
conn = DriverManager.getConnection(url, username, password);
// Query the database
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM stuinfo WHERE xh = ?");
stmt.setString(1, number);
ResultSet rs = stmt.executeQuery();
// Display the results
while (rs.next()) {
out.println("Number: " + rs.getString("xh") + " Name: " + rs.getString("name"));
}
// Close the database connection
rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
```
3. byName.jsp 页面:
```
<%@ page language="java" %>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<title>Search Students by Name</title>
</head>
<body>
<h1>Search Students by Name</h1>
<%
// Get the name parameter from the request
String name = request.getParameter("name");
// Connect to the database
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/students";
String username = "root";
String password = "password";
conn = DriverManager.getConnection(url, username, password);
// Query the database
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM stuinfo WHERE name = ?");
stmt.setString(1, name);
ResultSet rs = stmt.executeQuery();
// Display the results
while (rs.next()) {
out.println("Number: " + rs.getString("xh") + " Name: " + rs.getString("name"));
}
// Close the database connection
rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
```
4. allStu.jsp 页面:
```
<%@ page language="java" %>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<title>All Students</title>
</head>
<body>
<h1>All Students</h1>
<%
// Connect to the database
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/students";
String username = "root";
String password = "password";
conn = DriverManager.getConnection(url, username, password);
// Query the database
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM stuinfo");
// Display the results
while (rs.next()) {
out.println("Number: " + rs.getString("xh") + " Name: " + rs.getString("name"));
}
// Close the database connection
rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
```
在浏览器中访问 searchStu.jsp 页面,即可开始查询学生信息。
阅读全文