Java SQL Execute execute(final String url, final String username, final String password, final String sql, final Consumer consumer)

Here you can find the source of execute(final String url, final String username, final String password, final String sql, final Consumer consumer)

Description

execute

License

Apache License

Parameter

Parameter Description
conf a parameter
sql a parameter
consumer a parameter

Exception

Parameter Description
SQLException an exception

Declaration

public static void execute(final String url, final String username, final String password, final String sql,
        final Consumer<ResultSet> consumer) throws SQLException 

Method Source Code


//package com.java2s;
/* $Id: d9f0c935a858f9b9afe1ecc368d00381180da56b $
 * //from   w  w w.j  av a 2 s .co  m
 * @license
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy
 * of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.function.Consumer;

public class Main {
    /**
     * @param conf
     * @param sql
     * @param consumer
     * @throws SQLException
     */
    public static void execute(final String url, final String username, final String password, final String sql,
            final Consumer<ResultSet> consumer) throws SQLException {
        // FIXME use connection pool with time-outs?
        try (final Connection conn = DriverManager.getConnection(url, username, password);
                final Statement stmt = conn.createStatement();
                final ResultSet rs = stmt.executeQuery(sql);) {
            consumer.accept(rs);
        }
    }
}

Related

  1. execute(Connection conn, String sql, Object[] args)
  2. execute(Connection conn, String sql, Object[] params)
  3. execute(Connection conn, String string)
  4. execute(Connection connection, String sql)
  5. execute(Connection connection, String sql, boolean closeConn)
  6. execute(List sql)
  7. execute(Statement statement, String sql)
  8. execute(String sql, Connection connection)
  9. executeCall(Connection connection, String command, String schemaName, String tableName, int paramLength)