com.kumarvv.setl.utils.SqlRunner.java Source code

Java tutorial

Introduction

Here is the source code for com.kumarvv.setl.utils.SqlRunner.java

Source

/**
 * Copyright (c) 2016 Vijay Vijayaram
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
 * and associated documentation files (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
 * THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package com.kumarvv.setl.utils;

import com.kumarvv.setl.model.DS;
import org.apache.commons.lang3.StringUtils;

import javax.sql.rowset.JdbcRowSet;
import java.sql.ResultSetMetaData;
import java.util.HashMap;
import java.util.Map;

public class SqlRunner {

    RowSetUtil rowSetUtil;

    private SqlRunner() {
        rowSetUtil = RowSetUtil.getInstance();
    }

    public static SqlRunner getInstance() {
        return new SqlRunner();
    }

    /**
     * retrieves single value from datasource
     *
     * @param sql
     * @param ds
     * @return
     */
    public Object getSingleValue(String sql, DS ds) {
        if (StringUtils.isEmpty(sql)) {
            return null;
        }
        try (JdbcRowSet jrs = rowSetUtil.getRowSet(ds)) {
            jrs.setCommand(sql);
            jrs.execute();
            if (jrs.next()) {
                return jrs.getObject(1);
            }
        } catch (Exception e) {
            return null;
        }
        return null;
    }

    /**
     * retrieves single row Map from datasource
     *
     * @param sql
     * @param ds
     * @return
     */
    public Map<String, Object> getSingleRowMap(String sql, DS ds) {
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isEmpty(sql)) {
            return map;
        }

        try (JdbcRowSet jrs = rowSetUtil.getRowSet(ds)) {
            jrs.setCommand(sql);
            jrs.execute();
            if (!jrs.next()) {
                return map;
            }
            ResultSetMetaData meta = jrs.getMetaData();
            for (int i = 1; i <= meta.getColumnCount(); i++) {
                map.put(meta.getColumnName(i), jrs.getObject(i));
            }
        } catch (Exception e) {
        }
        return map;
    }
}