程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> JSP編程 >> 關於JSP >> jsp自定義標簽下拉框

jsp自定義標簽下拉框

編輯:關於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.jsp.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"
                         "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>
 
 摘自xinghui_liu的專欄

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved