FileDocCategorySizeDatePackage
UpdateTagSupport.javaAPI DocGlassfish v2 API8609Sat May 05 19:17:54 BST 2007org.apache.taglibs.standard.tag.common.sql

UpdateTagSupport

public abstract class UpdateTagSupport extends javax.servlet.jsp.tagext.BodyTagSupport implements javax.servlet.jsp.jstl.sql.SQLExecutionTag, javax.servlet.jsp.tagext.TryCatchFinally

Tag handler for <Update> in JSTL.

author
Hans Bergsten
author
Justyna Horwat

Fields Summary
private String
var
private int
scope
protected Object
rawDataSource
protected boolean
dataSourceSpecified
protected String
sql
private Connection
conn
private List
parameters
private boolean
isPartOfTransaction
Constructors Summary
public UpdateTagSupport()

	super();
	init();
    
Methods Summary
public voidaddSQLParameter(java.lang.Object o)
Called by nested parameter elements to add PreparedStatement parameter values.

	if (parameters == null) {
	    parameters = new ArrayList();
	}
	parameters.add(o);
    
public voiddoCatch(java.lang.Throwable t)
Just rethrows the Throwable.

	throw t;
    
public intdoEndTag()

Execute the SQL statement, set either through the sql attribute or as the body, and save the result as a variable named by the var attribute in the scope specified by the scope attribute, as an object that implements the Result interface.

The connection used to execute the statement comes either from the DataSource specified by the dataSource attribute, provided by a parent action element, or is retrieved from a JSP scope attribute named javax.servlet.jsp.jstl.sql.dataSource.

	/*
	 * Use the SQL statement specified by the sql attribute, if any,
	 * otherwise use the body as the statement.
	 */
	String sqlStatement = null;
	if (sql != null) {
	    sqlStatement = sql;
	}
	else if (bodyContent != null) {
	    sqlStatement = bodyContent.getString();
	}
	if (sqlStatement == null || sqlStatement.trim().length() == 0) {
	    throw new JspTagException(
                Resources.getMessage("SQL_NO_STATEMENT"));
	}

	int result = 0;
	try {
	    PreparedStatement ps = conn.prepareStatement(sqlStatement);
	    setParameters(ps, parameters);
	    result = ps.executeUpdate();
	}
	catch (Throwable e) {
	    throw new JspException(sqlStatement + ": " + e.getMessage(), e);
	}
	if (var != null)
            pageContext.setAttribute(var, Integer.valueOf(result), scope);
	return EVAL_PAGE;
    
public voiddoFinally()
Close the Connection, unless this action is used as part of a transaction.

	if (conn != null && !isPartOfTransaction) {
	    try {
		conn.close();
	    } catch (SQLException e) {
		// Not much we can do
	    }
	}

	parameters = null;
	conn = null;
    
public intdoStartTag()
Prepares for execution by setting the initial state, such as getting the Connection


	try {
	    conn = getConnection();
	} catch (SQLException e) {
	    throw new JspException(sql + ": " + e.getMessage(), e);
	}

	return EVAL_BODY_BUFFERED;
    
private java.sql.ConnectiongetConnection()

	// Fix: Add all other mechanisms
	Connection conn = null;
	isPartOfTransaction = false;

	TransactionTagSupport parent = (TransactionTagSupport) 
	    findAncestorWithClass(this, TransactionTagSupport.class);
	if (parent != null) {
            if (dataSourceSpecified) {
                throw new JspTagException(
                    Resources.getMessage("ERROR_NESTED_DATASOURCE"));
            }
	    conn = parent.getSharedConnection();
            isPartOfTransaction = true;
	} else {
	    if ((rawDataSource == null) && dataSourceSpecified) {
		throw new JspException(
		    Resources.getMessage("SQL_DATASOURCE_NULL"));
	    }
	    DataSource dataSource = DataSourceUtil.getDataSource(rawDataSource,
								 pageContext);
            try {
                conn = dataSource.getConnection();
            } catch (Exception ex) {
                throw new JspException(
                    Resources.getMessage("DATASOURCE_INVALID",
					 ex.toString()));
            }
	}

	return conn;
    
private voidinit()

	rawDataSource = null;
	sql = null;
	conn = null;
	parameters = null;
	isPartOfTransaction = dataSourceSpecified = false;
        scope = PageContext.PAGE_SCOPE;
	var = null;
    
private voidsetParameters(java.sql.PreparedStatement ps, java.util.List parameters)

	if (parameters != null) {
	    for (int i = 0; i < parameters.size(); i++) {
                /* The first parameter has index 1.  If a null
                 * is passed to setObject the parameter will be
                 * set to JDBC null so an explicit call to
                 * ps.setNull is not required.
                 */
                ps.setObject(i + 1, parameters.get(i));
	    }
	}
    
public voidsetScope(java.lang.String scopeName)
Setter method for the scope of the variable to hold the result.

        scope = Util.getScope(scopeName);
    
public voidsetVar(java.lang.String var)
Setter method for the name of the variable to hold the result.

	this.var = var;