Example usage for java.sql ResultSetMetaData getColumnName

List of usage examples for java.sql ResultSetMetaData getColumnName

Introduction

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

Prototype

String getColumnName(int column) throws SQLException;

Source Link

Document

Get the designated column's name.

Usage

From source file:de.static_interface.reallifeplugin.database.AbstractTable.java

public boolean hasColumn(ResultSet rs, String columnName) throws SQLException {
    ResultSetMetaData rsmd = rs.getMetaData();
    int columns = rsmd.getColumnCount();
    for (int x = 1; x <= columns; x++) {
        if (columnName.equals(rsmd.getColumnName(x))) {
            return true;
        }//from w  w  w  . j ava  2 s  .c  o m
    }
    return false;
}

From source file:org.apache.kylin.jdbc.KylinConnectionTest.java

@Test
public void testJdbcClientCalcitePropsInUrl() throws Exception {
    String sql = "select 1 as val";

    // mock client
    when(client.executeQuery(anyString(), Mockito.<List<Object>>any(), Mockito.<Map<String, String>>any()))
            .thenReturn(getMockResult());
    Map<String, String> toggles = new HashMap<>();
    Properties info = new Properties();
    info.setProperty("caseSensitive", "false");
    info.setProperty("unquotedCasing", "UNCHANGED");
    try (KylinConnection conn = getConnectionWithMockClient("jdbc:kylin:test_url/test_db", info)) {
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            verify(client).executeQuery(eq(sql), Mockito.<List<Object>>any(),
                    argThat(new ArgumentMatcher<Map<String, String>>() {
                        @Override
                        public boolean matches(Map<String, String> argument) {
                            String propsStr = argument.get("JDBC_CLIENT_CALCITE_PROPS");
                            assertNotNull(propsStr);
                            Properties props = new Properties();
                            try {
                                props.load(new StringReader(propsStr));
                            } catch (IOException e) {
                                throw new RuntimeException(e);
                            }/*from w w w  .j  a va 2s .  c  o m*/
                            assertEquals("false", props.getProperty("caseSensitive"));
                            assertEquals("UNCHANGED", props.getProperty("unquotedCasing"));
                            return true;
                        }
                    }));

            assertTrue(resultSet.next());
            ResultSetMetaData metaData = resultSet.getMetaData();
            assertEquals("VAL", metaData.getColumnName(1));
            assertEquals(1, resultSet.getInt("VAL"));
        }
    }
}

From source file:org.apache.hadoop.sqoop.util.ResultSetPrinter.java

/**
 * Format the contents of the ResultSet into something that could be printed
 * neatly; the results are appended to the supplied StringBuilder.
 *//*from   ww w . j a  v a2 s . c  om*/
public final void printResultSet(OutputStream os, ResultSet results) throws IOException {
    try {
        StringBuilder sbNames = new StringBuilder();
        int cols = results.getMetaData().getColumnCount();

        int[] colWidths = new int[cols];
        ResultSetMetaData metadata = results.getMetaData();
        for (int i = 1; i < cols + 1; i++) {
            String colName = metadata.getColumnName(i);
            colWidths[i - 1] = Math.min(metadata.getColumnDisplaySize(i), MAX_COL_WIDTH);
            if (colName == null || colName.equals("")) {
                colName = metadata.getColumnLabel(i) + "*";
            }
            printPadded(sbNames, colName, colWidths[i - 1]);
            sbNames.append(COL_SEPARATOR);
        }
        sbNames.append('\n');

        StringBuilder sbPad = new StringBuilder();
        for (int i = 0; i < cols; i++) {
            for (int j = 0; j < COL_SEPARATOR.length() + colWidths[i]; j++) {
                sbPad.append('-');
            }
        }
        sbPad.append('\n');

        sendToStream(sbPad, os);
        sendToStream(sbNames, os);
        sendToStream(sbPad, os);

        while (results.next()) {
            StringBuilder sb = new StringBuilder();
            for (int i = 1; i < cols + 1; i++) {
                printPadded(sb, results.getString(i), colWidths[i - 1]);
                sb.append(COL_SEPARATOR);
            }
            sb.append('\n');
            sendToStream(sb, os);
        }

        sendToStream(sbPad, os);
    } catch (SQLException sqlException) {
        LOG.error("Error reading from database: " + sqlException.toString());
    }
}

From source file:org.apache.sqoop.manager.sqlserver.MSSQLTestUtils.java

public void metadataStuff(String table) {
    Connection dbcon = this.getConnection();
    String sql = "select top 1 * from " + table;

    Statement st;/*from   w  w w  .ja  va 2 s  .  c  om*/
    try {

        st = dbcon.createStatement();
        ResultSet rs = st.executeQuery(sql);
        ResultSetMetaData rsmd = rs.getMetaData();

        for (int i = 1; i <= rsmd.getColumnCount(); i++) {
            System.out.println(rsmd.getColumnName(i) + "\t" + rsmd.getColumnClassName(i) + "\t"
                    + rsmd.getColumnType(i) + "\t" + rsmd.getColumnTypeName(i) + "\n");
        }

    } catch (SQLException e) {
        LOG.error(StringUtils.stringifyException(e));
    }

}

