Search in sources :

Example 71 with ResultSetMetaData

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);
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData)

Example 72 with ResultSetMetaData

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);
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) HashMap(java.util.HashMap)

Example 73 with ResultSetMetaData

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;
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) SQLException(java.sql.SQLException)

Example 74 with ResultSetMetaData

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;
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) Field(java.lang.reflect.Field) Method(java.lang.reflect.Method)

Example 75 with ResultSetMetaData

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;
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Aggregations

ResultSetMetaData (java.sql.ResultSetMetaData)449 ResultSet (java.sql.ResultSet)279 SQLException (java.sql.SQLException)125 Statement (java.sql.Statement)123 Test (org.junit.Test)109 PreparedStatement (java.sql.PreparedStatement)107 ArrayList (java.util.ArrayList)78 Connection (java.sql.Connection)68 HashMap (java.util.HashMap)44 DatabaseMetaData (java.sql.DatabaseMetaData)41 Map (java.util.Map)31 LinkedHashMap (java.util.LinkedHashMap)22 List (java.util.List)18 IOException (java.io.IOException)15 BigDecimal (java.math.BigDecimal)12 HashSet (java.util.HashSet)12 ParameterMetaData (java.sql.ParameterMetaData)10 Properties (java.util.Properties)10 KnownFailure (dalvik.annotation.KnownFailure)9 Field (java.lang.reflect.Field)9