?? querytag.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 + -