本文整理汇总了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;
}
示例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);
}
示例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);
}
示例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;
}
}
示例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;
}
示例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;
}
示例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;
}
示例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");
}
}
}
示例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);
}
}
示例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);
}
示例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);
}
}
示例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);
}
}
示例13: extractColumnAlias
import java.sql.ResultSetMetaData; //导入方法依赖的package包/类
@Override
public String extractColumnAlias(ResultSetMetaData metaData, int position) throws SQLException {
return metaData.getColumnLabel( position );
}
示例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;
}
示例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) {}
}