Java SQL Execute execute(Connection conn, String sql, Object[] args)

Here you can find the source of execute(Connection conn, String sql, Object[] args)

Description

Executes the given sql string.

License

Open Source License

Parameter

Parameter Description
conn the database connection
sql the sql string to execute
args the sql argument objects

Exception

Parameter Description
SQLException when error occurs during execution.

Declaration

private static void execute(Connection conn, String sql, Object[] args)
        throws SQLException 

Method Source Code

//package com.java2s;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class Main {
    /**/*from   ww w  . j ava  2 s  .co  m*/
     * <p>
     * Executes the given sql string.
     * </p>
     * @param conn the database connection
     * @param sql the sql string to execute
     * @param args the sql argument objects
     * @throws SQLException when error occurs during execution.
     */
    private static void execute(Connection conn, String sql, Object[] args)
            throws SQLException {
        PreparedStatement stmt = null;

        try {
            stmt = conn.prepareStatement(sql);

            for (int i = 0; i < args.length;) {
                Object obj = args[i++];

                if (obj instanceof Long) {
                    stmt.setLong(i, ((Long) obj).longValue());
                } else if (obj instanceof Double) {
                    stmt.setDouble(i, ((Double) obj).doubleValue());
                } else if (obj instanceof Boolean) {
                    stmt.setBoolean(i, ((Boolean) obj).booleanValue());
                } else if (obj instanceof String) {
                    stmt.setString(i, (String) obj);
                }
            }

            stmt.executeUpdate();
        } finally {
            close(stmt);
        }
    }

    /**
     * <p>
     * Closes the sql statement.
     * </p>
     * @param statement the statement to be closed.
     */
    private static void close(PreparedStatement statement) {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                // ignore
            }
        }
    }

    /**
     * <p>
     * Closes the sql connection.
     * </p>
     * @param connection the connection to be closed.
     */
    private static void close(Connection connection) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                // ignore
            }
        }
    }
}

Related

  1. exec(Connection psql, String sql)
  2. execSql(String sql, Connection conn)
  3. execStatement(Connection con, String strStatement)
  4. execte(Connection conn, String sql)
  5. execute(Connection conn, String SQL)
  6. execute(Connection conn, String sql, Object[] params)
  7. execute(Connection conn, String string)
  8. execute(Connection connection, String sql)
  9. execute(Connection connection, String sql, boolean closeConn)