例子5:排序查询数据库中表的数据(JSP)

本文介绍了一个使用JSP和Java实现的动态网页排序查询功能。通过用户选择的价格或生产日期进行排序,利用自定义标签处理数据库查询并展示结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>
  排序查询:
  <br><input type="radio" name="orderType"value="price">按价格排序
  <input type="radio" name="orderType"value="madeTime">按生产日期排序
  <br><input type=submit name="g" value="提交">
  </form>
  <% String orderType=request.getParameter("orderType");
  if(orderType==null)
  orderType=""; %>
  <inquire:SortTag orderType="<%=orderType%>"/>
  根据排序方式:<%=orderType %>,排序的记录:
  <br/><%=orderResult %>
  </body>
</html>

oederTag.tag

<%@ tag pageEncoding="GB2312" %>
<%@ tag import="java.sql.*" %>
<%@ attribute name="orderType" required="true" %>
<%@ variable name-given="orderResult" scope="AT_END" %>
<%  String orderCondition=
           "SELECT * FROM product ORDER BY "+orderType;
    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";
          con=DriverManager.getConnection(uri,"root","");
          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(orderCondition);
          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("");
    }
     //返回orderResult对象 :
    jspContext.setAttribute("orderResult",new String(result));
%>

文件目录结构显示

运行效果显示

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值