1、文件目录结构图示
2、运行效果图示
3、代码显示
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ taglib tagdir="/WEB-INF/tags" prefix="inquire" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<FORM action="" method=post name=form>
选择:<Select name="ziduan">
<Option value="name">产品名称
<Option value="number">产品号 </Option>
</Select>
含有:<INPUT type="text" name="keyWord" >
<Input type=submit name="g" value="提交">
</Form>
<% String ziduan=request.getParameter("ziduan");
String keyWord=request.getParameter("keyWord");
if(ziduan==null||keyWord==null){
ziduan="";
keyWord="";
}
byte bb[]=keyWord.getBytes("iso-8859-1");
keyWord=new String(bb);
out.println("关键字 "+keyWord);
%>
<inquire:KeyWord ziduan="<%=ziduan%>" keyWord="<%=keyWord%>" />
查询到的记录:
<BR> <%=foundResult%>
</body>
</html>
KeyWord.jsp
<%@ tag pageEncoding="GB2312" %>
<%@ tag import="java.sql.*" %>
<%@ attribute name="keyWord" required="true" %>
<%@ attribute name="ziduan" required="true" %>
<%@ variable name-given="foundResult" scope="AT_END" %>
<%
String condition="SELECT * FROM product Where "+ziduan+
" like '%"+keyWord+"%'";
StringBuffer result;
result=new StringBuffer();
try{ Class.forName("com.mysql.jdbc.Driver");
}
catch(Exception e){}
Connection con;
Statement sql;
ResultSet rs;
int n=0;
try{ result.append("<table border=1>");
String uri=
"jdbc:mysql://127.0.0.1/warehouse?"+
"user=root&password=&characterEncoding=gb2312";
con=DriverManager.getConnection(uri);
DatabaseMetaData metadata=con.getMetaData();
ResultSet rs1=metadata.getColumns(null,null,"product",null);
int 字段个数=0;
result.append("<tr>");
while(rs1.next()){
字段个数++;
String clumnName=rs1.getString(4);
result.append("<td>"+clumnName+"</td>");
}
result.append("</tr>");
sql=con.createStatement();
rs=sql.executeQuery(condition);
while(rs.next()){
result.append("<tr>");
for(int k=1;k<=字段个数;k++)
result.append("<td>"+rs.getString(k)+"</td>");
result.append("</tr>");
}
result.append("</table>");
con.close();
}
catch(SQLException e){
result.append("请输入模糊查询条件");
}
//返回foundResult对象:
jspContext.setAttribute("foundResult",new String(result));
%>