jsp自定义标签下拉框
package com.soft.test;
import java.sql.*;
import sun.jdbc.odbc.ee.connectionpool;
public class basedao {
//定义数据源驱动
private static final string drive="oracle.jdbc.driver.oracledriver";
//定义连接字符串
private static final string url="jdbc:oracle:thin:@10.72.240.34:1522:ffv2dev2";
//用户名
private static final string uid="produsr";
//密码
private static final string pwd="prod_123";
//获得连接
public static connection getconnection()
{
connection con=null;
try {
//加载驱动
class.forname(drive);
//建立连接
con=drivermanager.getconnection(url,uid,pwd);
} catch (exception e) {
e.printstacktrace();
}
return con;
}
//运行有结果,没有参数的sql语句
public static resultset resultrunselectsql(string sql)
{
connection con=null;
preparedstatement ps=null;
resultset res=null;
try
{
con=getconnection();
ps=con.preparestatement(sql);
res=ps.executequery();
}
catch(exception e)
{
e.printstacktrace();
}
return res;
}
//执行有结果有参数的sql语句
public static resultset runselectsql(string sql,object[] params)
{
connection con=null;
preparedstatement pre=null;
resultset res=null;
try {
con=getconnection();
pre=con.preparestatement(sql);
for(int i=0;i<params.length;i++)
{
pre.setobject(i+1, params[i]);
}
res=pre.executequery();
} catch (exception e) {
// todo auto-generated catch block
e.printstacktrace();
}
return res;
}
//运行没有结果,没有参数的sql语句
public static boolean runupdatesql(string sql)
{
connection con=null;
preparedstatement ps=null;
resultset res=null;
try
{
con=getconnection();
ps=con.preparestatement(sql);
ps.executeupdate();
return true;
}
catch(exception e)
{
e.printstacktrace();
return false;
}
}
//执行有结果有参数的sql语句
public static boolean runupdatesql(string sql,object[] params)
{
connection con=null;
preparedstatement pre=null;
try {
con=getconnection();
pre=con.preparestatement(sql);
for(int i=0;i<params.length;i++)
{
pre.setobject(i+1, params[i]);
}
pre.executeupdate();
return true;
} catch (exception e) {
// todo auto-generated catch block
e.printstacktrace();
return false;
}
}
}
[java]
package com.soft.test;
import java.io.ioexception;
import java.sql.connection;
import java.sql.preparedstatement;
import java.sql.resultset;
import java.sql.sqlexception;
import javax.servlet..jspexception;
import javax.servlet.jsp.jspwriter;
import javax.servlet.jsp.tagext.tagsupport;
public class mytag extends tagsupport{
private string tablename;//表明
private string label;//下拉框要显示的名称
private string value;//下拉框的值
private string where;//条件
private string selectname;//下拉框的名称
private string selectid;//下拉款的id
@override
public int doendtag() throws jspexception {
// todo auto-generated method stub
jspwriter out=this.pagecontext.getout();
string sql="select "+label+","+value+" from "+tablename+" "+where+"";//定义sql语句
connection conn=basedao.getconnection();
try {
preparedstatement ps=conn.preparestatement(sql);
resultset res=ps.executequery();
out.print("<select id=\""+selectid+"\" name=\""+selectname+"\">");
out.print("<option value=\"\">请选择</option>");
while(res.next()){
object values=res.getobject(value);
object labels=res.getobject(label);
out.print("<option value=\""+values+"\">"+labels+"</option>");
}
out.print("</select>");
} catch (sqlexception e) {
// todo auto-generated catch block
e.printstacktrace();
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace();
}finally{
}
return super.doendtag();
}
public string gettablename() {
return tablename;
}
public void settablename(string tablename) {
this.tablename = tablename;
}
public string getlabel() {
return label;
}
public void setlabel(string label) {
this.label = label;
}
public string getvalue() {
return value;
}
public void setvalue(string value) {
this.value = value;
}
public string getwhere() {
return where;
}
public void setwhere(string where) {
this.where = where;
}
public string getselectname() {
return selectname;
}
public void setselectname(string selectname) {
this.selectname = selectname;
}
public string getselectid() {
return selectid;
}
public void setselectid(string selectid) {
this.selectid = selectid;
}
}
[java]
<?xml version="1.0" encoding="utf-8"?>
<!doctype taglib public "-//sun microsystems, inc.//dtd jsp tag library 1.2//en"
"https://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>s</short-name>
<uri>https://www.574394550.com</uri>
<tag>
<name>select</name>
<tag-class>com.soft.test.mytag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>tablename</name>
<required>true</required>
</attribute>
<attribute>
<name>label</name>
<required>true</required>
</attribute>
<attribute>
<name>value</name>
<required>true</required>
</attribute>
<attribute>
<name>where</name>
<required>true</required>
</attribute>
<attribute>
<name>selectname</name>
<required>true</required>
</attribute>
<attribute>
<name>selectid</name>
<required>true</required>
</attribute>
</tag>
</taglib>
[java]
<%@ page language="java" pageencoding="gbk"%>
<%@ taglib uri="" prefix="s" %>
<%
string path = request.getcontextpath();
%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
<head>
<title>my jsp 'index.jsp' starting page</title>
</head>
<body>
this is my jsp page. <br>
<s:select selectname="select" selectid="select" label="user_name" value="user_id" tablename="tu_oaf_users" where="where 1=1"/>
</body>
</html>
摘自xinghui_liu的专栏
下一篇: 工具集 -- sh文件的编写和使用