當前位置: 首頁>>代碼示例>>Java>>正文


Java ResultSetMetaData.getColumnLabel方法代碼示例

本文整理匯總了Java中java.sql.ResultSetMetaData.getColumnLabel方法的典型用法代碼示例。如果您正苦於以下問題:Java ResultSetMetaData.getColumnLabel方法的具體用法?Java ResultSetMetaData.getColumnLabel怎麽用?Java ResultSetMetaData.getColumnLabel使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.sql.ResultSetMetaData的用法示例。


在下文中一共展示了ResultSetMetaData.getColumnLabel方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: mapColumnsToProperties

import java.sql.ResultSetMetaData; //導入方法依賴的package包/類
protected int[] mapColumnsToProperties(ResultSetMetaData rsmd, PropertyDescriptor[] props)
        throws SQLException
{
    int cols = rsmd.getColumnCount();
    int[] columnToProperty = new int[cols + 1];
    Arrays.fill(columnToProperty, -1);
    for (int col = 1; col <= cols; col++)
    {
        String columnName = rsmd.getColumnLabel(col);
        if ((null == columnName) || (0 == columnName.length())) {
            columnName = rsmd.getColumnName(col);
        }
        String propertyName = (String)this.columnToPropertyOverrides.get(columnName);
        if (propertyName == null) {
            propertyName = columnName;
        }
        for (int i = 0; i < props.length; i++) {
            if (propertyName.equalsIgnoreCase(props[i].getName()))
            {
                columnToProperty[col] = i;
                break;
            }
        }
    }
    return columnToProperty;
}
 
開發者ID:kingston-csj,項目名稱:jforgame,代碼行數:27,代碼來源:BeanProcessor.java

示例2: printTable

import java.sql.ResultSetMetaData; //導入方法依賴的package包/類
/**
 * Prints a table displaying specified columns, and checks the expected
 * number of rows.
 */
private void printTable(String table, String cols,
                        int expected) throws SQLException {

    int               rows = 0;
    ResultSet rs = stmnt.executeQuery("SELECT " + cols + " FROM " + table);
    ResultSetMetaData rsmd = rs.getMetaData();
    String result = "Table " + table + ", expecting " + expected
                    + " rows total:\n";

    while (rs.next()) {
        for (int i = 0; i < rsmd.getColumnCount(); i++) {
            result += rsmd.getColumnLabel(i + 1) + ":"
                      + rs.getString(i + 1) + ":";
        }

        result += "\n";

        rows++;
    }

    rs.close();
    System.out.println(result);
    assertEquals(expected, rows);
}
 
開發者ID:tiweGH,項目名稱:OpenDiabetes,代碼行數:29,代碼來源:TestMerge.java

示例3: printTable

import java.sql.ResultSetMetaData; //導入方法依賴的package包/類
private void printTable(String table, String cols, int expected) throws SQLException {
    int rows = 0;
    ResultSet rs = stmnt.executeQuery("SELECT " + cols + " FROM " + table);

    ResultSetMetaData rsmd = rs.getMetaData();
    String result = "Table " + table + ", expecting "
        + expected + " rows total:\n";

    while (rs.next()) {
        for (int i = 0; i < rsmd.getColumnCount(); i++) {
            result += rsmd.getColumnLabel(i + 1) + ":"
                       + rs.getString(i + 1) + ":";
        }
        result += "\n";
        rows++;
    }
    rs.close();
    System.out.println(result);
    assertEquals(expected, rows);
}
 
開發者ID:tiweGH,項目名稱:OpenDiabetes,代碼行數:21,代碼來源:TestMultiInsert.java

示例4: init

import java.sql.ResultSetMetaData; //導入方法依賴的package包/類
@Override
public void init(ResultSet rs) throws SQLException {		
	
	ResultSetMetaData metadata = rs.getMetaData();
	
	int columnCount = metadata.getColumnCount();
	this.columnLabels = new String[columnCount];
	
	for (int i = 1; i <= columnCount; i++){
		
		String colName = metadata.getColumnLabel(i);
		this.columnLabels[i - 1] = colName;
	}
}
 
開發者ID:joseerodrigues,項目名稱:db_utils,代碼行數:15,代碼來源:MapResultSetMapper.java

示例5: getAttributes

