use of java.sql.ResultSetMetaData in project ats-framework by Axway.
the class AbstractDbProvider method logDebugInfoForDBValue.
/**
* Trace with Debug severity info about retrieved value such as the DB and JDBC type
* @param value value already got from the database
* @param index the column index (starting from 1) of the cell
* @param resultSet needed for extra
* @throws SQLException
*/
protected void logDebugInfoForDBValue(Object value, int index, ResultSet resultSet) throws SQLException {
if (log.isDebugEnabled()) {
// trace column type too
ResultSetMetaData metaData = resultSet.getMetaData();
String dbType = metaData.getColumnTypeName(index);
int javaType = metaData.getColumnType(index);
log.debug("DB value is '" + value + "' (retrieved as " + value.getClass().getSimpleName() + "), JDBC type " + javaType + ", DB type " + dbType);
}
}
use of java.sql.ResultSetMetaData in project Rosetta by HubSpot.
the class RosettaMapper method mapRow.
/**
* Map a single ResultSet row to a T instance.
*
* @throws SQLException
*/
public T mapRow(ResultSet rs) throws SQLException {
Map<String, Object> map = new HashMap<String, Object>();
ResultSetMetaData metadata = rs.getMetaData();
for (int i = 1; i <= metadata.getColumnCount(); ++i) {
String label = metadata.getColumnLabel(i);
final Object value;
// calling getObject on a BLOB/CLOB produces weird results
switch(metadata.getColumnType(i)) {
case Types.BLOB:
value = rs.getBytes(i);
break;
case Types.CLOB:
value = rs.getString(i);
break;
default:
value = rs.getObject(i);
}
// don't use table name extractor because we don't want aliased table name
boolean overwrite = metadata.getTableName(i).equals(this.tableName);
String tableName = TABLE_NAME_EXTRACTOR.getTableName(metadata, i);
if (tableName != null && !tableName.isEmpty()) {
String qualifiedName = tableName + "." + metadata.getColumnName(i);
add(map, qualifiedName, value, overwrite);
}
add(map, label, value, overwrite);
}
return objectMapper.convertValue(map, type);
}
use of java.sql.ResultSetMetaData in project enhydrator by AdamBien.
the class ResultSetToEntries method apply.
@Override
public Row apply(ResultSet resultSet) {
Row row = new Row();
try {
final ResultSetMetaData metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount();
int columnIndex = 0;
for (int i = 1; i <= columnCount; i++) {
columnIndex++;
//from java.sql.Types
int columnType = metaData.getColumnType(i);
String columnName = metaData.getColumnName(i);
switch(columnType) {
case Types.VARCHAR:
case Types.CHAR:
row.addColumn(columnIndex, columnName, resultSet.getString(i));
break;
case Types.INTEGER:
row.addColumn(columnIndex, columnName, resultSet.getInt(i));
break;
case Types.DOUBLE:
row.addColumn(columnIndex, columnName, resultSet.getDouble(i));
break;
case Types.BOOLEAN:
row.addColumn(columnIndex, columnName, resultSet.getBoolean(i));
break;
case Types.FLOAT:
row.addColumn(columnIndex, columnName, resultSet.getFloat(i));
break;
default:
row.addColumn(columnIndex, columnName, resultSet.getObject(i));
}
}
} catch (SQLException ex) {
throw new IllegalStateException("Problems accessing ResultSet", ex);
}
return row;
}
use of java.sql.ResultSetMetaData in project nymph by Onnt.
the class SqlDbCurdResultAsObject method createObj.
@SuppressWarnings({ "rawtypes", "unchecked" })
private Object createObj(Class clazz) throws InstantiationException, IllegalAccessException, SQLException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
ResultSetMetaData rsmd = getResultSetMetaData();
if (rsmd == null)
return null;
T obj = (T) clazz.newInstance();
int columnCount = getColumnCount(rsmd);
for (int i = 1; i <= columnCount; i++) {
String cname = rsmd.getColumnName(i);
int ctype = rsmd.getColumnType(i);
Field[] fields = clazz.getDeclaredFields();
Method method = null;
for (Field field : fields) {
if (cname.equalsIgnoreCase(field.getName())) {
String methodName = createMethodName(field);
if (ctype == Types.INTEGER) {
method = clazz.getMethod(methodName, Integer.class);
method.invoke(obj, rs.getInt(i));
} else {
method = clazz.getMethod(methodName, String.class);
method.invoke(obj, rs.getString(i));
}
}
}
}
return obj;
}
use of java.sql.ResultSetMetaData in project Axe by DongyuCai.
the class DataBaseHelper method queryPrimitive.
/**
* 执行返回结果是基本类型的查询
* @throws SQLException
*/
@SuppressWarnings("unchecked")
public static <T> T queryPrimitive(String sql, Object[] params, Class<?>[] paramTypes, String dataSourceName) throws SQLException {
T result = null;
Connection conn = getConnection(dataSourceName);
try {
PreparedStatement ps = getPrepareStatement(conn, sql, params, paramTypes, false);
ResultSet table = ps.executeQuery();
if (table.next()) {
ResultSetMetaData rsmd = ps.getMetaData();
if (rsmd.getColumnCount() > 0)
;
result = (T) table.getObject(1);
}
table.close();
ps.close();
} catch (SQLException e) {
LOGGER.error("execute queryPrimitive failure", e);
throw new SQLException(e);
} finally {
if (conn.getAutoCommit()) {
closeConnection(dataSourceName);
}
}
return result;
}
Aggregations