List of usage examples for java.sql ResultSetMetaData getColumnName
String getColumnName(int column) throws SQLException;
From source file:com.ibm.research.rdf.store.jena.impl.DB2ResultSetImpl.java
public DB2ResultSetImpl(LiteralInfoResultSet rs, Store store, Connection c, List<String> list, VarExprList varExprList) {//ww w .j av a2 s . c om liRs = rs; set = rs.getResultSet(); try { ResultSetMetaData rsMetaData = set.getMetaData(); int numberOfColumns = rsMetaData.getColumnCount(); // get the column names; column indexes start from 1 for (int i = 1; i <= numberOfColumns; i++) { columnNames.add(rsMetaData.getColumnName(i)); } } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException("Error getting result metadata"); } this.store = store; connection = c; if (varExprList.isEmpty()) { varList = list; } else { List<Var> vars = varExprList.getVars(); varList = new ArrayList<String>(); for (int i = 0; i < vars.size(); i++) { varList.add(vars.get(i).getName()); } } }
From source file:com.taobao.tddl.common.jdbc.MetaDataQueryForMapHandler.java
/** * @param tableName //from ww w. ja v a 2 s .c o m */ private void initMetaData(String tableName, ResultSetMetaData rsmd) { try { int columnCount = rsmd.getColumnCount(); String[] columnNames = new String[columnCount]; ColumnMetaData[] columns = new ColumnMetaData[columnCount]; for (int i = 1; i <= columnCount; i++) { columnNames[i - 1] = rsmd.getColumnName(i).toLowerCase(); int sqlType = rsmd.getColumnType(i); if (sqlType == java.sql.Types.DATE) { sqlType = java.sql.Types.TIMESTAMP; } int scale = rsmd.getScale(i); String className = rsmd.getColumnClassName(i); columns[i - 1] = new ColumnMetaData(sqlType, scale, className); } TableMetaData tmd = new TableMetaData(columnNames, columns); this.tableMetaDatas.putIfAbsent(tableName, tmd); } catch (SQLException e) { log.warn("Fetch Metadata from resultSet failed.", e); } }
From source file:com.svds.resttest.services.GenericDataService.java
/** * Obtain metaDataSet from rs and add to genericResultsOutput * /*from w w w. jav a2 s . c om*/ * @param rs result set containing metadata * @param metaDataSet metadata map to add to * @param genericResultsOutput output object to add metadata to * @throws SQLException */ private void getMetaData(ResultSet rs, Map<String, Integer> metaDataSet, GenericResultsOutput genericResultsOutput) throws SQLException { LOG.info(">>>>Starting getMetaData"); ResultSetMetaData rsmd = rs.getMetaData(); int columnCount = rsmd.getColumnCount(); List<MetaData> metaDataArrayList = genericResultsOutput.getMetaData(); for (int i = 1; i <= columnCount; i++) { metaDataSet.put(rsmd.getColumnName(i), Integer.valueOf(i)); Map<String, Object> columnAttributes = new HashMap<>(); columnAttributes.put("ColumnTypeName", rsmd.getColumnTypeName(i)); columnAttributes.put("IndexNumber", i); MetaData metaData = new MetaData(); metaData.setColumnName(rsmd.getColumnName(i)); metaData.setColumnAttributes(columnAttributes); LOG.info("getColumnType : " + rsmd.getColumnType(i)); LOG.info("getColumnTypeName : " + rsmd.getColumnTypeName(i)); LOG.info("index : " + i); metaDataArrayList.add(metaData); } LOG.info(">>>>Ending getMetaData"); }
From source file:io.stallion.dataAccess.BeanListHandler.java
private String[] makeColumnToProperty(ResultSetMetaData rsmd, List<String> propertyNames) throws SQLException { int cols = rsmd.getColumnCount(); String[] columnToProperty = new String[cols + 1]; Arrays.fill(columnToProperty, ""); for (int col = 1; col <= cols; ++col) { String columnName = rsmd.getColumnLabel(col); if (null == columnName || 0 == columnName.length()) { columnName = rsmd.getColumnName(col); }/*from w w w. j a va2 s . c om*/ for (int i = 0; i < propertyNames.size(); ++i) { String propertyName = propertyNames.get(i); if (propertyName.equalsIgnoreCase(columnName)) { columnToProperty[col] = propertyName; break; } } } return columnToProperty; }
From source file:com.opensource.dbhelp.dbutils.CamelBeanProcessor.java
/** * /*from w w w . j a v a2 s . c om*/ * * @param rsmd * * @param props * * @return ?? */ @Override protected int[] mapColumnsToProperties(ResultSetMetaData rsmd, PropertyDescriptor[] props) throws SQLException { int cols = rsmd.getColumnCount(); int columnToProperty[] = new int[cols + 1]; Arrays.fill(columnToProperty, PROPERTY_NOT_FOUND); for (int col = 1; col <= cols; col++) { String columnName = rsmd.getColumnLabel(col); if (null == columnName || 0 == columnName.length()) { columnName = rsmd.getColumnName(col); } for (int i = 0; i < props.length; i++) { if (formatColName(columnName).equalsIgnoreCase(props[i].getName())) { columnToProperty[col] = i; break; } if (columnName.equalsIgnoreCase(props[i].getName())) { columnToProperty[col] = i; break; } } } return columnToProperty; }
From source file:org.executequery.databaseobjects.impl.AbstractNamedObject.java
Map<String, String> resultSetRowToMap(ResultSet rs) throws SQLException { ResultSetMetaData rsmd = rs.getMetaData(); int columnCount = rsmd.getColumnCount(); String[] metaColumnNames = new String[columnCount]; for (int i = 1; i < columnCount; i++) { metaColumnNames[i - 1] = rsmd.getColumnName(i); }//w w w . ja va 2 s.c o m Map<String, String> metaData = new HashMap<String, String>(columnCount); for (int i = 1; i < columnCount; i++) { metaData.put(metaColumnNames[i - 1].toUpperCase(), rs.getString(i)); } return metaData; }
From source file:ResultsDecoratorSQL.java
public void write(ResultSet rs) throws IOException, SQLException { ResultSetMetaData md = rs.getMetaData(); // This assumes you're not using a Join!! String tableName = md.getTableName(1); int cols = md.getColumnCount(); StringBuffer sb = new StringBuffer("insert into ").append(tableName).append("("); for (int i = 1; i <= cols; i++) { sb.append(md.getColumnName(i)); if (i != cols) { sb.append(", "); }/*from w ww . j a va 2s .co m*/ } sb.append(") values ("); String insertCommand = sb.toString(); while (rs.next()) { println(insertCommand); for (int i = 1; i <= cols; i++) { String tmp = rs.getString(i); if (rs.wasNull()) { print("null"); } else { int type = md.getColumnType(i); // Don't quote numeric types; quote all others for now. switch (type) { case Types.BIGINT: case Types.DECIMAL: case Types.DOUBLE: case Types.FLOAT: case Types.INTEGER: print(tmp); break; default: tmp = tmp.replaceAll("'", "''"); print("'" + tmp + "'"); } } if (i != cols) { print(", "); } } println(");"); } }
From source file:org.apache.hive.storage.jdbc.dao.GenericJdbcDatabaseAccessor.java
@Override public List<String> getColumnNames(Configuration conf) throws HiveJdbcDatabaseAccessException { Connection conn = null;//from w ww. ja va 2s . c om PreparedStatement ps = null; ResultSet rs = null; try { initializeDatabaseConnection(conf); String sql = JdbcStorageConfigManager.getQueryToExecute(conf); String metadataQuery = addLimitToQuery(sql, 1); LOGGER.debug("Query to execute is [{}]", metadataQuery); conn = dbcpDataSource.getConnection(); ps = conn.prepareStatement(metadataQuery); rs = ps.executeQuery(); ResultSetMetaData metadata = rs.getMetaData(); int numColumns = metadata.getColumnCount(); List<String> columnNames = new ArrayList<String>(numColumns); for (int i = 0; i < numColumns; i++) { columnNames.add(metadata.getColumnName(i + 1)); } return columnNames; } catch (Exception e) { LOGGER.error("Error while trying to get column names.", e); throw new HiveJdbcDatabaseAccessException("Error while trying to get column names: " + e.getMessage(), e); } finally { cleanupResources(conn, ps, rs); } }
From source file:be.bittich.dynaorm.repository.GenericDynaRepository.java
/** * Configuration//from ww w .j a va2 s .c o m */ private void configure() { runner = getQueryRunner(); dialect = getDialect(); // default tableName TableFromDB table = AnnotationProcessor.getAnnotationType(clazz, TableFromDB.class); String tableName = clazz.getSimpleName().toLowerCase(); if (table != null && !isEmpty(table.tableName())) { tableName = table.tableName(); } tableColumn = new TableColumn(tableName); rowProcessor = new DynaRowProcessor(tableColumn); try { ResultSet rs = runner.getDataSource().getConnection() .prepareStatement(dialect.requestForTableColumns(tableName)).executeQuery(); ResultSetMetaData metaData = rs.getMetaData(); Integer nbColumns = metaData.getColumnCount(); for (int i = 1; i <= nbColumns; i++) { String name = metaData.getColumnName(i); int type = metaData.getColumnType(i); tableColumn.addColumn(name, type); } } catch (SQLException ex) { LOG.log(Level.SEVERE, null, ex); } }
From source file:annis.sqlgen.FrequencySqlGenerator.java
@Override public FrequencyTable extractData(ResultSet rs) throws SQLException, DataAccessException { FrequencyTable result = new FrequencyTable(); ResultSetMetaData meta = rs.getMetaData(); while (rs.next()) { Validate.isTrue(meta.getColumnCount() > 1, "frequency table extractor needs at least 2 columns"); Validate.isTrue("count".equalsIgnoreCase(meta.getColumnName(meta.getColumnCount())), "last column name must be \"count\""); long count = rs.getLong("count"); String[] tupel = new String[meta.getColumnCount() - 1]; for (int i = 1; i <= tupel.length; i++) { String colVal = rs.getString(i); if (colVal == null) { tupel[i - 1] = ""; } else { String[] splitted = colVal.split(":", 3); if (splitted.length > 0) { colVal = splitted[splitted.length - 1]; }/*from w ww .j av a 2 s. c o m*/ tupel[i - 1] = colVal; } } // end for each column (except last "count" column) result.addEntry(new FrequencyTable.Entry(tupel, count)); } // end for complete result return result; }