根据提交页面参数查询数据库中的数据

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html>
 4 <html>
 5 <head>
 6 <meta charset="UTF-8">
 7 <title>查询页面提交</title>
 8 </head>
 9 <body>
10 请选择查询条件<hr width="100%" size="3">
11 <form action="find_stu_3.jsp" method="post">
12 性别:男<input type="radio" value="男" name="sex" checked="checked">
13 女<input type="radio" value="女" name="sex"><br><br>
14 
15 体重范围:<p>
16 最小<input type="text" name="w1" value="0"><br><br>
17 最大<input type="text" name="w2" value="150"><br><br>
18 <input type="submit" value="提 交">
19 <input type="reset" value="取 消">
20 
21 </form>
22 </body>
23 </html>
 1 <%@page import="java.sql.*"%>
 2 <%@ page language="java" contentType="text/html; charset=UTF-8"
 3     pageEncoding="UTF-8"%>
 4 <!DOCTYPE html>
 5 <html>
 6 <head>
 7 <meta charset="UTF-8">
 8 <title>由提交页面获取查询条件并实现查询的页面</title>
 9 </head>
10 <body>
11 <center>
12  <%
13          String className="com.mysql.jdbc.Driver";
14          String user="root";
15          String password="root";
16          String url="jdbc:mysql://localhost:3306/db_shangke";
17          
18          Class.forName(className);
19          Connection conn= DriverManager.getConnection(url, user, password);
20          request.setCharacterEncoding("UTF-8");
21          String sex=request.getParameter("sex");
22          float weight1=Float.parseFloat(request.getParameter("w1"));
23          float weight2=Float.parseFloat(request.getParameter("w2"));
24           String sql="select *from stu_info where sex=? and weight>=? and weight<=?";
25           PreparedStatement pstmt=conn.prepareStatement(sql);
26           
27           pstmt.setString(1, sex);
28           pstmt.setFloat(2, weight1);
29           pstmt.setFloat(3, weight2);
30           
31           ResultSet rs=pstmt.executeQuery();
32           rs.last();%>你要查询的学生数据表中共有
33           <font size="5" color="red">
34           <%=rs.getRow()%>
35           </font>36           <table border="2" bgcolor="ccceee" width="650">
37             <tr bgcolor="CCCCCC" align="center">
38                         <td>记录条数</td>
39                         <td>学号</td>
40                         <td>姓名</td>
41                         <td>性别</td>
42                         <td>年龄</td>
43                         <td>体重</td>
44              </tr>
45              <%
46                 rs.beforeFirst();
47              while(rs.next()){
48                  
49              %>
50              <tr align="center">
51                           <td><%=rs.getRow() %> </td>
52                           <td><%=rs.getString("id")%> </td>            
53                           <td><%=rs.getString("name") %> </td>             
54                           <td><%=rs.getString("sex")%> </td>            
55                           <td><%=rs.getInt("age") %> </td>            
56                           <td><%=rs.getDouble("weight") %> </td>
57                           <td><%=rs.getDouble("height")%> </td>           
58              </tr>
59              <%} %>
60           </table>
61  </center>
62 <%
63       if(rs!=null){
64           rs.close();
65       }
66      if(pstmt!=null){
67          pstmt.close();
68      }
69      if(conn!=null){
70          conn.close();
71      }
72 %>
73 </body>
74 </html>

 

posted @ 2021-04-18 16:01  丁帅帅dss  阅读(145)  评论(0)    收藏  举报