import java.sql.ResultSetMetaData; //導入方法依賴的package包/類
public static List<Attribute> getAttributes(ResultSetMetaData metaData) throws SQLException {
    LinkedList result = new LinkedList();
    if(metaData != null) {
        HashMap duplicateNameMap = new HashMap();

        for(int columnIndex = 1; columnIndex <= metaData.getColumnCount(); ++columnIndex) {
            String dbColumnName = metaData.getColumnLabel(columnIndex);
            String columnName = dbColumnName;
            Integer duplicateCount = (Integer)duplicateNameMap.get(dbColumnName);
            boolean isUnique = duplicateCount == null;
            if(isUnique) {
                duplicateNameMap.put(dbColumnName, new Integer(1));
            } else {
                while(!isUnique) {
                    duplicateCount = new Integer(duplicateCount.intValue() + 1);
                    columnName = dbColumnName + "_" + (duplicateCount.intValue() - 1);
                    isUnique = duplicateNameMap.get(columnName) == null;
                }

                duplicateNameMap.put(dbColumnName, duplicateCount);
            }

            int attributeType = DatabaseHandler.getRapidMinerTypeIndex(metaData.getColumnType(columnIndex));
            Attribute attribute = AttributeFactory.createAttribute(columnName, attributeType);
            attribute.getAnnotations().setAnnotation("sql_type", metaData.getColumnTypeName(columnIndex));
            result.add(attribute);
        }
    }

    return result;
}
 
開發者ID:transwarpio,項目名稱:rapidminer,代碼行數:32,代碼來源:DatabaseDataReader.java

示例6: queryMap

import java.sql.ResultSetMetaData; //導入方法依賴的package包/類
/**
 * 查詢返回一個map
 * @param Connection 數據庫鏈接
 * @param sql
 * @param entity
 * @return
 */
public static Map<String, Object> queryMap(Connection connection, String sql) {
	Statement statement = null;
	Map<String, Object> result = new HashMap<>();
	try{
		statement = connection.createStatement();
		ResultSet rs = statement.executeQuery(sql);
		ResultSetMetaData rsmd = rs.getMetaData();

		while (rs.next()) {
			int cols = rsmd.getColumnCount();
			for (int i = 1; i <= cols; i++)
			{
				String columnName = rsmd.getColumnLabel(i);
				if ((null == columnName) || (0 == columnName.length())) {
					columnName = rsmd.getColumnName(i);
				}
				result.put(columnName, rs.getObject(i));
			}
			break;
		}
	}catch(Exception e) {
		logger.error("DbUtils queryMap failed", e);
	}finally {
		if (connection != null) {
			try{
				connection.close();
			}catch(Exception e2) {
				logger.error("DbUtils queryMap failed", e2);
			}
		}
	}
	return result;
}
 
開發者ID:kingston-csj,項目名稱:jforgame,代碼行數:41,代碼來源:DbHelper.java

示例7: query

import java.sql.ResultSetMetaData; //導入方法依賴的package包/類
public static List query(Class c, String sql, Object... p) {
	List list = new ArrayList();
	Connection connection = getConnection();
	try {
		PreparedStatement ps = connection.prepareStatement(sql);
		if (p != null) {
			for (int i = 0; i < p.length; i++) {
				ps.setObject(i + 1, p[i]);
			}
		}
		ResultSet rs = ps.executeQuery();
		ResultSetMetaData rsmd = rs.getMetaData();
		int count = rsmd.getColumnCount();
		while (rs.next()) {
			Object object = c.newInstance();
			for (int i = 1; i <= count; i++) {
				String fieldname = rsmd.getColumnLabel(i);
				Field field = c.getDeclaredField(fieldname);
				field.setAccessible(true);
				field.set(object, rs.getObject(i));
			}
			list.add(object);
		}
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		releaseConnection(connection);
	}

	return list;
}
 
開發者ID:sivanWu0222,項目名稱:Registration,代碼行數:32,代碼來源:DButil.java

示例8: init

import java.sql.ResultSetMetaData; //導入方法依賴的package包/類
@Override
public void init(ResultSet rs) throws SQLException {

    ResultSetMetaData metadata = rs.getMetaData();
    int colCount = metadata.getColumnCount();

    for (int i = 1; i <= colCount; i++){

        String colLabel = metadata.getColumnLabel(i);

        if (!this.colsFields.containsKey(colLabel)){
            logger.warn("Column '" + colLabel + "' not found");
        }
    }
}
 
開發者ID:joseerodrigues,項目名稱:db_utils,代碼行數:16,代碼來源:BeanResultSetMapper.java

示例9: populate

import java.sql.ResultSetMetaData; //導入方法依賴的package包/類
/**
 * 
 * @param rs
 * @param builder
 * @throws SQLException
 */
private void populate(ResultSet rs, Message.Builder builder)
		throws SQLException {
	ResultSetMetaData metaData = rs.getMetaData();
	int columnCount = metaData.getColumnCount();// 列個數
	String columnLabel = null;// 列名
	Object columnValue = null;// 列值
	Descriptors.FieldDescriptor fieldDescriptor = null;
	for (int i = 1; i <= columnCount; i++) {
		columnLabel = metaData.getColumnLabel(i);
		columnValue = rs.getObject(i);
		if (columnValue == null)
			continue;// 如果為空,繼續下一個
		fieldDescriptor = descriptor.findFieldByName(columnLabel);
		if (fieldDescriptor == null)
			continue;// 如果為空,繼續下一個
		// 轉換為相應的類型 ,會自動將 date 類型轉換為long
		if (fieldDescriptor.getType().equals(FieldDescriptor.Type.ENUM)) {
			columnValue = fieldDescriptor.getEnumType().findValueByNumber(
					(int) columnValue);
		} else {
			columnValue = ConvertUtils.convert(columnValue, fieldDescriptor
					.getDefaultValue().getClass());
		}
		builder.setField(fieldDescriptor, columnValue);
	}
}
 
開發者ID:jigsaw-projects,項目名稱:jigsaw-payment,代碼行數:33,代碼來源:JdbcProtobufTemplate.java

示例10: createDynaProperty

import java.sql.ResultSetMetaData; //導入方法依賴的package包/類
/**
 * <p>Factory method to create a new DynaProperty for the given index
 * into the result set metadata.</p>
 *
 * @param metadata is the result set metadata
 * @param i is the column index in the metadata
 * @return the newly created DynaProperty instance
 * @throws SQLException If an error occurs accessing the SQL metadata
 */
protected DynaProperty createDynaProperty(
                                final ResultSetMetaData metadata,
                                final int i)
                                throws SQLException {

    String columnName = null;
    if (useColumnLabel) {
        columnName = metadata.getColumnLabel(i);
    }
    if (columnName == null || columnName.trim().length() == 0) {
        columnName = metadata.getColumnName(i);
    }
    final String name = lowerCase ? columnName.toLowerCase() : columnName;
    if (!name.equals(columnName)) {
        if (columnNameXref == null) {
            columnNameXref = new HashMap<String, String>();
        }
        columnNameXref.put(name, columnName);
    }
    String className = null;
    try {
        final int sqlType = metadata.getColumnType(i);
        switch (sqlType) {
            case java.sql.Types.DATE:
                return new DynaProperty(name, java.sql.Date.class);
            case java.sql.Types.TIMESTAMP:
                return new DynaProperty(name, java.sql.Timestamp.class);
            case java.sql.Types.TIME:
                return new DynaProperty(name, java.sql.Time.class);
            default:
                className = metadata.getColumnClassName(i);
        }
    } catch (final 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);

}
 
開發者ID:yippeesoft,項目名稱:NotifyTools,代碼行數:56,代碼來源:JDBCDynaClass.java

示例11: buildLabelNamesAndTypes

import java.sql.ResultSetMetaData; //導入方法依賴的package包/類
private static final void buildLabelNamesAndTypes(ResultSetMetaData rsmd, String[] labelNames, int[] types) throws SQLException {
	for (int i=1; i<labelNames.length; i++) {
		labelNames[i] = rsmd.getColumnLabel(i);
		types[i] = rsmd.getColumnType(i);
	}
}
 
開發者ID:WhatAKitty,項目名稱:spark-project,代碼行數:7,代碼來源:ModelBuilder.java

示例12: setup

import java.sql.ResultSetMetaData; //導入方法依賴的package包/類
@Override
public void setup(OperatorContext operatorContext, OutputMutator output) throws ExecutionSetupException {
  try {

    this.operatorContext = operatorContext;
    connection = source.getConnection();
    statement = connection.createStatement();
    resultSet = statement.executeQuery(sql);

    final ResultSetMetaData meta = resultSet.getMetaData();
    final int columns = meta.getColumnCount();
    ImmutableList.Builder<ValueVector> vectorBuilder = ImmutableList.builder();
    ImmutableList.Builder<Copier<?>> copierBuilder = ImmutableList.builder();

    for (int i = 1; i <= columns; i++) {
      final String name = meta.getColumnLabel(i);
      final int jdbcType = meta.getColumnType(i);
      final int width = meta.getPrecision(i);
      final int scale = meta.getScale(i);
      MinorType minorType = JDBC_TYPE_MAPPINGS.get(jdbcType);
      if (minorType == null) {
        throw UserException.dataReadError()
            .message("The JDBC storage plugin failed while trying to execute a query. "
                + "The JDBC data type %d is not currently supported.", jdbcType)

            .addContext("sql", sql)
            .addContext("plugin", storagePluginName)
            .build(logger);
      }

      final MajorType type = Types.optional(minorType);
      final MaterializedField field = MaterializedField.create(name, type);
      final Class<? extends ValueVector> clazz = (Class<? extends ValueVector>) TypeHelper.getValueVectorClass(
          minorType, type.getMode());
      ValueVector vector = output.addField(field, clazz);
      vectorBuilder.add(vector);
      copierBuilder.add(getCopier(jdbcType, i, resultSet, vector));

    }

    vectors = vectorBuilder.build();
    copiers = copierBuilder.build();

  } catch (SQLException | SchemaChangeException e) {
    throw UserException.dataReadError(e)
        .message("The JDBC storage plugin failed while trying setup the SQL query. ")
        .addContext("sql", sql)
        .addContext("plugin", storagePluginName)
        .build(logger);
  }
}
 
開發者ID:skhalifa,項目名稱:QDrill,代碼行數:52,代碼來源:JdbcRecordReader.java

示例13: extractColumnAlias

import java.sql.ResultSetMetaData; //導入方法依賴的package包/類
@Override
public String extractColumnAlias(ResultSetMetaData metaData, int position) throws SQLException {
	return metaData.getColumnLabel( position );
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:5,代碼來源:ColumnAliasExtractor.java

示例14: appendResultSetSlashGStyle

import java.sql.ResultSetMetaData; //導入方法依賴的package包/類
public static StringBuilder appendResultSetSlashGStyle(StringBuilder appendTo, ResultSet rs) throws SQLException {
    ResultSetMetaData rsmd = rs.getMetaData();

    int numFields = rsmd.getColumnCount();
    int maxWidth = 0;

    String[] fieldNames = new String[numFields];

    for (int i = 0; i < numFields; i++) {
        fieldNames[i] = rsmd.getColumnLabel(i + 1);

        if (fieldNames[i].length() > maxWidth) {
            maxWidth = fieldNames[i].length();
        }
    }

    int rowCount = 1;

    while (rs.next()) {
        appendTo.append("*************************** ");
        appendTo.append(rowCount++);
        appendTo.append(". row ***************************\n");

        for (int i = 0; i < numFields; i++) {
            int leftPad = maxWidth - fieldNames[i].length();

            for (int j = 0; j < leftPad; j++) {
                appendTo.append(" ");
            }

            appendTo.append(fieldNames[i]);
            appendTo.append(": ");

            String stringVal = rs.getString(i + 1);

            if (stringVal != null) {
                appendTo.append(stringVal);
            } else {
                appendTo.append("NULL");
            }

            appendTo.append("\n");
        }

        appendTo.append("\n");
    }

    return appendTo;
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:50,代碼來源:ResultSetUtil.java

示例15: formatResultSet

import java.sql.ResultSetMetaData; //導入方法依賴的package包/類
private void formatResultSet(ResultSet r) {

        if (r == null) {
            String g[] = new String[1];

            g[0] = "Result";

            gResult.setHead(g);

            g[0] = "(empty)";

            gResult.addRow(g);

            return;
        }

        try {
            ResultSetMetaData m   = r.getMetaData();
            int               col = m.getColumnCount();
            Object            h[] = new Object[col];

            for (int i = 1; i <= col; i++) {
                h[i - 1] = m.getColumnLabel(i);
            }

            gResult.setHead(h);

            while (r.next()) {
                for (int i = 1; i <= col; i++) {
                    h[i - 1] = r.getObject(i);

                    if (r.wasNull()) {
                        h[i - 1] = null;    // = "(null)";
                    }
                }

                gResult.addRow(h);
            }

            r.close();
        } catch (SQLException e) {}
    }
 
開發者ID:parabuild-ci,項目名稱:parabuild-ci,代碼行數:43,代碼來源:DatabaseManagerSwing.java


注:本文中的java.sql.ResultSetMetaData.getColumnLabel方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。