Example usage for java.sql ResultSetMetaData getColumnLabel

List of usage examples for java.sql ResultSetMetaData getColumnLabel

Introduction

In this page you can find the example usage for java.sql ResultSetMetaData getColumnLabel.

Prototype

String getColumnLabel(int column) throws SQLException;

Source Link

Document

Gets the designated column's suggested title for use in printouts and displays.

Usage

From source file:io.lightlink.spring.StreamingMapper.java

private void readMetadata(ResultSet resultSet) throws SQLException {
    ResultSetMetaData metaData = resultSet.getMetaData();
    colCount = metaData.getColumnCount();
    colNames = new String[colCount];
    for (int i = 0; i < colNames.length; i++) {
        colNames[i] = metaData.getColumnLabel(i + 1);
    }/*from   w ww .ja  v a 2s  .c  o  m*/
}

From source file:org.vruum.unstack.norm.CustomBasicRowProcessor.java

@Override
public Map<String, Object> toMap(ResultSet rs) throws SQLException {
    Map<String, Object> result = new CaseInsensitiveHashMap();
    ResultSetMetaData rsmd = rs.getMetaData();
    int cols = rsmd.getColumnCount();

    for (int i = 1; i <= cols; i++) {
        String columnName = rsmd.getColumnLabel(i);
        if (null == columnName || 0 == columnName.length()) {
            columnName = rsmd.getColumnName(i);
        }/*  w ww .ja va  2s.c  om*/
        result.put(columnName, rs.getObject(i));
    }

    return result;
}

From source file:br.com.manish.ahy.kernel.util.DAOUtil.java

public List<Map<String, Object>> executeSQLQuery(String sql) {
    List<Map<String, Object>> ret = new ArrayList<Map<String, Object>>();

    try {//ww w  .  j  av  a 2 s  . c  o  m
        log.info("executeSQLQuery: " + sql);

        rs = stmt.executeQuery(sql);

        while (rs.next()) {
            Map<String, Object> item = new HashMap<String, Object>();
            ResultSetMetaData rsmd = rs.getMetaData();
            for (int i = 1; i < rsmd.getColumnCount() + 1; i++) {
                item.put(rsmd.getColumnLabel(i), rs.getObject(i));
            }
            ret.add(item);
        }

    } catch (Exception e) {
        throw new OopsException(e, "Problem when executing sql command.");
    }

    return ret;
}

From source file:com.dangdang.ddframe.rdb.sharding.merger.component.other.WrapperResultSet.java

/**
 * ?./*from  w ww.  ja v a2s .co  m*/
 * 
 * @return 
 * @throws SQLException ??
 */
public Map<String, Integer> getColumnLabelIndexMap() throws SQLException {
    ResultSetMetaData resultSetMetaData = getDelegate().getMetaData();
    Map<String, Integer> result = new CaseInsensitiveMap<>(resultSetMetaData.getColumnCount());
    for (int i = 1; i <= resultSetMetaData.getColumnCount(); i++) {
        result.put(resultSetMetaData.getColumnLabel(i), i);
    }
    return result;
}

From source file:BadProfessor.java

public void checkData(String sql) throws Exception {
        ResultSet rs = st.executeQuery(sql);
        ResultSetMetaData metadata = rs.getMetaData();

        for (int i = 0; i < metadata.getColumnCount(); i++) {
            System.out.print("\t" + metadata.getColumnLabel(i + 1));
        }/*from   w ww.  java 2s . com*/
        System.out.println("\n----------------------------------");

        while (rs.next()) {
            for (int i = 0; i < metadata.getColumnCount(); i++) {
                Object value = rs.getObject(i + 1);
                if (value == null) {
                    System.out.print("\t       ");
                } else {
                    System.out.print("\t" + value.toString().trim());
                }
            }
            System.out.println("");
        }
    }

From source file:com.dangdang.ddframe.rdb.sharding.jdbc.adapter.AbstractResultSetAdapter.java

private Map<String, Integer> generateColumnLabelIndexMap() throws SQLException {
    ResultSetMetaData resultSetMetaData = resultSets.get(0).getMetaData();
    Map<String, Integer> result = new CaseInsensitiveMap<>(resultSetMetaData.getColumnCount());
    for (int i = 1; i <= resultSetMetaData.getColumnCount(); i++) {
        result.put(resultSetMetaData.getColumnLabel(i), i);
    }/*from   w w  w  . j a v  a  2 s  .c  om*/
    return result;
}

From source file:com.streamsets.pipeline.lib.jdbc.multithread.CDCJdbcRunnable.java

