List of usage examples for java.sql ResultSetMetaData getColumnName
String getColumnName(int column) throws SQLException;
From source file:DatabaseTest.java
public void setQuery(String q) { cache = new Vector(); try {/* ww w . j ava2s . co m*/ // Execute the query and store the result set and its metadata ResultSet rs = statement.executeQuery(q); ResultSetMetaData meta = rs.getMetaData(); colCount = meta.getColumnCount(); // Now we must rebuild the headers array with the new column names headers = new String[colCount]; for (int h = 1; h <= colCount; h++) { headers[h - 1] = meta.getColumnName(h); } // and file the cache with the records from our query. This would // not be // practical if we were expecting a few million records in response // to our // query, but we aren't, so we can do this. 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); // notify everyone that we have a new table. } catch (Exception e) { cache = new Vector(); // blank it out and keep going. e.printStackTrace(); } }
From source file:com.kumarvv.setl.utils.RowSetUtil.java
/** * get meta columns list with columnId//w ww .j a va 2 s. c o m * * @param meta * @return */ public Map<String, Integer> getMetaColumns(ResultSetMetaData meta) { final Map<String, Integer> metaColumns = new HashMap<>(); if (meta == null) { return metaColumns; } try { int colCount = meta.getColumnCount(); for (int c = 1; c <= colCount; c++) { metaColumns.put(meta.getColumnName(c), meta.getColumnType(c)); } } catch (SQLException sqle) { Logger.error("error getting metaColumns:", sqle.getMessage()); Logger.trace(sqle); } return metaColumns; }
From source file:com.openddal.test.BaseTestCase.java
public static void printResultSet(ResultSet rs) { try {/*from w w w .j a va 2 s.co m*/ if (rs != null) { ResultSetMetaData md = rs.getMetaData(); int cols = md.getColumnCount(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < cols; i++) { sb.append(md.getColumnName(i + 1) + " "); } sb.append('\n'); for (int i = 0; i < cols; i++) { sb.append("-------------------"); } sb.append('\n'); while (rs.next()) { for (int i = 0; i < cols; i++) { sb.append(rs.getString(i + 1) + " "); } } sb.append("\n"); System.out.println(sb.toString()); } } catch (Exception e) { e.printStackTrace(); } }
From source file:de.ufinke.cubaja.sql.ObjectFactoryGenerator.java
private void createSearchMap(ResultSetMetaData metaData) throws SQLException { int size = metaData.getColumnCount(); searchMap = new HashMap<String, SearchEntry>(); for (int i = 1; i <= size; i++) { String name = metaData.getColumnName(i).toLowerCase(); SearchEntry entry = new SearchEntry(name, i, metaData.getColumnType(i)); searchMap.put(Util.createMethodName(name, "set"), entry); }//from ww w .jav a 2 s .com if (searchMap.size() == 1) { singleColumnSqlType = metaData.getColumnType(1); } }
From source file:org.jtalks.poulpe.util.databasebackup.persistence.DbTableData.java
/** * Constructs a new ColumnMetaData objects from given ResultSetMetaData with provided column default values map and * the object's index.// w w w .j a va 2 s . c o m * * @param rsmd * A ResultSetMetaData which contains meta information about all columns for the table. * @param columnDefaultValues * A map of possibly defined values by default for columns. * @param i * Index of column which should be constructed. * @return A constructed ColumnMetaData object. * @throws SQLException * Is thrown in case any errors during work with database occur. */ private ColumnMetaData getColumnMetaData(ResultSetMetaData rsmd, Map<String, String> columnDefaultValues, int i) throws SQLException { SqlTypes columnType = SqlTypes.getSqlTypeByJdbcSqlType(rsmd.getColumnType(i)); ColumnMetaData column = ColumnMetaData.getInstance(rsmd.getColumnName(i), columnType) .setNullable(rsmd.isNullable(i) == ResultSetMetaData.columnNullable) .setAutoincrement(rsmd.isAutoIncrement(i)); if (columnDefaultValues.containsKey(rsmd.getColumnName(i))) { column.setDefaultValue(columnDefaultValues.get(rsmd.getColumnName(i))); } if (columnType.isHasSize()) { column.setSize(rsmd.getColumnDisplaySize(i)); } return column; }
From source file:com.github.tosdan.utils.sql.BasicRowProcessorMod.java
/** * Convert a <code>ResultSet</code> row into a <code>Map</code>. This * implementation returns a <code>Map</code> with case insensitive column * names as keys. Calls to <code>map.get("COL")</code> and * <code>map.get("col")</code> return the same value. * @see org.apache.commons.dbutils.RowProcessor#toMap(java.sql.ResultSet) * @param rs ResultSet that supplies the map data * @throws SQLException if a database access error occurs * @return the newly created Map//from w ww. j a va 2 s. c o m */ @Override public Map<String, Object> toMap(ResultSet rs) throws SQLException { Map<String, Object> result = new LinkedHashMap<String, Object>(); ResultSetMetaData rsmd = rs.getMetaData(); int cols = rsmd.getColumnCount(); for (int i = 1; i <= cols; i++) { Object obj = rs.getObject(i); result.put(rsmd.getColumnName(i), obj); } return result; }
From source file:kr.co.bitnine.octopus.testutils.MemoryDatabaseTest.java
private void verifyTableEquals(JSONObject expectedTable, ResultSet actualRows) throws Exception { ResultSetMetaData actualRowsMetaData = actualRows.getMetaData(); JSONArray expectedSchema = (JSONArray) expectedTable.get("table-schema"); assertEquals(expectedSchema.size(), actualRowsMetaData.getColumnCount()); for (int i = 0; i < expectedSchema.size(); i++) assertEquals(expectedSchema.get(i), actualRowsMetaData.getColumnName(i + 1)); for (Object rowObj : (JSONArray) expectedTable.get("table-rows")) { JSONArray row = (JSONArray) rowObj; actualRows.next();/*from www .j a v a 2 s. c om*/ for (int i = 0; i < row.size(); i++) { Object expected = row.get(i); Object actual; int sqlType = actualRowsMetaData.getColumnType(i + 1); switch (sqlType) { case Types.INTEGER: if (expected instanceof Boolean) expected = (long) ((Boolean) expected ? 1 : 0); actual = actualRows.getLong(i + 1); break; case Types.NULL: case Types.FLOAT: case Types.VARCHAR: actual = actualRows.getObject(i + 1); break; default: throw new RuntimeException("java.sql.Types " + sqlType + " is not supported"); } assertEquals(expected, actual); } } assertFalse(actualRows.next()); }
From source file:org.apache.tika.parser.jdbc.JDBCTableReader.java
public List<String> getHeaders() throws IOException { List<String> headers = new LinkedList<String>(); //lazy initialization if (results == null) { reset();//from w ww. j ava2 s .co m } try { ResultSetMetaData meta = results.getMetaData(); for (int i = 1; i <= meta.getColumnCount(); i++) { headers.add(meta.getColumnName(i)); } } catch (SQLException e) { throw new IOExceptionWithCause(e); } return headers; }
From source file:com.thinkbiganalytics.hive.service.HiveService.java
public QueryResult query(String query) throws DataAccessException { final DefaultQueryResult queryResult = new DefaultQueryResult(query); final List<QueryResultColumn> columns = new ArrayList<>(); final Map<String, Integer> displayNameMap = new HashMap<>(); if (query != null && !query.toLowerCase().startsWith("show")) { query = safeQuery(query);/*from ww w. ja v a2 s . c om*/ } try { // Setting in order to query complex formats like parquet jdbcTemplate.execute("set hive.optimize.index.filter=false"); jdbcTemplate.query(query, new RowMapper<Map<String, Object>>() { @Override public Map<String, Object> mapRow(ResultSet rs, int rowNum) throws SQLException { if (columns.isEmpty()) { ResultSetMetaData rsMetaData = rs.getMetaData(); for (int i = 1; i <= rsMetaData.getColumnCount(); i++) { String colName = rsMetaData.getColumnName(i); DefaultQueryResultColumn column = new DefaultQueryResultColumn(); column.setField(rsMetaData.getColumnName(i)); String displayName = rsMetaData.getColumnLabel(i); column.setHiveColumnLabel(displayName); //remove the table name if it exists displayName = StringUtils.substringAfterLast(displayName, "."); Integer count = 0; if (displayNameMap.containsKey(displayName)) { count = displayNameMap.get(displayName); count++; } displayNameMap.put(displayName, count); column.setDisplayName(displayName + "" + (count > 0 ? count : "")); column.setTableName(StringUtils.substringAfterLast(rsMetaData.getColumnName(i), ".")); column.setDataType(ParserHelper.sqlTypeToHiveType(rsMetaData.getColumnType(i))); columns.add(column); } queryResult.setColumns(columns); } Map<String, Object> row = new LinkedHashMap<>(); for (QueryResultColumn column : columns) { row.put(column.getDisplayName(), rs.getObject(column.getHiveColumnLabel())); } queryResult.addRow(row); return row; } }); } catch (DataAccessException dae) { dae.printStackTrace(); throw dae; } return queryResult; }
From source file:com.adaptris.jdbc.connection.FailoverConnection.java
private void testConnection() throws SQLException { Statement stmt = sqlConnection.createStatement(); ResultSet rs = null;//from w w w. j a v a2 s .co m try { if (isEmpty(config.getTestStatement())) { return; } if (config.getAlwaysValidateConnection()) { if (isDebugMode()) { rs = stmt.executeQuery(config.getTestStatement()); if (rs.next()) { StringBuffer sb = new StringBuffer("TestStatement Results - "); ResultSetMetaData rsm = rs.getMetaData(); for (int i = 1; i <= rsm.getColumnCount(); i++) { sb.append("["); sb.append(rsm.getColumnName(i)); sb.append("="); try { sb.append(rs.getString(i)); } catch (Exception e) { sb.append("'unknown'"); } sb.append("] "); } logR.trace(sb.toString()); } } else { stmt.execute(config.getTestStatement()); } } } finally { JdbcUtil.closeQuietly(rs); JdbcUtil.closeQuietly(stmt); } }