Java JDBC Reds Connection getRow(String username, String password, String url, String driver, String query, String[] params)

Here you can find the source of getRow(String username, String password, String url, String driver, String query, String[] params)

Description

get Row

License

Open Source License

Declaration

public static Map[] getRow(String username, String password, String url, String driver, String query,
            String[] params) throws Exception 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * xFramium// w w  w .  ja  v a  2 s.  com
 *
 * Copyright 2016 by Moreland Labs, Ltd. (http://www.morelandlabs.com)
 *
 * Some open source application is free software: you can redistribute 
 * it and/or modify it under the terms of the GNU General Public 
 * License as published by the Free Software Foundation, either 
 * version 3 of the License, or (at your option) any later version.
 *  
 * Some open source application 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 General Public License for more details.
 *  
 * You should have received a copy of the GNU General Public License
 * along with xFramium.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @license GPL-3.0+ <http://spdx.org/licenses/GPL-3.0+>
 *******************************************************************************/

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

public class Main {
    private static final Map[] EMPTY_MAP_ARRAY = new Map[0];

    public static Map[] getRow(String username, String password, String url, String driver, String query,
            String[] params) throws Exception {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            conn = getConnection(username, password, url, driver);

            pstmt = conn.prepareStatement(query);

            if (params != null) {
                int offset = 1;
                for (String param : params) {
                    pstmt.setString(offset++, param);
                }
            }

            rs = pstmt.executeQuery();

            return consume2(rs);
        } finally {
            try {
                rs.close();
            } catch (Throwable e) {
            }

            try {
                pstmt.close();
            } catch (Throwable e) {
            }

            try {
                conn.close();
            } catch (Throwable e) {
            }
        }
    }

    private static Connection getConnection(String username, String password, String url, String driver)
            throws Exception {
        Connection conn = null;

        Class.forName(driver);

        conn = DriverManager.getConnection(url, username, password);

        return conn;
    }

    private static Map[] consume2(ResultSet rs) throws Exception {
        ResultSetMetaData rsmd = rs.getMetaData();
        ArrayList results = new ArrayList();
        int colCount = rsmd.getColumnCount();

        while (rs.next()) {
            Map row = new HashMap();

            for (int i = 1; i <= colCount; ++i) {
                Object val = rs.getObject(i);

                row.put(rsmd.getColumnName(i), val);
                row.put(i, val);
            }

            results.add(row);
        }

        return toOutArray2(results);
    }

    private static Map[] toOutArray2(List listOfMaps) {
        return (Map[]) listOfMaps.toArray(EMPTY_MAP_ARRAY);
    }
}

Related

  1. createRedshiftTable(String redshiftURL, Properties loginProperties, String tableName, List fields)
  2. checkConnection()
  3. checkConnection()
  4. getUserFace(String sUserName)
  5. getUserFromHost(String host)
  6. query(String sql, Object... args)
  7. queryForList(String sql, Object... args)