Java SQL Execute executeCallable(Connection conn, String sql)

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

Description

execute Callable

License

Open Source License

Declaration

public static void executeCallable(Connection conn, String sql)
            throws Exception 

Method Source Code

//package com.java2s;
/*//from   w  ww .j  a v a  2  s  . c o m
 * JBoss, Home of Professional Open Source.
 * See the COPYRIGHT.txt file distributed with this work for information
 * regarding copyright ownership.  Some portions may be licensed
 * to Red Hat, Inc. under one or more contributor license agreements.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 */

import java.sql.*;

public class Main {
    public static void executeCallable(Connection conn, String sql)
            throws Exception {

        System.out.println("Query SQL: " + sql);

        CallableStatement cStmt = null;
        ResultSet rs = null;

        try {
            cStmt = conn.prepareCall(sql);
            boolean hadResults = cStmt.execute();
            while (hadResults) {
                rs = cStmt.getResultSet();
                int columns = rs.getMetaData().getColumnCount();
                for (int row = 1; rs.next(); row++) {
                    System.out.print(row + ": ");
                    for (int i = 0; i < columns; i++) {
                        if (i > 0) {
                            System.out.print(", ");
                        }
                        System.out.print(rs.getObject(i + 1));
                    }
                    System.out.println();
                }
                rs.close();
                hadResults = cStmt.getMoreResults();
            }
        } catch (Exception e) {
            throw e;
        } finally {
            close(rs, cStmt);
        }

        System.out.println();

    }

    public static void close(Connection conn) {

        if (null != conn) {
            try {
                conn.close();
                conn = null;
            } catch (SQLException e) {

            }
        }
    }

    public static void close(Statement stmt) {

        if (null != stmt) {
            try {
                stmt.close();
                stmt = null;
            } catch (SQLException e) {

            }
        }
    }

    public static void close(ResultSet rs, Statement stmt) {

        if (null != rs) {
            try {
                rs.close();
                rs = null;
            } catch (SQLException e) {
            }
        }

        if (null != stmt) {
            try {
                stmt.close();
                stmt = null;
            } catch (SQLException e) {
            }
        }
    }
}

Related

  1. execute(final String url, final String username, final String password, final String sql, final Consumer consumer)
  2. execute(List sql)
  3. execute(Statement statement, String sql)
  4. execute(String sql, Connection connection)
  5. executeCall(Connection connection, String command, String schemaName, String tableName, int paramLength)
  6. executeCreateDB(String className, String URL, String userName, String password, String dbName)
  7. executeDDL(final Connection connection, final String sql, final int... ignoreErrors)
  8. executeDDL(String ddl)
  9. executeImmediate(String sql)