欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

自定义标签---数据库条件查询标签

程序员文章站 2022-06-09 12:02:07
...

后台代码(直接后台拼接)

package com.yydb.tag;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.yydb.util.DBUtil;

public class QueryTag extends TagSupport{
	private String tableName;
	private String conditionName;
	private String conditionVal;
	public String getTableName() {
		return tableName;
	}
	public void setTableName(String tableName) {
		this.tableName = tableName;
	}
	public String getConditionName() {
		return conditionName;
	}
	public void setConditionName(String conditionName) {
		this.conditionName = conditionName;
	}
	public String getConditionVal() {
		return conditionVal;
	}
	public void setConditionVal(String conditionVal) {
		this.conditionVal = conditionVal;
	}
	@Override
	public int doStartTag() throws JspException {
		
		DBUtil db = new DBUtil();
		Connection conn = db.getConn();
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		String sql = "select * from "+tableName+" where "+conditionName+" = "+conditionVal;
		
		try {
			ps = conn.prepareStatement(sql);
			ResultSetMetaData rsmd = ps.getMetaData();
			int cloumn = rsmd.getColumnCount();
			JspWriter out = pageContext.getOut();
			
			out.print("<table border='1' cellspacing='1' cellpadding='1'>");
			out.print("<tr>");
			for (int i = 0; i < cloumn; i++) {
				out.print("<th>"+rsmd.getColumnName(i+1)+"</th>");
			}
			out.print("</tr>");
			
			rs = ps.executeQuery();
			
			while(rs.next()){
				out.print("<tr>");
				for (int i = 0; i < cloumn; i++) {
					out.print("<td>"+rs.getObject(i+1)+"</td>");
				}
				
				out.print("</tr>");
			}
			out.print("</table>");
			
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return super.SKIP_BODY;
	}
	
	
}

tld设置:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
                        "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
	 <tlib-version>1.0</tlib-version>
	 <jsp-version>1.2</jsp-version>
	 <short-name>mt</short-name>
	 <uri>www.yydb</uri>
	 <tag>
	 	<name>QueryTag</name>
	 	<tag-class>com.yydb.tag.QueryTag</tag-class>
	 	<body-content>jsp</body-content>
	 	<attribute>
	 		<name>tableName</name>
	 		<required>true</required>
	 		<rtexprvalue>true</rtexprvalue>
	 	</attribute>
	 	<attribute>
	 		<name>conditionName</name>
	 		<required>true</required>
	 		<rtexprvalue>true</rtexprvalue>
	 	</attribute>
	 	<attribute>
	 		<name>conditionVal</name>
	 		<required>true</required>
	 		<rtexprvalue>true</rtexprvalue>
	 	</attribute>
	 </tag>
 </taglib>

jsp中引入标签库

<%@ taglib uri="www.yydb" prefix="mt" %>

jsp中引入自定义标签

<mt:QueryTag tableName="admin" conditionName="username" conditionVal="23786"></mt:QueryTag>