Java SQL Execute execStatement(Connection con, String strStatement)

Here you can find the source of execStatement(Connection con, String strStatement)

Description


Purpose: execute statement, can be used to do update or insert
Assumptions: Note: this version does not take into account special characters.

License

Open Source License

Parameter

Parameter Description

Return

either the row count for INSERT, UPDATE or DELETE statements; or 0 for SQL statements that return nothing

Declaration

public static int execStatement(Connection con, String strStatement) throws SQLException 

Method Source Code

//package com.java2s;
/** Copyright (C) (MSA, Inc) All rights reserved.
 ** General Public License: http://www.opensource.org/licenses/gpl-license.php
 **///from  ww  w  .  jav  a  2s. c  o  m

import java.sql.*;

public class Main {
    /** <br><em>Purpose:</em> execute statement, can be used to do update or insert
     * <br><em>Assumptions:</em>  Note: this version does not take into account special characters.
     * You need a more sophisticated mechanism to take those into account.
     * @param                     con, Connection
     * @param                     strStatement, String the statement
     * @return                    either the row count for INSERT, UPDATE or DELETE statements; or 0 for SQL statements that return nothing
     * @exception                 SQLException
     **/
    public static int execStatement(Connection con, String strStatement) throws SQLException {
        ResultSet rs = null;
        PreparedStatement st = null;
        try {
            st = con.prepareStatement(strStatement);
            return st.executeUpdate();
        } finally {
            try {
                closeAll(rs, st, null);
            } catch (SQLException e) {
            }
        }
    }

    /** Convenience method for closing sql objects. Closing is tried on
     ** all objects, independent of errors/exceptions in previous closes.
     * @param rs ResultSet to be closed
     * @param st Statement (or subclass) to be closed
     * @param conn Connection to be closed
     * @exception SQLException when any problem occurs while closing objects
     */
    public static void closeAll(ResultSet rs, Statement st, Connection conn) throws SQLException {
        try {
            if (rs != null) {
                rs.close();
            }
        } finally {
            try {
                if (st != null) {
                    st.close();
                }
            } finally {
                if (conn != null) {
                    conn.close();
                }
            }
        }
    }
}

Related

  1. exec(Connection conn, String sql)
  2. exec(Connection psql, String sql)
  3. execSql(String sql, Connection conn)
  4. execte(Connection conn, String sql)
  5. execute(Connection conn, String SQL)
  6. execute(Connection conn, String sql, Object[] args)
  7. execute(Connection conn, String sql, Object[] params)