Jsp custom tag drop-down box

Source: Internet
Author: User

Package com. soft. test;
 
Import java. SQL .*;
 
Import sun. jdbc. odbc. ee. ConnectionPool;
 
Public class BaseDao {
// Define the data source driver
Private static final String drive = "oracle. jdbc. driver. OracleDriver ";
// Define the connection string
Private static final String url = "jdbc: oracle: thin: @ 10.72.240.34: 1522: ffv2dev2 ";
// User Name
Private static final String uid = "produsr ";
// Password
Private static final String pwd = "prod_123 ";
// Obtain the connection
Public static Connection getConnection ()
{
Connection con = null;
Try {
// Load the driver
Class. forName (drive );
// Establish a connection
Con = DriverManager. getConnection (url, uid, pwd );
} Catch (Exception e ){
E. printStackTrace ();
}
Return con;
}
// SQL statement with no parameters
Public static ResultSet ResultrunSelectSql (String SQL)
{
Connection con = null;
PreparedStatement ps = null;
ResultSet res = null;
Try
{
Con = getConnection ();
Ps = con. prepareStatement (SQL );
Res=ps.exe cuteQuery ();
}
Catch (Exception e)
{
E. printStackTrace ();
}
Return res;
}
// Execute an SQL statement with Parameters
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.exe cuteQuery ();
} Catch (Exception e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
Return res;
}

// SQL statement with no results or parameters
Public static boolean runUpdateSql (String SQL)

{
Connection con = null;
PreparedStatement ps = null;
ResultSet res = null;
Try
{
Con = getConnection ();
Ps = con. prepareStatement (SQL );

Ps.exe cuteUpdate ();
Return true;

}
Catch (Exception e)
{
E. printStackTrace ();
Return false;
}


}
// Execute an SQL statement with Parameters
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.exe cuteUpdate ();
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. jsp. JspException;
Import javax. servlet. jsp. JspWriter;
Import javax. servlet. jsp. tagext. TagSupport;
 
Public class MyTag extends TagSupport {
Private String tableName; // indicates
Private String label; // name to be displayed in the drop-down list
Private String value; // The value of the drop-down list.
Private String where; // Condition
Private String selectName; // The name of the drop-down list.
Private String selectId; // the ID of the drop-down list
@ Override
Public int doEndTag () throws JspException {
// TODO Auto-generated method stub
JspWriter out = this. pageContext. getOut ();
String SQL = "select" + label + "," + value + "from" + tableName + "" + where + "; // define an SQL statement
Connection conn = BaseDao. getConnection ();
Try {
PreparedStatement ps = conn. prepareStatement (SQL );
ResultSet res=ps.exe cuteQuery ();
Out. print ("<select id = \" "+ selectId +" \ "name = \" "+ selectName +" \ "> ");
Out. print ("<option value = \" \ "> select </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"
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> s </short-name>
<Uri> http://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 = "http://www.bkjia.com" 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>
 
From xinghui_liu's column

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.