⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 querytag.java

📁 weblogic应用全实例
💻 JAVA
字号:
//声明本类所在的包
package examples.jsp.tagext.sql;
//声明本类引入的其它包
import java.io.*;
import java.sql.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

/**
 * 执行SQL语句
 */
public class QueryTag extends BodyTagSupport {
  
  Connection conn;
  Statement stat;
  String sql;
  
  public void setSql(String qStr) { sql = qStr; }
  public String getSql() { return sql; }
  //实现doStartTag方法
  public int doStartTag() throws javax.servlet.jsp.JspException {
    
    if (id == null) {
      throw new JspException("id attribure for ResultSet was not defined");
    }
    
    try {
      ConnectionTag connTag = (ConnectionTag)
           findAncestorWithClass(this, 
                                 Class.forName("examples.jsp.tagext.sql.ConnectionTag"));
      if (connTag != null) {
        conn = connTag.getConnection();
      }
    } catch(ClassNotFoundException cnfe) {
      throw new JspException("Query tag connection attribute not set AND not nested within connection tag");
    }
    
    if (conn == null) {
      throw new JspException("This query tag must be nested within a connection tag");
    }
    
    try {
      //创建SQL语句	
      stat = conn.createStatement();
      
      ResultSet results = stat.executeQuery(sql);
      pageContext.setAttribute(id, results);
      return EVAL_BODY_TAG;
    } catch (Exception e) {
      throw new JspException("Failed to execute query in QueryTag. Exception caught: "+e);
    } 
  }
  //实现doAfterBody方法
  public int doAfterBody() throws javax.servlet.jsp.JspException
  {
    try {
      getBodyContent().writeOut(getPreviousOut());
    } catch(java.io.IOException ioe) {
      throw new JspException("Failed to write body content");
    }
    return SKIP_BODY;
  }
  //实现doEndTag方法
  public int doEndTag() throws javax.servlet.jsp.JspException
  {
    if (stat != null) {
      try {
        stat.close();
      } catch(Exception e) {
        throw new JspException("Failed to close statement");
      }
    }
    return EVAL_PAGE;
  }
  //释放资源
  public void release() {
    super.release();
    stat = null;
    conn = null;
    sql = null;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -