jsp中模糊查询

来源:互联网 发布:淘宝美孚机油是真的吗 编辑:程序博客网 时间:2024/05/16 04:59

<%@ page contentType="text/html;charset=gb2312" %>
<%@ page language="java" import="java.sql.*"%>
<jsp:useBean id="connc" scope="page" class="db.ConnOracle"/>
<%
String sql = "";
String key = "";
String type= "";
String sortfield = "";
%>
<%
request.setCharacterEncoding("gb2312");
sql = "select * from student";
key = request.getParameter("mkey");
System.out.print(key);
type = request.getParameter("mtype");
sortfield = request.getParameter("sortfield");
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

 <title>test db connection</title>
</head>
<body bgcolor="#FFFFFF">
<div align="center">
<form action="d_sql2.jsp" method="post">
    查询关键字:<Input type="text" name="mkey">&nbsp;&nbsp;&nbsp;
 请选择查找操作字段:
 <select name="mtype">
  <option value="studentid">studentid</option>
  <option value="studentname">studentname</option>
  <option value="gender">gender</option>
  <option value="phone">phone</option>
 </select>&nbsp;&nbsp;&nbsp;
 <Input type="submit" value="查看">
</form>
<hr>
  <table width="60%" border="1">
    <tr bgcolor="#CCCCFF">
      <td width="25%">
        <div align="center"><font color="#FF0033"><b>
    <a href="d_sql2.jsp?mkey=<%=key%>&mtype=<%=type%>&studentid">学号</a>
  </b></font></div>
      </td>
      <td width="25%">
        <div align="center"><font color="#FF0033"><b>
    <a href="d_sql2.jsp?mkey=<%=key%>&mtype=<%=type%>&studentname">姓名</a>
  </b></font></div>
      </td>
      <td width="25%">
        <div align="center"><font color="#FF0033"><b>
    <a href="d_sql2.jsp?mkey=<%=key%>&mtype=<%=type%>&sortfield=gender">性别</a>
  </b></font></div>
      </td>
      <td width="25%">
        <div align="center"><font color="#FF0033"><b>
    <a href="d_sql2.jsp?mkey=<%=key%>&m-type=<%=type%>&sortfield=phone">电话</a>
  </b></font></div>
      </td>
    </tr>
<%
if(key != null && !key.equals("") && type != null && !type.equals(""))
    sql += " where " + type + " like '%" + key + "%' ";
if(sortfield != null && !sortfield.equals("")) 
 sql += " order by " + sortfield ;

ResultSet RS_result = connc.executeQuery( sql );
String studentName="";
String studentSex="";
String studentScore="";
String phone="";
while(RS_result.next())
{
studentName = RS_result.getString("studentid");
studentSex = RS_result.getString("studentname");
studentScore = RS_result.getString("gender");
phone=RS_result.getString("phone");
%>
    <tr>
      <td width="25%" bgcolor="#FFFFFF">
        <div align="center"><%=studentName%></div>
      </td>
      <td width="25%">
        <div align="center"><%=studentSex%></div>
      </td>
      <td width="25%">
       <div align="center"><%=studentScore%></div>
      </td>
 <td width="25%">
       <div align="center"><%=phone%></div>
      </td>
    </tr>
<%
}
RS_result.close();
%>
  </table>
</div>
</body>
</html>
值得体会

原创粉丝点击