From source file:com.cloudera.sqoop.util.ResultSetPrinter.java

/**
 * Format the contents of the ResultSet into something that could be printed
 * neatly; the results are appended to the supplied StringBuilder.
 *//*w  w  w  . j a va 2 s . c o m*/
public final void printResultSet(PrintWriter pw, ResultSet results) throws IOException {
    try {
        StringBuilder sbNames = new StringBuilder();
        int cols = results.getMetaData().getColumnCount();

        int[] colWidths = new int[cols];
        ResultSetMetaData metadata = results.getMetaData();
        sbNames.append(LEFT_BORDER);
        for (int i = 1; i < cols + 1; i++) {
            String colName = metadata.getColumnName(i);
            colWidths[i - 1] = Math.min(metadata.getColumnDisplaySize(i), MAX_COL_WIDTH);
            if (colName == null || colName.equals("")) {
                colName = metadata.getColumnLabel(i) + "*";
            }
            printPadded(sbNames, colName, colWidths[i - 1]);
            sbNames.append(COL_SEPARATOR);
        }
        sbNames.append('\n');

        StringBuilder sbPad = new StringBuilder();
        for (int i = 0; i < cols; i++) {
            for (int j = 0; j < COL_SEPARATOR.length() + colWidths[i]; j++) {
                sbPad.append('-');
            }
        }
        sbPad.append('-');
        sbPad.append('\n');

        pw.print(sbPad.toString());
        pw.print(sbNames.toString());
        pw.print(sbPad.toString());

        while (results.next()) {
            StringBuilder sb = new StringBuilder();
            sb.append(LEFT_BORDER);
            for (int i = 1; i < cols + 1; i++) {
                printPadded(sb, results.getString(i), colWidths[i - 1]);
                sb.append(COL_SEPARATOR);
            }
            sb.append('\n');
            pw.print(sb.toString());
        }

        pw.print(sbPad.toString());
    } catch (SQLException sqlException) {
        LOG.error("Error reading from database: " + StringUtils.stringifyException(sqlException));
    }
}

From source file:com.job.portal.utils.AbstractDAO.java

protected JSONObject getJSONObject(ResultSet rs) throws SQLException, JSONException {
    JSONObject obj = null;//from   w  w w  . j ava 2s. c o  m
    ResultSetMetaData rsmd = rs.getMetaData();
    int size = rsmd.getColumnCount();
    if (rs.next()) {
        obj = new JSONObject();
        for (int i = 1; i <= size; i++) {
            obj.put(rsmd.getColumnName(i), rs.getString(i));
        }
    }
    return obj;
}

From source file:com.trackplus.ddl.GenericStringValueConverter.java

@Override
public String getStringValue(ResultSetMetaData rsmd, int idx, ResultSet rs, String tableName)
        throws SQLException, DDLException {
    String value = null;//from  w  ww . ja v  a  2 s  .  com
    int type = rsmd.getColumnType(idx);
    String columnName = rsmd.getColumnName(idx);
    if (tableName.equalsIgnoreCase("TSITE") && columnName.equalsIgnoreCase("EXPDATE")) {
        Date d = rs.getDate(idx);
        if (d != null) {
            value = "'" + d + "'";
        }
    } else {
        value = extractColumnValue(rs, idx, type);
    }
    return value;
}

From source file:com.job.portal.utils.AbstractDAO.java

protected JSONArray getJSONArray(ResultSet rs) throws SQLException, JSONException {
    JSONArray arr = new JSONArray();
    JSONObject obj = null;/*from  w w  w .  j a  va  2  s.c  o  m*/
    ResultSetMetaData rsmd = rs.getMetaData();
    int size = rsmd.getColumnCount();
    while (rs.next()) {
        obj = new JSONObject();
        for (int i = 1; i <= size; i++) {
            obj.put(rsmd.getColumnName(i), rs.getString(i));
        }
        arr.put(obj);
    }
    return arr;
}

From source file:MainClass.java

public void setQuery(String q) {
    cache = new Vector();
    try {//from w w  w . j av a  2  s.com
        ResultSet rs = statement.executeQuery(q);
        ResultSetMetaData meta = rs.getMetaData();
        colCount = meta.getColumnCount();

        headers = new String[colCount];
        for (int h = 1; h <= colCount; h++) {
            headers[h - 1] = meta.getColumnName(h);
        }

        while (rs.next()) {
            String[] record = new String[colCount];
            for (int i = 0; i < colCount; i++) {
                record[i] = rs.getString(i + 1);
            }
            cache.addElement(record);
        }
        fireTableChanged(null);
    } catch (Exception e) {
        cache = new Vector();
        e.printStackTrace();
    }
}

From source file:com.teradata.benchto.driver.execution.QueryExecutionDriver.java

private void logRow(int rowNumber, ResultSet resultSet) throws SQLException {
    ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
    StringJoiner joiner = new StringJoiner("; ", "[", "]");
    for (int i = 1; i <= resultSetMetaData.getColumnCount(); ++i) {
        joiner.add(resultSetMetaData.getColumnName(i) + ": " + resultSet.getObject(i));
    }//from w w w  .j av  a 2  s. co  m

    LOG.info("Row: " + rowNumber + ", column values: " + joiner.toString());
}