public void doTag()

in jelly-tags/sql/src/main/java/org/apache/commons/jelly/tags/sql/UpdateTag.java [68:136]


    public void doTag(XMLOutput output) throws JellyTagException {
        try {
            conn = getConnection();
        }
        catch (SQLException e) {
            throw new JellyTagException(sql + ": " + e.getMessage(), e);
        }

        /*
         * 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 {
            sqlStatement = getBodyText();
        }
        if (sqlStatement == null || sqlStatement.trim().length() == 0) {
            throw new JellyTagException(Resources.getMessage("SQL_NO_STATEMENT"));
        }

        Statement statement = null;
        int result = 0;
        try {
            if ( hasParameters() ) {
                PreparedStatement ps = conn.prepareStatement(sqlStatement);
                statement = ps;
                setParameters(ps);
                result = ps.executeUpdate();
            }
            else {
                statement = conn.createStatement();
                result = statement.executeUpdate(sqlStatement);
            }
            if (var != null) {
                context.setVariable(var, new Integer(result));
            }

            // lets nullify before we close in case we get exceptions
            // while closing, we don't want to try to close again
            Statement tempStatement = statement;
            statement = null;
            tempStatement.close();
        }
        catch (SQLException e) {
            throw new JellyTagException(sqlStatement + ": " + e.getMessage(), e);
        }
        finally {
            if (statement != null) {
                try {
                    statement.close();
                }
                catch (SQLException e) {
                    log.error("Caught exception while closing statement: " + e, e);
                }
            }
            if (conn != null && !isPartOfTransaction) {
                try {
                    conn.close();
                }
                catch (SQLException e) {
                    log.error("Caught exception while closing connection: " + e, e);
                }
            }
            clearParameters();
        }
    }