Java SQL Execute exec(Connection conn, String sql)

Here you can find the source of exec(Connection conn, String sql)

Description

exec

License

MIT License

Declaration

public static void exec(Connection conn, String sql) throws SQLException 

Method Source Code

//package com.java2s;
/**/*from  w  ww  .  j  a v  a  2s .  c  o  m*/
 * Copyright (c) 2004-2011 Wang Jinbao(Julian Wong), http://www.ralasafe.com
 * Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
 */

import java.sql.Connection;

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

public class Main {
    public static void exec(Connection conn, String sql) throws SQLException {
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.execute();
        } finally {
            close(pstmt);
        }
    }

    public static void close(Connection conn) {
        if (conn != null) {
            try {
                if (!conn.isClosed()) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                conn = null;
            }
        }
    }

    public static void close(Statement stmt) {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                stmt = null;
            }
        }
    }

    public static void close(ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                rs = null;
            }
        }
    }

    public static void close(Statement stmt, Connection conn) {
        close(stmt);
        close(conn);
    }

    public static void close(ResultSet rs, Statement stmt, Connection conn) {
        close(rs);
        close(stmt);
        close(conn);
    }
}

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)