Example usage for java.sql Struct getAttributes

List of usage examples for java.sql Struct getAttributes

Introduction

In this page you can find the example usage for java.sql Struct getAttributes.

Prototype

Object[] getAttributes(java.util.Map<String, Class<?>> map) throws SQLException;

Source Link

Document

Produces the ordered values of the attributes of the SQL structured type that this Struct object represents.

Usage

From source file:lasige.steeldb.jdbc.BFTRowSet.java

/**
 * Retrieves the value of the designated column in this
 * <code>CachedRowSetImpl</code> object as an <code>Object</code> in
 * the Java programming language, using the given
 * <code>java.util.Map</code> object to custom map the value if
 * appropriate./*from w  ww .j av  a 2 s  . c  om*/
 *
 * @param columnIndex the first column is <code>1</code>, the second
 *        is <code>2</code>, and so on; must be <code>1</code> or larger
 *        and equal to or less than the number of columns in this rowset
 * @param map a <code>java.util.Map</code> object showing the mapping
 *            from SQL type names to classes in the Java programming
 *            language
 * @return an <code>Object</code> representing the SQL value
 * @throws SQLException if the given column index is out of bounds or
 *            the cursor is not on one of this rowset's rows or its
 *            insert row
 */
public Object getObject(int columnIndex, java.util.Map<String, Class<?>> map) throws SQLException {
    Object value;

    // sanity check.
    checkIndex(columnIndex);
    // make sure the cursor is on a valid row
    checkCursor();

    setLastValueNull(false);
    value = getCurrentRow().getColumnObject(columnIndex);

    // check for SQL NULL
    if (value == null) {
        setLastValueNull(true);
        return null;
    }
    if (value instanceof Struct) {
        Struct s = (Struct) value;

        // look up the class in the map
        Class<?> c = map.get(s.getSQLTypeName());
        if (c != null) {
            // create new instance of the class
            SQLData obj = null;
            try {
                obj = (SQLData) c.newInstance();
            } catch (java.lang.InstantiationException ex) {
                throw new SQLException(MessageFormat.format(
                        resBundle.handleGetObject("cachedrowsetimpl.unableins").toString(), ex.getMessage()));
            } catch (java.lang.IllegalAccessException ex) {
                throw new SQLException(MessageFormat.format(
                        resBundle.handleGetObject("cachedrowsetimpl.unableins").toString(), ex.getMessage()));
            }
            // get the attributes from the struct
            Object attribs[] = s.getAttributes(map);
            // create the SQLInput "stream"
            SQLInputImpl sqlInput = new SQLInputImpl(attribs, map);
            // read the values...
            obj.readSQL(sqlInput, s.getSQLTypeName());
            return (Object) obj;
        }
    }
    return value;
}

From source file:lasige.steeldb.jdbc.BFTRowSet.java

/**
 * Retrieves the value of the designated column in the current row
 * of this <code>CachedRowSetImpl</code> object as an
 * <code>Object</code> value.
 * <P>/*w  w w  . j ava2 s .  com*/
 * The type of the <code>Object</code> will be the default
 * Java object type corresponding to the column's SQL type,
 * following the mapping for built-in types specified in the JDBC 3.0
 * specification.
 * <P>
 * This method may also be used to read datatabase-specific
 * abstract data types.
 * <P>
 * This implementation of the method <code>getObject</code> extends its
 * behavior so that it gets the attributes of an SQL structured type
 * as an array of <code>Object</code> values.  This method also custom
 * maps SQL user-defined types to classes in the Java programming language.
 * When the specified column contains
 * a structured or distinct value, the behavior of this method is as
 * if it were a call to the method <code>getObject(columnIndex,
 * this.getStatement().getConnection().getTypeMap())</code>.
 *
 * @param columnIndex the first column is <code>1</code>, the second
 *        is <code>2</code>, and so on; must be <code>1</code> or larger
 *        and equal to or less than the number of columns in the rowset
 * @return a <code>java.lang.Object</code> holding the column value;
 *         if the value is SQL <code>NULL</code>, the result is <code>null</code>
 * @throws SQLException if the given column index is out of bounds,
 *            the cursor is not on a valid row, or there is a problem getting
 *            the <code>Class</code> object for a custom mapping
 * @see #getObject(String)
 */
public Object getObject(int columnIndex) throws SQLException {
    Object value;
    java.util.Map<String, Class<?>> map;

    // sanity check.
    checkIndex(columnIndex);
    // make sure the cursor is on a valid row
    checkCursor();

    setLastValueNull(false);
    value = getCurrentRow().getColumnObject(columnIndex);

    // check for SQL NULL
    if (value == null) {
        setLastValueNull(true);
        return null;
    }
    if (value instanceof Struct) {
        Struct s = (Struct) value;
        map = getTypeMap();
        // look up the class in the map
        Class<?> c = map.get(s.getSQLTypeName());
        if (c != null) {
            // create new instance of the class
            SQLData obj = null;
            try {
                obj = (SQLData) c.newInstance();
            } catch (java.lang.InstantiationException ex) {
                throw new SQLException(MessageFormat.format(
                        resBundle.handleGetObject("cachedrowsetimpl.unableins").toString(), ex.getMessage()));
            } catch (java.lang.IllegalAccessException ex) {
                throw new SQLException(MessageFormat.format(
                        resBundle.handleGetObject("cachedrowsetimpl.unableins").toString(), ex.getMessage()));
            }
            // get the attributes from the struct
            Object attribs[] = s.getAttributes(map);
            // create the SQLInput "stream"
            SQLInputImpl sqlInput = new SQLInputImpl(attribs, map);
            // read the values...
            obj.readSQL(sqlInput, s.getSQLTypeName());
            return (Object) obj;
        }
    }
    return value;
}