Search in sources :

Example 1 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project groovy by apache.

the class GroovyResultSetExtension method toString.

public String toString() {
    try {
        StringBuilder sb = new StringBuilder("[");
        ResultSetMetaData metaData = resultSet.getMetaData();
        int count = metaData.getColumnCount();
        for (int i = 1; i <= count; i++) {
            sb.append(metaData.getColumnName(i));
            sb.append(":");
            Object object = resultSet.getObject(i);
            if (object != null) {
                sb.append(object.toString());
            } else {
                sb.append("[null]");
            }
            if (i < count) {
                sb.append(", ");
            }
        }
        sb.append("]");
        return sb.toString();
    } catch (SQLException e) {
        //            System.err.println("e.getMessage() = " + e.getMessage());
        return super.toString();
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) SQLException(java.sql.SQLException)

Example 2 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project hive by apache.

the class JdbcRecordIterator method next.

@Override
public Map<String, String> next() {
    try {
        ResultSetMetaData metadata = rs.getMetaData();
        int numColumns = metadata.getColumnCount();
        Map<String, String> record = new HashMap<String, String>(numColumns);
        for (int i = 0; i < numColumns; i++) {
            String key = metadata.getColumnName(i + 1);
            String value = rs.getString(i + 1);
            if (value == null) {
                value = NullWritable.get().toString();
            }
            record.put(key, value);
        }
        return record;
    } catch (Exception e) {
        LOGGER.warn("next() threw exception", e);
        return null;
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) HashMap(java.util.HashMap)

Example 3 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project hive by apache.

the class TxnDbUtil method queryToString.

public static String queryToString(String query, boolean includeHeader) throws Exception {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    StringBuilder sb = new StringBuilder();
    try {
        conn = getConnection();
        stmt = conn.createStatement();
        rs = stmt.executeQuery(query);
        ResultSetMetaData rsmd = rs.getMetaData();
        if (includeHeader) {
            for (int colPos = 1; colPos <= rsmd.getColumnCount(); colPos++) {
                sb.append(rsmd.getColumnName(colPos)).append("   ");
            }
            sb.append('\n');
        }
        while (rs.next()) {
            for (int colPos = 1; colPos <= rsmd.getColumnCount(); colPos++) {
                sb.append(rs.getObject(colPos)).append("   ");
            }
            sb.append('\n');
        }
    } finally {
        closeResources(conn, stmt, rs);
    }
    return sb.toString();
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet)

Example 4 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project hive by apache.

the class cbo_rp_TestJdbcDriver2 method testMetaDataGetColumns.

@Test
public void testMetaDataGetColumns() throws SQLException {
    Map<String[], Integer> tests = new HashMap<String[], Integer>();
    tests.put(new String[] { "testhivejdbcdriver\\_table", null }, 2);
    tests.put(new String[] { "testhivejdbc%", null }, 7);
    tests.put(new String[] { "testhiveJDBC%", null }, 7);
    tests.put(new String[] { "%jdbcdriver\\_table", null }, 2);
    tests.put(new String[] { "%jdbcdriver\\_table%", "under\\_col" }, 1);
    //    tests.put(new String[]{"%jdbcdriver\\_table%", "under\\_COL"}, 1);
    tests.put(new String[] { "%jdbcdriver\\_table%", "under\\_co_" }, 1);
    tests.put(new String[] { "%jdbcdriver\\_table%", "under_col" }, 1);
    tests.put(new String[] { "%jdbcdriver\\_table%", "und%" }, 1);
    tests.put(new String[] { "%jdbcdriver\\_table%", "%" }, 2);
    tests.put(new String[] { "%jdbcdriver\\_table%", "_%" }, 2);
    for (String[] checkPattern : tests.keySet()) {
        ResultSet rs = con.getMetaData().getColumns(null, null, checkPattern[0], checkPattern[1]);
        // validate the metadata for the getColumns result set
        ResultSetMetaData rsmd = rs.getMetaData();
        assertEquals("TABLE_CAT", rsmd.getColumnName(1));
        int cnt = 0;
        while (rs.next()) {
            String columnname = rs.getString("COLUMN_NAME");
            int ordinalPos = rs.getInt("ORDINAL_POSITION");
            switch(cnt) {
                case 0:
                    assertEquals("Wrong column name found", "under_col", columnname);
                    assertEquals("Wrong ordinal position found", ordinalPos, 1);
                    break;
                case 1:
                    assertEquals("Wrong column name found", "value", columnname);
                    assertEquals("Wrong ordinal position found", ordinalPos, 2);
                    break;
                default:
                    break;
            }
            cnt++;
        }
        rs.close();
        assertEquals("Found less columns then we test for.", tests.get(checkPattern).intValue(), cnt);
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) HashMap(java.util.HashMap) ResultSet(java.sql.ResultSet) Test(org.junit.Test)

Example 5 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project hive by apache.

the class cbo_rp_TestJdbcDriver2 method testImportedKeys.

/**
   * test getImportedKeys()
   * @throws SQLException
   */
@Test
public void testImportedKeys() throws SQLException {
    DatabaseMetaData dbmd = con.getMetaData();
    assertNotNull(dbmd);
    // currently getImportedKeys always returns an empty resultset for Hive
    ResultSet res = dbmd.getImportedKeys(null, null, null);
    ResultSetMetaData md = res.getMetaData();
    assertEquals(md.getColumnCount(), 14);
    assertFalse(res.next());
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) ResultSet(java.sql.ResultSet) DatabaseMetaData(java.sql.DatabaseMetaData) Test(org.junit.Test)

Aggregations

ResultSetMetaData (java.sql.ResultSetMetaData)414 ResultSet (java.sql.ResultSet)262 Statement (java.sql.Statement)118 SQLException (java.sql.SQLException)111 Test (org.junit.Test)109 PreparedStatement (java.sql.PreparedStatement)95 Connection (java.sql.Connection)64 ArrayList (java.util.ArrayList)61 DatabaseMetaData (java.sql.DatabaseMetaData)40 HashMap (java.util.HashMap)40 Map (java.util.Map)28 List (java.util.List)17 LinkedHashMap (java.util.LinkedHashMap)15 IOException (java.io.IOException)14 BigDecimal (java.math.BigDecimal)12 HashSet (java.util.HashSet)12 ParameterMetaData (java.sql.ParameterMetaData)10 Properties (java.util.Properties)10 KnownFailure (dalvik.annotation.KnownFailure)9 Test (org.testng.annotations.Test)9