jsp中模糊查詢
<%@ 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">
請選擇查詢操作欄位:
<select name="mtype">
<option value="studentid">studentid</option>
<option value="studentname">studentname</option>
<option value="gender">gender</option>
<option value="phone">phone</option>
</select>
<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>
值得體會