use of java.sql.ResultSetMetaData in project jdk8u_jdk by JetBrains.
the class CommonCachedRowSetTests method compareRowSets.
/*
* Utility method to compare two rowsets
*/
private void compareRowSets(CachedRowSet crs, CachedRowSet crs1) throws Exception {
int rows = crs.size();
assertTrue(rows == crs1.size());
ResultSetMetaData rsmd = crs.getMetaData();
compareMetaData(rsmd, crs1.getMetaData());
int cols = rsmd.getColumnCount();
for (int row = 1; row <= rows; row++) {
crs.absolute((row));
crs1.absolute(row);
for (int col = 1; col <= cols; col++) {
compareColumnValue(JDBCType.valueOf(rsmd.getColumnType(col)), crs, crs1, col);
}
}
}
use of java.sql.ResultSetMetaData in project jdk8u_jdk by JetBrains.
the class RowSetMetaDataTests method test99.
/*
* Validate isWrapperFor and unwrap work correctly
*/
@SuppressWarnings("unchecked")
@Test
public void test99() throws Exception {
RowSetMetaData rsmd1 = rsmd;
ResultSetMetaData rsmd2 = rsmd;
Class clzz = rsmd.getClass();
assertTrue(rsmd1.isWrapperFor(clzz));
assertTrue(rsmd2.isWrapperFor(clzz));
RowSetMetaDataImpl rsmdi = (RowSetMetaDataImpl) rsmd2.unwrap(clzz);
// False should be returned
assertFalse(rsmd1.isWrapperFor(this.getClass()));
assertFalse(rsmd2.isWrapperFor(this.getClass()));
}
use of java.sql.ResultSetMetaData in project druid by alibaba.
the class ResultSetProxyImpl method getMetaData.
@Override
public ResultSetMetaData getMetaData() throws SQLException {
FilterChainImpl chain = createChain();
ResultSetMetaData value = chain.resultSet_getMetaData(this);
recycleFilterChain(chain);
return value;
}
use of java.sql.ResultSetMetaData in project druid by alibaba.
the class JdbcUtils method printResultSet.
public static void printResultSet(ResultSet rs, PrintStream out, boolean printHeader, String seperator) throws SQLException {
ResultSetMetaData metadata = rs.getMetaData();
int columnCount = metadata.getColumnCount();
if (printHeader) {
for (int columnIndex = 1; columnIndex <= columnCount; ++columnIndex) {
if (columnIndex != 1) {
out.print(seperator);
}
out.print(metadata.getColumnName(columnIndex));
}
}
out.println();
while (rs.next()) {
for (int columnIndex = 1; columnIndex <= columnCount; ++columnIndex) {
if (columnIndex != 1) {
out.print(seperator);
}
int type = metadata.getColumnType(columnIndex);
if (type == Types.VARCHAR || type == Types.CHAR || type == Types.NVARCHAR || type == Types.NCHAR) {
out.print(rs.getString(columnIndex));
} else if (type == Types.DATE) {
Date date = rs.getDate(columnIndex);
if (rs.wasNull()) {
out.print("null");
} else {
out.print(date.toString());
}
} else if (type == Types.BIT) {
boolean value = rs.getBoolean(columnIndex);
if (rs.wasNull()) {
out.print("null");
} else {
out.print(Boolean.toString(value));
}
} else if (type == Types.BOOLEAN) {
boolean value = rs.getBoolean(columnIndex);
if (rs.wasNull()) {
out.print("null");
} else {
out.print(Boolean.toString(value));
}
} else if (type == Types.TINYINT) {
byte value = rs.getByte(columnIndex);
if (rs.wasNull()) {
out.print("null");
} else {
out.print(Byte.toString(value));
}
} else if (type == Types.SMALLINT) {
short value = rs.getShort(columnIndex);
if (rs.wasNull()) {
out.print("null");
} else {
out.print(Short.toString(value));
}
} else if (type == Types.INTEGER) {
int value = rs.getInt(columnIndex);
if (rs.wasNull()) {
out.print("null");
} else {
out.print(Integer.toString(value));
}
} else if (type == Types.BIGINT) {
long value = rs.getLong(columnIndex);
if (rs.wasNull()) {
out.print("null");
} else {
out.print(Long.toString(value));
}
} else if (type == Types.TIMESTAMP) {
out.print(String.valueOf(rs.getTimestamp(columnIndex)));
} else if (type == Types.DECIMAL) {
out.print(String.valueOf(rs.getBigDecimal(columnIndex)));
} else if (type == Types.CLOB) {
out.print(String.valueOf(rs.getString(columnIndex)));
} else if (type == Types.JAVA_OBJECT) {
Object object = rs.getObject(columnIndex);
if (rs.wasNull()) {
out.print("null");
} else {
out.print(String.valueOf(object));
}
} else if (type == Types.LONGVARCHAR) {
Object object = rs.getString(columnIndex);
if (rs.wasNull()) {
out.print("null");
} else {
out.print(String.valueOf(object));
}
} else if (type == Types.NULL) {
out.print("null");
} else {
Object object = rs.getObject(columnIndex);
if (rs.wasNull()) {
out.print("null");
} else {
if (object instanceof byte[]) {
byte[] bytes = (byte[]) object;
String text = HexBin.encode(bytes);
out.print(text);
} else {
out.print(String.valueOf(object));
}
}
}
}
out.println();
}
}
use of java.sql.ResultSetMetaData in project druid by alibaba.
the class JdbcUtils method executeQuery.
public static List<Map<String, Object>> executeQuery(Connection conn, String sql, List<Object> parameters) throws SQLException {
List<Map<String, Object>> rows = new ArrayList<Map<String, Object>>();
PreparedStatement stmt = null;
ResultSet rs = null;
try {
stmt = conn.prepareStatement(sql);
setParameters(stmt, parameters);
rs = stmt.executeQuery();
ResultSetMetaData rsMeta = rs.getMetaData();
while (rs.next()) {
Map<String, Object> row = new LinkedHashMap<String, Object>();
for (int i = 0, size = rsMeta.getColumnCount(); i < size; ++i) {
String columName = rsMeta.getColumnLabel(i + 1);
Object value = rs.getObject(i + 1);
row.put(columName, value);
}
rows.add(row);
}
} finally {
JdbcUtils.close(rs);
JdbcUtils.close(stmt);
}
return rows;
}
Aggregations