?? statementimpltag.java
字號(hào):
/*
* Copyright 1999,2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.taglibs.dbtags.statement;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.BodyTagSupport;
/**
* <p>JSP tag statement. According to the TEI, the java.sql.Statement
* object specified in the "id" attribute is available within the scope
* of the statement tags. </p>
*
* <p>JSP Tag Lib Descriptor
* <pre>
* <name>statement</name>
* <tagclass>org.apache.taglibs.dbtags.statement.StatementImplTag</tagclass>
* <teiclass>org.apache.taglibs.dbtags.connection.StatementTEI</teiclass>
* <bodycontent>JSP</bodycontent>
* <info>JSP tag statement, uses the enclosed query and resultset/execute
* tags to perform a database operation.</info>
* <attribute>
* <name>id</name>
* <required>true</required>
* <rtexprvalue>false</rtexprvalue>
* </attribute>
* <attribute>
* <name>conn</name>
* <required>true</required>
* <rtexprvalue>false</rtexprvalue>
* </attribute>
* </pre>
*
* @author Morgan Delagrange
*/
public class StatementImplTag extends BodyTagSupport implements StatementTag{
private Statement _statement = null;
private String _query = null;
private String _connId = null;
// all the public methods are Javadoced
// in their interfaces
public void setQuery(String query) {
_query = query;
}
public void setConn(String connId) {
_connId = connId;
}
public void executeUpdate() throws SQLException {
_statement.executeUpdate(_query);
}
public ResultSet executeQuery() throws SQLException {
return _statement.executeQuery(_query);
}
public int doStartTag() throws JspTagException {
if (_connId == null) {
throw new JspTagException("Connection id has not been set.");
}
try {
Connection conn = (Connection)pageContext.findAttribute(_connId);
if(conn == null) {
throw new JspTagException("There is no such connection'"+_connId+"'");
}
_statement = createStatement ( conn );
pageContext.setAttribute(getId(), _statement);
} catch (SQLException e) {
throw new JspTagException(e.toString());
}
return EVAL_BODY_TAG;
}
/**
* Let subclass redefine how to create the statement
*/
protected Statement createStatement ( Connection theConnection ) throws SQLException
{
return theConnection.createStatement();
}
public int doAfterBody() throws JspTagException
{
// Automatically trim the contents of this tag
try {
getPreviousOut().write(getBodyContent().getString().trim());
} catch (IOException e) {
throw new JspTagException(e.toString());
}
return EVAL_PAGE;
}
public int doEndTag() {
pageContext.removeAttribute(getId());
try {
_statement.close();
} catch (SQLException e) {
// it's not a fatal error if we can't close the statement
e.printStackTrace();
}
return EVAL_PAGE;
}
public void release() {
_connId=null;
_statement = null;
_query=null;
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -