List of usage examples for java.sql ResultSetMetaData getColumnName
String getColumnName(int column) throws SQLException;
From source file:RowSetModel.java
public String getColumnName(int col) { try {// w w w . ja v a 2s .c o m ResultSetMetaData meta = rowSet.getMetaData(); if (meta == null) { return null; } return meta.getColumnName(col + 1); } catch (SQLException e) { return "Error"; } }
From source file:io.lightlink.oracle.AbstractOracleType.java
protected Map<String, Object> getMapFromStruct(OracleConnection con, Struct struct) throws SQLException { Object[] attributes = struct.getAttributes(); HashMap<String, Object> res = new HashMap<String, Object>(); StructDescriptor structType = safeCreateStructureDescriptor(struct.getSQLTypeName(), con); ResultSetMetaData stuctMeteData = structType.getMetaData(); for (int col = 1; col <= stuctMeteData.getColumnCount(); col++) { String columnName = stuctMeteData.getColumnName(col); res.put(columnName, attributes[col - 1]); }//from w w w . ja va2 s . co m return res; }
From source file:com.aw.core.dao.AWQueryExecuter.java
private String[] buildColumnNames(ResultSet rs, BeanWrapper wrapper) throws SQLException { ResultSetMetaData meta = rs.getMetaData(); List<String> colNames = new ArrayList<String>(meta.getColumnCount()); for (int i = 0; i < meta.getColumnCount(); i++) { String colName = meta.getColumnName(i + 1); if (wrapper != null && wrapper.isWritableProperty(colName)) colNames.add(colName);/*from w w w .j a v a2 s. com*/ } return colNames.toArray(new String[colNames.size()]); }
From source file:net.mlw.vlh.adapter.jdbc.dynabean.fix.JDBCDynaClass.java
/** * <p>Factory method to create a new DynaProperty for the given index * into the result set metadata.</p> * /*from ww w. j a va 2s .co m*/ * @param metadata is the result set metadata * @param i is the column index in the metadata * @return the newly created DynaProperty instance */ protected DynaProperty createDynaProperty(ResultSetMetaData metadata, int i) throws SQLException { String name = (useName) ? metadata.getColumnName(i) : metadata.getColumnLabel(i); if (lowerCase) { name = name.toLowerCase(); } String className = null; try { className = metadata.getColumnClassName(i); } catch (SQLException e) { // this is a patch for HsqlDb to ignore exceptions // thrown by its metadata implementation } // Default to Object type if no class name could be retrieved // from the metadata Class clazz = Object.class; if (className != null) { clazz = loadClass(className); } return new DynaProperty(name, clazz); }
From source file:jp.co.golorp.emarf.model.Models.java
/** * Model?????IO/*from w w w . j av a2 s. c om*/ * * @param sql * sql * @param params * params * @return List */ private static List<Map<String, Object>> getDatas(final String sql, final Object... params) { // ?SQL? String rawSql = getRawSql(sql, params); // SQL?????? List<Map<String, Object>> datas = ModelsCache.get(rawSql); if (datas != null) { return datas; } // statementLog(rawSql); // ? PreparedStatement ps = null; try { ps = Connections.get().prepareStatement(sql); for (int i = 0; i < params.length; i++) { ps.setString(i + 1, String.valueOf(params[i])); } // ?? datas = new ArrayList<Map<String, Object>>(); ResultSet rs = null; try { // ??? rs = ps.executeQuery(); while (rs.next()) { // ? Map<String, Object> data = new LinkedHashMap<String, Object>(); // ResultSet?META? ResultSetMetaData meta = rs.getMetaData(); // META???? int columnCount = meta.getColumnCount(); for (int i = 1; i <= columnCount; i++) { // ??? String columnName = meta.getColumnName(i); // ??? String propertyName = StringUtil.toCamelCase(columnName); String key = propertyName; if (data.containsKey(propertyName)) { String modelName = StringUtil.toUpperCamelCase(meta.getTableName(i)); key = modelName + "." + propertyName; } // data.put(key, rs.getObject(columnName)); } // datas.add(data); } } catch (SQLException e) { throw new SystemError(e); } finally { IOUtil.closeQuietly(rs); } } catch (SQLException e) { throw new SystemError(e); } finally { IOUtil.closeQuietly(ps); } // SQL? // ModelsCache.set(rawSql, datas); return datas; }
From source file:ch.cern.db.flume.sink.kite.util.InferSchemaFromTable.java
public Schema getSchema() throws SQLException { FieldAssembler<Schema> builder = SchemaBuilder.record("log").fields(); Connection connection = DriverManager.getConnection(connection_url, connection_user, connection_password); Statement statement = connection.createStatement(); String query = "SELECT * " + "FROM " + tableName + " WHERE 0=1"; LOG.info("Running query for obtaining metadata: " + query); ResultSet result = statement.executeQuery(query); ResultSetMetaData metadata = result.getMetaData(); int columnCount = metadata.getColumnCount(); for (int i = 1; i <= columnCount; i++) { String columnName = metadata.getColumnName(i); int columnType = metadata.getColumnType(i); boolean nullable = metadata.isNullable(i) != ResultSetMetaData.columnNoNulls; FieldTypeBuilder<Schema> field = builder.name(columnName).doc("SQL type: " + columnType).type(); switch (columnType) { case java.sql.Types.SMALLINT: case java.sql.Types.TINYINT: case java.sql.Types.INTEGER: case java.sql.Types.BIGINT: if (nullable) field.nullable().intType().noDefault(); else//from w ww .j a v a2 s .c o m field.intType().noDefault(); break; case java.sql.Types.BOOLEAN: if (nullable) field.nullable().booleanType().noDefault(); else field.booleanType().noDefault(); break; case java.sql.Types.NUMERIC: case java.sql.Types.DOUBLE: case java.sql.Types.FLOAT: if (nullable) field.nullable().doubleType().noDefault(); else field.doubleType().noDefault(); break; case java.sql.Types.TIMESTAMP: case -101: //TIMESTAMP(3) WITH TIME ZONE case -102: //TIMESTAMP(6) WITH LOCAL TIME ZONE default: if (nullable) field.nullable().stringType().noDefault(); else field.stringType().noDefault(); break; } } return builder.endRecord(); }
From source file:jongo.handler.ResultSetMetaDataHandler.java
@Override public List<Row> handle(ResultSet rs) throws SQLException { List<Row> results = new ArrayList<Row>(); int rowId = 0; ResultSetMetaData metaData = rs.getMetaData(); Map<String, String> map = null; for (int i = 1; i <= metaData.getColumnCount(); i++) { map = new HashMap<String, String>(2); map.put("tableName", metaData.getTableName(i)); map.put("columnName", metaData.getColumnName(i)); map.put("columnLabel", metaData.getColumnLabel(i)); map.put("columnType", metaData.getColumnTypeName(i)); map.put("columnSize", String.valueOf(metaData.getColumnDisplaySize(i))); map.put("precision", String.valueOf(metaData.getPrecision(i))); map.put("scale", String.valueOf(metaData.getScale(i))); // map.put("catalog_name", metaData.getCatalogName(i)); // map.put("column_class_name", metaData.getColumnClassName(i)); // map.put("schema_name", metaData.getSchemaName(i)); // map.put("column_type", String.valueOf(metaData.getColumnType(i))); if (map != null) results.add(new Row(rowId++, map)); }//ww w . j av a 2 s .co m return results; }
From source file:net.mindengine.oculus.frontend.db.jdbc.BeanMapper.java
@Override public Object mapRow(ResultSet rs, int rowNum) throws SQLException { // TODO Auto-generated method stub ResultSetMetaData metaData = rs.getMetaData(); int columns = metaData.getColumnCount(); try {//from w w w . ja v a 2 s. c o m Class<?> clazz = Class.forName(className); Object obj = clazz.newInstance(); for (int i = 1; i <= columns; i++) { String column = metaData.getColumnName(i); FieldMapper fm = fields.get(column); if (fm != null) { Method method = fm.getMethod(); String typeName = fm.getType().getSimpleName(); typeName = StringUtils.capitalize(typeName); if (typeName.endsWith("[]")) { typeName = typeName.substring(0, typeName.length() - 2) + "s"; } if (typeName.equals("Date")) { typeName = "Timestamp"; } if (typeName.equals("Integer")) { typeName = "Int"; } Method rsMethod = ResultSet.class.getMethod("get" + typeName, String.class); Object value = rsMethod.invoke(rs, column); if (value instanceof Timestamp) { Timestamp timestamp = (Timestamp) value; value = new Date(timestamp.getTime()); } method.invoke(obj, value); } } return obj; } catch (Exception e) { throw new SQLException(e); } }
From source file:org.apache.hadoop.hive.jdbc.storagehandler.JdbcSerDeHelper.java
public void initialize(Properties tblProps, Configuration sysConf) { setProperties(tblProps, sysConf);/*from w w w. j a v a 2s. c o m*/ StringBuilder colNames = new StringBuilder(); StringBuilder colTypeNames = new StringBuilder(); DBConfiguration dbConf = new DBConfiguration(sysConf); try { dbConnection = dbConf.getConnection(); String query = getSelectQuery(tblProps); Statement st = dbConnection.createStatement(); ResultSet rs = st.executeQuery(query); ResultSetMetaData rsmd = rs.getMetaData(); int columnsNumber = rsmd.getColumnCount(); int i = 0; for (i = 1; i < columnsNumber; i++) { String colName = rsmd.getColumnName(i).replaceAll("\\.", "\\$"); String colType = rsmd.getColumnTypeName(i); colNames.append(colName + ","); colTypeNames.append(sqlToHiveColumnTypeNames(colType) + ":"); } colNames.append(rsmd.getColumnName(i).replaceAll("\\.", "\\$")); colTypeNames.append(rsmd.getColumnTypeName(i)); columnNames = colNames.toString(); columnTypeNames = colTypeNames.toString(); rs.close(); st.close(); dbConnection.close(); } catch (Exception ex) { throw new RuntimeException(ex); } }
From source file:org.apache.usergrid.query.validator.SqliteRunner.java
@Override public List<Entity> execute(String query) { List<Entity> entities = jdbcTemplate.query(query, new RowMapper<Entity>() { @Override//from w ww . j a v a 2 s. c o m public Entity mapRow(ResultSet rs, int i) throws SQLException { ResultSetMetaData rsmd = rs.getMetaData(); Entity entity = new QueryEntity(); for (int j = 1; j <= rsmd.getColumnCount(); j++) { entity.setProperty(rsmd.getColumnName(j), rs.getObject(j)); } return entity; } }); return entities; }