Description
Prints the ResultSetMetaData values for each column
License
Open Source License
Parameter
Parameter | Description |
---|
rsmd | a parameter |
out | a parameter |
Exception
Parameter | Description |
---|
SQLException | an exception |
IOException | an exception |
Declaration
public static void printResultSetMetadata(ResultSetMetaData rsmd,
Writer out) throws SQLException, IOException
Method Source Code
//package com.java2s;
/*/* ww w.j av a2s . co m*/
* JBoss, Home of Professional Open Source.
* See the COPYRIGHT.txt file distributed with this work for information
* regarding copyright ownership. Some portions may be licensed
* to Red Hat, Inc. under one or more contributor license agreements.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*/
import java.io.IOException;
import java.io.Writer;
import java.lang.reflect.Method;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
public class Main {
private static final String SPACER = " ";
private static final String NULL = "<null>";
private static final String MORE = "$ ";
private static String[] METADATA_METHODS = { "getColumnName", //$NON-NLS-1$
"getColumnType", //$NON-NLS-1$
"getCatalogName", //$NON-NLS-1$
"getColumnClassName", //$NON-NLS-1$
"getColumnLabel", //$NON-NLS-1$
"getColumnTypeName", //$NON-NLS-1$
"getSchemaName", //$NON-NLS-1$
"getTableName", //$NON-NLS-1$
"getColumnDisplaySize", //$NON-NLS-1$
"getPrecision", //$NON-NLS-1$
"getScale", //$NON-NLS-1$
"isAutoIncrement", //$NON-NLS-1$
"isCaseSensitive", //$NON-NLS-1$
"isCurrency", //$NON-NLS-1$
"isDefinitelyWritable", //$NON-NLS-1$
"isNullable", //$NON-NLS-1$
"isReadOnly", //$NON-NLS-1$
"isSearchable", //$NON-NLS-1$
"isSigned", //$NON-NLS-1$
"isWritable", //$NON-NLS-1$
};
/**
* Prints the ResultSetMetaData values for each column
* @param rsmd
* @param out
* @throws SQLException
* @throws IOException
* @since 4.2
*/
public static void printResultSetMetadata(ResultSetMetaData rsmd,
Writer out) throws SQLException, IOException {
int columns = rsmd.getColumnCount();
Class RSMD = ResultSetMetaData.class;
Class[] params = { int.class };
int numMethods = METADATA_METHODS.length;
String[][] metadataStrings = new String[columns][numMethods];
// Init the widths of the columns
int[] maxColWidths = new int[numMethods];
for (int i = 0; i < numMethods; i++) {
maxColWidths[i] = METADATA_METHODS[i].length();
}
// Buffer the metadata
for (int col = 1; col <= columns; col++) {
Object[] columnParam = { new Integer(col) };
for (int i = 0; i < numMethods; i++) {
try {
Method m = RSMD.getMethod(METADATA_METHODS[i], params);
Object obj = m.invoke(rsmd, columnParam);
String stringVal = (obj == null) ? NULL : obj
.toString();
metadataStrings[col - 1][i] = stringVal;
if (maxColWidths[i] < stringVal.length()) {
maxColWidths[i] = stringVal.length();
}
} catch (Throwable t) {
}
}
}
// Print the header
for (int i = 0; i < numMethods; i++) {
out.append(resizeString(METADATA_METHODS[i], maxColWidths[i]));
if (i != numMethods) {
out.append(SPACER);
}
}
out.append("\n");
// Print the metadata from the buffer
for (int col = 0; col < columns; col++) {
for (int i = 0; i < numMethods; i++) {
out.append(resizeString(metadataStrings[col][i],
maxColWidths[i]));
if (i != numMethods) {
out.append(SPACER);
}
}
out.append("\n");
}
}
private static String resizeString(Object obj, int size) {
if (obj == null) {
return resizeString(NULL, size);
}
String str = obj.toString();
if (str.length() == size) {
return str;
} else if (str.length() < size) {
return pad(str, size - str.length());
} else {
return str.substring(0, size) + MORE;
}
}
private static String pad(String str, int padding) {
StringBuffer buf = new StringBuffer(str);
for (int i = 0; i < padding; i++) {
buf.append(' ');
}
return buf.toString();
}
}
Related
- printResultSet(ResultSet rs)
- printResultSet(ResultSet rs)
- printResultSet(ResultSet rs)
- printResultSetColumns(ResultSet rs)
- printResultSetHeader(ResultSet rs)
- printResultSetRow(ResultSet rs)
- printResultsToFile(ResultSet rst, File outFile)
- printRow(ResultSet pk)
- printRsetTypeAndConcurrencyType(ResultSet rset)