@Override
public void generateSchemaChanges(BatchContext batchContext) throws SQLException {
    Map<String, Integer> source = new HashMap<>();
    ResultSet rs = tableReadContext.getMoreResultSet();
    String schemaName = "";
    String tableName = "";
    String captureInstanceName = "";

    if (rs != null && rs.next()) {
        ResultSetMetaData data = rs.getMetaData();

        for (int i = 1; i <= data.getColumnCount(); i++) {
            String label = data.getColumnLabel(i);
            if (label.equals(MSQueryUtil.CDC_SOURCE_SCHEMA_NAME)) {
                schemaName = rs.getString(label);
            } else if (label.equals(MSQueryUtil.CDC_SOURCE_TABLE_NAME)) {
                tableName = rs.getString(label);
            } else if (label.equals(MSQueryUtil.CDC_CAPTURE_INSTANCE_NAME)) {
                captureInstanceName = rs.getString(label);
            } else {
                int type = data.getColumnType(i);
                source.put(label, type);
            }/* ww w .  jav a2s . c  o m*/
        }

        boolean schemaChanges = getDiff(captureInstanceName, source,
                tableRuntimeContext.getSourceTableContext().getColumnToType());

        if (schemaChanges) {
            JdbcEvents.SCHEMA_CHANGE.create(context, batchContext).with("source-table-schema-name", schemaName)
                    .with("source-table-name", tableName).with("capture-instance-name", captureInstanceName)
                    .createAndSend();
            context.processBatch(batchContext);
        }
    }
}

From source file:org.hyperic.hq.plugin.postgresql.ServerCollector.java

private void extartMetrics(String query, Connection conn) throws SQLException {
    Statement stmt = null;/* w  w w . j  a va  2  s.  c  o m*/
    ResultSet rs = null;

    try {
        log.debug("[extartMetrics] query:'" + query + "'");
        stmt = conn.createStatement();
        rs = stmt.executeQuery(query);
        ResultSetMetaData md = rs.getMetaData();
        while (rs.next()) {
            for (int c = 1; c <= md.getColumnCount(); c++) {
                setValue(md.getColumnLabel(c), rs.getString(c));
            }
        }
    } finally {
        DBUtil.closeJDBCObjects(log, null, stmt, rs);
    }
}

From source file:org.openmrs.module.querybrowser.api.impl.QueryBrowserServiceImpl.java

@Override
public QueryResults query(String query) {

    final Connection conn = sessionFactory.getCurrentSession().connection();

    final QueryResults qResults = new QueryResults();

    query = query.trim();/* w ww .j  a  v a 2 s.c  o  m*/
    boolean dataManipulation = false;

    final String sqlLower = query.toLowerCase();
    if (sqlLower.startsWith("insert") || sqlLower.startsWith("update") || sqlLower.startsWith("delete")
            || sqlLower.startsWith("alter") || sqlLower.startsWith("drop") || sqlLower.startsWith("create")
            || sqlLower.startsWith("rename")) {
        dataManipulation = true;
    }

    PreparedStatement ps = null;
    final List<List<Object>> results = new Vector<List<Object>>();
    final List<String> headers = new ArrayList<String>();

    try {
        ps = conn.prepareStatement(query);

        if (dataManipulation == true) {
            final Integer i = ps.executeUpdate();
            final List<Object> row = new Vector<Object>();
            row.add(i);
            results.add(row);
        } else {
            final ResultSet resultSet = ps.executeQuery();

            final ResultSetMetaData rmd = resultSet.getMetaData();
            final int columnCount = rmd.getColumnCount();

            for (int i = 1; i < columnCount; i++) {
                headers.add(rmd.getColumnLabel(i));
            }

            while (resultSet.next()) {
                final List<Object> rowObjects = new Vector<Object>();
                for (int x = 1; x <= columnCount; x++) {
                    rowObjects.add(resultSet.getObject(x));
                }
                results.add(rowObjects);
            }
        }
    } catch (final Exception e) {
        log.debug("Error while running sql: " + query, e);
        throw new DAOException("Error while running sql: " + query + " . Message: " + e.getMessage(), e);
    } finally {
        if (ps != null) {
            try {
                ps.close();
            } catch (final SQLException e) {
                log.error("Error generated while closing statement", e);
            }
        }
    }

    qResults.setHeaders(headers);
    qResults.setResults(results);
    return qResults;
}

From source file:ResultsDecoratorHTML.java

public void write(ResultSet rs) throws IOException, SQLException {

    ResultSetMetaData md = rs.getMetaData();
    int count = md.getColumnCount();
    println("<table border=1>");
    print("<tr>");
    for (int i = 1; i <= count; i++) {
        print("<th>");
        print(md.getColumnLabel(i));
    }/*  w w w . j  a v  a 2 s.c o  m*/
    println("</tr>");
    while (rs.next()) {
        print("<tr>");
        for (int i = 1; i <= count; i++) {
            print("<td>");
            print(rs.getString(i));
        }
        println("</tr>");
    }
    println("</table>");
}