Java SQL Update executeUpdate(final Connection conn, final String sqlStatmeent)

Here you can find the source of executeUpdate(final Connection conn, final String sqlStatmeent)

Description

Executes a jdbc update statement and disposes of the resources

License

Open Source License

Parameter

Parameter Description
conn Connection
sqlStatmeent Statement to execute

Exception

Parameter Description
Throwable an exception

Declaration

public static final void executeUpdate(final Connection conn, final String sqlStatmeent) throws Throwable 

Method Source Code


//package com.java2s;
/* **********************************************************************
/*
 * NOTE: This copyright does *not* cover user programs that use Hyperic
 * program services by normal system calls through the application
 * program interfaces provided as part of the Hyperic Plug-in Development
 * Kit or the Hyperic Client Development Kit - this is merely considered
 * normal use of the program, and does *not* fall under the heading of
 * "derived work"./*from www.  j av  a 2  s.c  o m*/
 *
 * Copyright (C) [2004-2012], VMware, Inc.
 * This file is part of Hyperic.
 *
 * Hyperic is free software; you can redistribute it and/or modify
 * it under the terms version 2 of the GNU General Public License as
 * published by the Free Software Foundation. This program is distributed
 * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA.
 */

import java.io.Closeable;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.sql.Connection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Main {
    public static final int NOOP_INSTRUCTION_FLAG = 0;

    /**
     * Executes a jdbc update statement and disposes of the resources 
     * @param conn Connection 
     * @param sqlStatmeent Statement to execute 
     * @throws Throwable
     */
    public static final void executeUpdate(final Connection conn, final String sqlStatmeent) throws Throwable {
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sqlStatmeent);
            ps.executeUpdate();
        } finally {
            close(new Object[] { ps });
        } //EO catch block 
    }

    /**
     * Closes all formal argument objects with no special operations.<br/> 
     * Supported types:<br/>  
     * - {@link Connection}<br/>
     * - {@link PreparedStatement}<br/>
     * - {@link OutputStream}<br/>
     * - {@link InputStream}<br/>
     * <p> 
     * <b>Note:</b> Exceptions are silenced.
     * </p>
     * @param closeables list of closeables
     */
    public static final void close(final Object... closeables) {
        close(NOOP_INSTRUCTION_FLAG, closeables);
    }

    /**
     * Closes all formal argument objects with no special operations.<br/> 
     * Supported types:<br/>  
     * - {@link Connection}<br/>
     *   - Special Instructions:<br/> 
     *     - {@value #COMMIT_INSTRUCTION_FLAG}<br/>
     *     - {@value #ROLLBACK_INSTRUCTION_FLAG}<br/>
     * - {@link PreparedStatement}<br/>
     * - {@link OutputStream}<br/>
     * - {@link InputStream}<br/>
     * <p> 
     * <b>Note:</b> Exceptions are silenced.
     * </p>
     * @param closeables list of closeables
     */
    public static final void close(final int specialInstructionsMask, final Object... closeables) {
        for (Object closeable : closeables) {
            if (closeables == null)
                continue;
            try {
                if ((closeable instanceof Connection)) {
                    Connection conn = (Connection) closeable;
                    try {
                        if ((specialInstructionsMask & 0x4) == 4)
                            conn.commit();
                        else if ((specialInstructionsMask & 0x8) == 8)
                            conn.rollback();
                    } catch (Throwable t2) {
                        printStackTrace(t2);
                    } //EO inner catch block 

                    ((Connection) closeable).close();
                } else if ((closeable instanceof PreparedStatement)) {
                    ((PreparedStatement) closeable).close();
                } else if ((closeable instanceof ResultSet)) {
                    ((ResultSet) closeable).close();
                } else if ((closeable instanceof Writer)) {
                    final Writer writer = (Writer) closeable;
                    writer.flush();
                    writer.close();
                } else if ((closeable instanceof Reader)) {
                    ((Reader) closeable).close();
                } else if ((closeable instanceof OutputStream)) {
                    final OutputStream os = (OutputStream) closeable;
                    os.flush();
                    os.close();
                } else if ((closeable instanceof InputStream)) {
                    final InputStream is = (InputStream) closeable;
                    is.close();
                } //EO else if inputstream
                else if ((closeable instanceof Closeable)) {
                    ((Closeable) closeable).close();
                } //EO else if Closeable
            } catch (Throwable t) {
                printStackTrace(t);
            } //EO catch block 
        } //EO while there are more closeables 
    }

    /**
     * @param exception to print 
     */
    public static final void printStackTrace(final Throwable exception) {
        printStackTrace(exception, null);
    }

    /**
     * Prints the exception to the {@link System#err} stream with an optional prefixMessage<br/> 
     * <b>Note:</b> If {@link SQLException}, prints all its linked exceptions as well.  
     * @param exception to print 
     * @param prefixMessage optional message to output to {@link System#err} stream  
     */
    public static final void printStackTrace(final Throwable exception, final String prefixMessage) {
        if (prefixMessage != null)
            System.err.println(prefixMessage);
        if ((exception instanceof SQLException)) {
            SQLException sqle = (SQLException) exception;
            Throwable t = null;
            do {
                t = sqle;
                t.printStackTrace();
            } while (((sqle = sqle.getNextException()) != null) && (sqle != t));
        } else {
            exception.printStackTrace();
        } //EO if instanceof SQLException 
    }
}

Related

  1. executeUpdate(Connection conn, String sql, Object... parameters)
  2. executeUpdate(Connection connection, String query)
  3. executeUpdate(Connection connection, String statement)
  4. executeUpdate(Connection dbConnection, String query)
  5. executeUpdate(final Connection conn, final String query)
  6. executeUpdate(List paramList, Connection conn, PreparedStatement pstmt)
  7. executeUpdate(PreparedStatement preparedStatement, List paramList)
  8. executeUpdate(PreparedStatement ps)
  9. executeUpdate(String parameterizedSQL, List values, Connection conn)

    HOME | Copyright © www.java2s.com 2016