将要查询的条件通过该页面提交给查询页面,在该页面中获取所提交的信息,并将这些信息作为SQL语句的参数信息,查询结束后,显示出所有满足条件的记录。
提交页面:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<body>
请选择提交条件<hr width="100%" size="3">
<form action="find_stu_3.jsp" method="post">
性别:男<input type="radio" value="man" name="sex" checked="checked">
女:<input type="radio" value="woman" name="sex"><br><br>
体重范围:<p>
最小<input type="text" name="w1" value="0"><br><br>
最大<input type="text" name="w2" value="150"><p>
<input type="submit" value="提 交">
<input type="reset" value="取 消">
</form>
</body>
</html>
获取提交页面信息,并实现查询和信息显示的程序:
<%@ page language="java" import="java.sql.*" pageEncoding="utf-8"%>
<html>
<head>
<body>
<%String driverName="com.mysql.jdbc.Driver";
String userName="root";
String userPwd="123456";
String dbName="student";
String url1="jdbc:mysql://localhost:3306/"+dbName;
String url2="?user="+userName+"&password="+userPwd;
String url3="&useUnicode=true&characterEncoding=UTF-8";
String url=url1+url2+url3;
Class.forName(driverName);
Connection conn=DriverManager.getConnection(url);
request.setCharacterEncoding("utf-8");
String sex=request.getParameter("sex");
float weight1=Float.parseFloat(request.getParameter("w1"));
float weight2=Float.parseFloat(request.getParameter("w2"));
String sql="select * from stu_info where sex=? and weight>=? and weight<=?";
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setString(1, sex);
pstmt.setFloat(2, weight1);
pstmt.setFloat(3, weight2);
ResultSet rs=pstmt.executeQuery();
rs.last();
%>你要查询的学生数据表中共有
<font size="5" color="red"><%=rs.getRow() %></font>人
<table border="2" bgcolor="ccceee" width="650">
<tr bgcolor="CCCCCC" align="center">
<td>记录条数</td><td>学号</td>姓名<td></td><td>性别</td>
<td>年龄</td><td>体重</td><td>身高</td>
</tr>
<%rs.beforeFirst();
while(rs.next()){
%> <tr align="center">
<td><%=rs.getRow() %></td>
<td><%=rs.getString("id") %></td>
<td><%=rs.getString("name") %></td>
<td><%=rs.getString("sex") %></td>
<td><%=rs.getString("age") %></td>
<td><%=rs.getString("weight") %></td>
<td><%=rs.getString("hight") %></td>
</tr>
<%} %>
</table>
</center>
<%if(rs!=null){rs.close();}
if(pstmt!=null){pstmt.close();}
if(conn!=null){conn.close();}
%>
</body>
</html>