alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (SQLInputImpl.java)

This example Java source code file (SQLInputImpl.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

blob, boolean, double, integer, jdbc, long, map, nclob, object, rowid, short, sql, sqlexception, sqlinputimpl, string, struct, util

The SQLInputImpl.java Java example source code

/*
 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code 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 General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
package javax.sql.rowset.serial;

import java.sql.*;
import java.util.Arrays;
import java.util.Map;

/**
 * An input stream used for custom mapping user-defined types (UDTs).
 * An <code>SQLInputImpl object is an input stream that contains a
 * stream of values that are the attributes of a UDT.
 * <p>
 * This class is used by the driver behind the scenes when the method
 * <code>getObject is called on an SQL structured or distinct type
 * that has a custom mapping; a programmer never invokes
 * <code>SQLInputImpl methods directly. They are provided here as a
 * convenience for those who write <code>RowSet implementations.
 * <P>
 * The <code>SQLInputImpl class provides a set of
 * reader methods analogous to the <code>ResultSet getter
 * methods.  These methods make it possible to read the values in an
 * <code>SQLInputImpl object.
 * <P>
 * The method <code>wasNull is used to determine whether the
 * the last value read was SQL <code>NULL.
 * <P>When the method getObject is called with an
 * object of a class implementing the interface <code>SQLData,
 * the JDBC driver calls the method <code>SQLData.getSQLType
 * to determine the SQL type of the UDT being custom mapped. The driver
 * creates an instance of <code>SQLInputImpl, populating it with the
 * attributes of the UDT.  The driver then passes the input
 * stream to the method <code>SQLData.readSQL, which in turn
 * calls the <code>SQLInputImpl reader methods
 * to read the attributes from the input stream.
 * @since 1.5
 * @see java.sql.SQLData
 */
public class SQLInputImpl implements SQLInput {

    /**
     * <code>true if the last value returned was SQL NULL;
     * <code>false otherwise.
     */
    private boolean lastValueWasNull;

    /**
     * The current index into the array of SQL structured type attributes
     * that will be read from this <code>SQLInputImpl object and
     * mapped to the fields of a class in the Java programming language.
     */
    private int idx;

    /**
     * The array of attributes to be read from this stream.  The order
     * of the attributes is the same as the order in which they were
     * listed in the SQL definition of the UDT.
     */
    private Object attrib[];

    /**
     * The type map to use when the method <code>readObject
     * is invoked. This is a <code>java.util.Map object in which
     * there may be zero or more entries.  Each entry consists of the
     * fully qualified name of a UDT (the value to be mapped) and the
     * <code>Class object for a class that implements
     * <code>SQLData (the Java class that defines how the UDT
     * will be mapped).
     */
    private Map<String,Class map;


    /**
     * Creates an <code>SQLInputImpl object initialized with the
     * given array of attributes and the given type map. If any of the
     * attributes is a UDT whose name is in an entry in the type map,
     * the attribute will be mapped according to the corresponding
     * <code>SQLData implementation.
     *
     * @param attributes an array of <code>Object instances in which
     *        each element is an attribute of a UDT. The order of the
     *        attributes in the array is the same order in which
     *        the attributes were defined in the UDT definition.
     * @param map a <code>java.util.Map object containing zero or more
     *        entries, with each entry consisting of 1) a <code>String
     *        giving the fully
     *        qualified name of the UDT and 2) the <code>Class object
     *        for the <code>SQLData implementation that defines how
     *        the UDT is to be mapped
     * @throws SQLException if the <code>attributes or the map
     *        is a <code>null value
     */

    public SQLInputImpl(Object[] attributes, Map<String,Class map)
        throws SQLException
    {
        if ((attributes == null) || (map == null)) {
            throw new SQLException("Cannot instantiate a SQLInputImpl " +
            "object with null parameters");
        }
        // assign our local reference to the attribute stream
        attrib = Arrays.copyOf(attributes, attributes.length);
        // init the index point before the head of the stream
        idx = -1;
        // set the map
        this.map = map;
    }


    /**
     * Retrieves the next attribute in this <code>SQLInputImpl object
     * as an <code>Object in the Java programming language.
     *
     * @return the next value in the input stream
     *         as an <code>Object in the Java programming language
     * @throws SQLException if the read position is located at an invalid
     *         position or if there are no further values in the stream
     */
    private Object getNextAttribute() throws SQLException {
        if (++idx >= attrib.length) {
            throw new SQLException("SQLInputImpl exception: Invalid read " +
                                   "position");
        } else {
            lastValueWasNull = attrib[idx] == null;
            return attrib[idx];
        }
    }


    //================================================================
    // Methods for reading attributes from the stream of SQL data.
    // These methods correspond to the column-accessor methods of
    // java.sql.ResultSet.
    //================================================================

    /**
     * Retrieves the next attribute in this <code>SQLInputImpl object as
     * a <code>String in the Java programming language.
     * <p>
     * This method does not perform type-safe checking to determine if the
     * returned type is the expected type; this responsibility is delegated
     * to the UDT mapping as defined by a <code>SQLData
     * implementation.
     * <p>
     * @return the next attribute in this <code>SQLInputImpl object;
     *     if the value is <code>SQL NULL, return null
     * @throws SQLException if the read position is located at an invalid
     *     position or if there are no further values in the stream.
     */
    public String readString() throws SQLException {
        return  (String)getNextAttribute();
    }

    /**
     * Retrieves the next attribute in this <code>SQLInputImpl object as
     * a <code>boolean in the Java programming language.
     * <p>
     * This method does not perform type-safe checking to determine if the
     * returned type is the expected type; this responsibility is delegated
     * to the UDT mapping as defined by a <code>SQLData
     * implementation.
     * <p>
     * @return the next attribute in this <code>SQLInputImpl object;
     *     if the value is <code>SQL NULL, return null
     * @throws SQLException if the read position is located at an invalid
     *     position or if there are no further values in the stream.
     */
    public boolean readBoolean() throws SQLException {
        Boolean attrib = (Boolean)getNextAttribute();
        return  (attrib == null) ? false : attrib.booleanValue();
    }

    /**
     * Retrieves the next attribute in this <code>SQLInputImpl object as
     * a <code>byte in the Java programming language.
     * <p>
     * This method does not perform type-safe checking to determine if the
     * returned type is the expected type; this responsibility is delegated
     * to the UDT mapping as defined by a <code>SQLData
     * implementation.
     * <p>
     * @return the next attribute in this <code>SQLInputImpl object;
     *     if the value is <code>SQL NULL, return null
     * @throws SQLException if the read position is located at an invalid
     *     position or if there are no further values in the stream
     */
    public byte readByte() throws SQLException {
        Byte attrib = (Byte)getNextAttribute();
        return  (attrib == null) ? 0 : attrib.byteValue();
    }

    /**
     * Retrieves the next attribute in this <code>SQLInputImpl object
     * as a <code>short in the Java programming language.
     * <P>
     * This method does not perform type-safe checking to determine if the
     * returned type is the expected type; this responsibility is delegated
     * to the UDT mapping as defined by a <code>SQLData implementation.
     * <P>
     * @return the next attribute in this <code>SQLInputImpl object;
     *       if the value is <code>SQL NULL, return null
     * @throws SQLException if the read position is located at an invalid
     *       position or if there are no more values in the stream
     */
    public short readShort() throws SQLException {
        Short attrib = (Short)getNextAttribute();
        return (attrib == null) ? 0 : attrib.shortValue();
    }

    /**
     * Retrieves the next attribute in this <code>SQLInputImpl object
     * as an <code>int in the Java programming language.
     * <P>
     * This method does not perform type-safe checking to determine if the
     * returned type is the expected type; this responsibility is delegated
     * to the UDT mapping as defined by a <code>SQLData implementation.
     * <P>
     * @return the next attribute in this <code>SQLInputImpl object;
     *       if the value is <code>SQL NULL, return null
     * @throws SQLException if the read position is located at an invalid
     *       position or if there are no more values in the stream
     */
    public int readInt() throws SQLException {
        Integer attrib = (Integer)getNextAttribute();
        return (attrib == null) ? 0 : attrib.intValue();
    }

    /**
     * Retrieves the next attribute in this <code>SQLInputImpl object
     * as a <code>long in the Java programming language.
     * <P>
     * This method does not perform type-safe checking to determine if the
     * returned type is the expected type; this responsibility is delegated
     * to the UDT mapping as defined by a <code>SQLData implementation.
     * <P>
     * @return the next attribute in this <code>SQLInputImpl object;
     *       if the value is <code>SQL NULL, return null
     * @throws SQLException if the read position is located at an invalid
     *       position or if there are no more values in the stream
     */
    public long readLong() throws SQLException {
        Long attrib = (Long)getNextAttribute();
        return (attrib == null) ? 0 : attrib.longValue();
    }

    /**
     * Retrieves the next attribute in this <code>SQLInputImpl object
     * as a <code>float in the Java programming language.
     * <P>
     * This method does not perform type-safe checking to determine if the
     * returned type is the expected type; this responsibility is delegated
     * to the UDT mapping as defined by a <code>SQLData implementation.
     * <P>
     * @return the next attribute in this <code>SQLInputImpl object;
     *       if the value is <code>SQL NULL, return null
     * @throws SQLException if the read position is located at an invalid
     *       position or if there are no more values in the stream
     */
    public float readFloat() throws SQLException {
        Float attrib = (Float)getNextAttribute();
        return (attrib == null) ? 0 : attrib.floatValue();
    }

    /**
     * Retrieves the next attribute in this <code>SQLInputImpl object
     * as a <code>double in the Java programming language.
     * <P>
     * This method does not perform type-safe checking to determine if the
     * returned type is the expected type; this responsibility is delegated
     * to the UDT mapping as defined by a <code>SQLData implementation.
     * <P>
     * @return the next attribute in this <code>SQLInputImpl object;
     *       if the value is <code>SQL NULL, return null
     * @throws SQLException if the read position is located at an invalid
     *       position or if there are no more values in the stream
     */
    public double readDouble() throws SQLException {
        Double attrib = (Double)getNextAttribute();
        return (attrib == null)  ? 0 :  attrib.doubleValue();
    }

    /**
     * Retrieves the next attribute in this <code>SQLInputImpl object
     * as a <code>java.math.BigDecimal.
     * <P>
     * This method does not perform type-safe checking to determine if the
     * returned type is the expected type; this responsibility is delegated
     * to the UDT mapping as defined by a <code>SQLData implementation.
     * <P>
     * @return the next attribute in this <code>SQLInputImpl object;
     *       if the value is <code>SQL NULL, return null
     * @throws SQLException if the read position is located at an invalid
     *       position or if there are no more values in the stream
     */
    public java.math.BigDecimal readBigDecimal() throws SQLException {
        return (java.math.BigDecimal)getNextAttribute();
    }

    /**
     * Retrieves the next attribute in this <code>SQLInputImpl object
     * as an array of bytes.
     * <p>
     * This method does not perform type-safe checking to determine if the
     * returned type is the expected type; this responsibility is delegated
     * to the UDT mapping as defined by a <code>SQLData implementation.
     * <P>
     * @return the next attribute in this <code>SQLInputImpl object;
     *       if the value is <code>SQL NULL, return null
     * @throws SQLException if the read position is located at an invalid
     *       position or if there are no more values in the stream
     */
    public byte[] readBytes() throws SQLException {
        return (byte[])getNextAttribute();
    }

    /**
     * Retrieves the next attribute in this <code>SQLInputImpl as
     * a <code>java.sql.Date object.
     * <P>
     * This method does not perform type-safe checking to determine if the
     * returned type is the expected type; this responsibility is delegated
     * to the UDT mapping as defined by a <code>SQLData implementation.
     * <P>
     * @return the next attribute in this <code>SQLInputImpl object;
     *       if the value is <code>SQL NULL, return null
     * @throws SQLException if the read position is located at an invalid
     *       position or if there are no more values in the stream
     */
    public java.sql.Date readDate() throws SQLException {
        return (java.sql.Date)getNextAttribute();
    }

    /**
     * Retrieves the next attribute in this <code>SQLInputImpl object as
     * a <code>java.sql.Time object.
     * <P>
     * This method does not perform type-safe checking to determine if the
     * returned type is the expected type as this responsibility is delegated
     * to the UDT mapping as implemented by a <code>SQLData
     * implementation.
     *
     * @return the attribute; if the value is <code>SQL NULL, return
     * <code>null
     * @throws SQLException if the read position is located at an invalid
     * position; or if there are no further values in the stream.
     */
    public java.sql.Time readTime() throws SQLException {
        return (java.sql.Time)getNextAttribute();
    }

    /**
     * Retrieves the next attribute in this <code>SQLInputImpl object as
     * a <code>java.sql.Timestamp object.
     *
     * @return the attribute; if the value is <code>SQL NULL, return
     * <code>null
     * @throws SQLException if the read position is located at an invalid
     * position; or if there are no further values in the stream.
     */
    public java.sql.Timestamp readTimestamp() throws SQLException {
        return (java.sql.Timestamp)getNextAttribute();
    }

    /**
     * Retrieves the next attribute in this <code>SQLInputImpl object
     * as a stream of Unicode characters.
     * <P>
     * This method does not perform type-safe checking to determine if the
     * returned type is the expected type as this responsibility is delegated
     * to the UDT mapping as implemented by a <code>SQLData
     * implementation.
     *
     * @return the attribute; if the value is <code>SQL NULL, return null
     * @throws SQLException if the read position is located at an invalid
     * position; or if there are no further values in the stream.
     */
    public java.io.Reader readCharacterStream() throws SQLException {
        return (java.io.Reader)getNextAttribute();
    }

    /**
     * Returns the next attribute in this <code>SQLInputImpl object
     * as a stream of ASCII characters.
     * <P>
     * This method does not perform type-safe checking to determine if the
     * returned type is the expected type as this responsibility is delegated
     * to the UDT mapping as implemented by a <code>SQLData
     * implementation.
     *
     * @return the attribute; if the value is <code>SQL NULL,
     * return <code>null
     * @throws SQLException if the read position is located at an invalid
     * position; or if there are no further values in the stream.
     */
    public java.io.InputStream readAsciiStream() throws SQLException {
        return (java.io.InputStream)getNextAttribute();
    }

    /**
     * Returns the next attribute in this <code>SQLInputImpl object
     * as a stream of uninterpreted bytes.
     * <P>
     * This method does not perform type-safe checking to determine if the
     * returned type is the expected type as this responsibility is delegated
     * to the UDT mapping as implemented by a <code>SQLData
     * implementation.
     *
     * @return the attribute; if the value is <code>SQL NULL, return
     * <code>null
     * @throws SQLException if the read position is located at an invalid
     * position; or if there are no further values in the stream.
     */
    public java.io.InputStream readBinaryStream() throws SQLException {
        return (java.io.InputStream)getNextAttribute();
    }

    //================================================================
    // Methods for reading items of SQL user-defined types from the stream.
    //================================================================

    /**
     * Retrieves the value at the head of this <code>SQLInputImpl
     * object as an <code>Object in the Java programming language.  The
     * actual type of the object returned is determined by the default
     * mapping of SQL types to types in the Java programming language unless
     * there is a custom mapping, in which case the type of the object
     * returned is determined by this stream's type map.
     * <P>
     * The JDBC technology-enabled driver registers a type map with the stream
     * before passing the stream to the application.
     * <P>
     * When the datum at the head of the stream is an SQL <code>NULL,
     * this method returns <code>null.  If the datum is an SQL
     * structured or distinct type with a custom mapping, this method
     * determines the SQL type of the datum at the head of the stream,
     * constructs an object of the appropriate class, and calls the method
     * <code>SQLData.readSQL on that object. The readSQL
     * method then calls the appropriate <code>SQLInputImpl.readXXX
     * methods to retrieve the attribute values from the stream.
     *
     * @return the value at the head of the stream as an <code>Object
     *         in the Java programming language; <code>null if
     *         the value is SQL <code>NULL
     * @throws SQLException if the read position is located at an invalid
     * position; or if there are no further values in the stream.
     */
    public Object readObject() throws SQLException {
        Object attrib = getNextAttribute();
        if (attrib instanceof Struct) {
            Struct s = (Struct)attrib;
            // 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("Unable to instantiate: " +
                            ex.getMessage());
                } catch (java.lang.IllegalAccessException ex) {
                    throw new SQLException("Unable to instantiate: " +
                            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 obj;
            }
        }
        return attrib;
    }

    /**
     * Retrieves the value at the head of this <code>SQLInputImpl object
     * as a <code>Ref object in the Java programming language.
     *
     * @return a <code>Ref object representing the SQL
     *         <code>REF value at the head of the stream; if the value
     *         is <code>SQL NULL return null
     * @throws SQLException if the read position is located at an invalid
     *         position; or if there are no further values in the stream.
     */
    public Ref readRef() throws SQLException {
        return (Ref)getNextAttribute();
    }

    /**
     * Retrieves the <code>BLOB value at the head of this
     * <code>SQLInputImpl object as a Blob object
     * in the Java programming language.
     * <P>
     * This method does not perform type-safe checking to determine if the
     * returned type is the expected type as this responsibility is delegated
     * to the UDT mapping as implemented by a <code>SQLData
     * implementation.
     *
     * @return a <code>Blob object representing the SQL
     *         <code>BLOB value at the head of this stream;
     *         if the value is <code>SQL NULL, return
     *         <code>null
     * @throws SQLException if the read position is located at an invalid
     * position; or if there are no further values in the stream.
     */
    public Blob readBlob() throws SQLException {
        return (Blob)getNextAttribute();
    }

    /**
     * Retrieves the <code>CLOB value at the head of this
     * <code>SQLInputImpl object as a Clob object
     * in the Java programming language.
     * <P>
     * This method does not perform type-safe checking to determine if the
     * returned type is the expected type as this responsibility is delegated
     * to the UDT mapping as implemented by a <code>SQLData
     * implementation.
     *
     * @return a <code>Clob object representing the SQL
     *         <code>CLOB value at the head of the stream;
     *         if the value is <code>SQL NULL, return
     *         <code>null
     * @throws SQLException if the read position is located at an invalid
     * position; or if there are no further values in the stream.
     */
    public Clob readClob() throws SQLException {
        return (Clob)getNextAttribute();
    }

    /**
     * Reads an SQL <code>ARRAY value from the stream and
     * returns it as an <code>Array object in the Java programming
     * language.
     * <P>
     * This method does not perform type-safe checking to determine if the
     * returned type is the expected type as this responsibility is delegated
     * to the UDT mapping as implemented by a <code>SQLData
     * implementation.
     *
     * @return an <code>Array object representing the SQL
     *         <code>ARRAY value at the head of the stream; *
     *         if the value is <code>SQL NULL, return
     *         <code>null
     * @throws SQLException if the read position is located at an invalid
     * position; or if there are no further values in the stream.

     */
    public Array readArray() throws SQLException {
        return (Array)getNextAttribute();
    }

    /**
     * Ascertains whether the last value read from this
     * <code>SQLInputImpl object was null.
     *
     * @return <code>true if the SQL value read most recently was
     *         <code>null; otherwise, false; by default it
     *         will return false
     * @throws SQLException if an error occurs determining the last value
     *         read was a <code>null value or not;
     */
    public boolean wasNull() throws SQLException {
        return lastValueWasNull;
    }

    /**
     * Reads an SQL <code>DATALINK value from the stream and
     * returns it as an <code>URL object in the Java programming
     * language.
     * <P>
     * This method does not perform type-safe checking to determine if the
     * returned type is the expected type as this responsibility is delegated
     * to the UDT mapping as implemented by a <code>SQLData
     * implementation.
     *
     * @return an <code>URL object representing the SQL
     *         <code>DATALINK value at the head of the stream; *
     *         if the value is <code>SQL NULL, return
     *         <code>null
     * @throws SQLException if the read position is located at an invalid
     * position; or if there are no further values in the stream.
     */
    public java.net.URL readURL() throws SQLException {
        return (java.net.URL)getNextAttribute();
    }

    //---------------------------- JDBC 4.0 -------------------------

    /**
     * Reads an SQL <code>NCLOB value from the stream and returns it as a
     * <code>Clob object in the Java programming language.
     *
     * @return a <code>NClob object representing data of the SQL NCLOB value
     * at the head of the stream; <code>null if the value read is
     * SQL <code>NULL
     * @exception SQLException if a database access error occurs
     * @since 1.6
     */
     public NClob readNClob() throws SQLException {
        return (NClob)getNextAttribute();
     }

    /**
     * Reads the next attribute in the stream and returns it as a <code>String
     * in the Java programming language. It is intended for use when
     * accessing  <code>NCHAR,NVARCHAR
     * and <code>LONGNVARCHAR columns.
     *
     * @return the attribute; if the value is SQL <code>NULL, returns null
     * @exception SQLException if a database access error occurs
     * @since 1.6
     */
    public String readNString() throws SQLException {
        return (String)getNextAttribute();
    }

    /**
     * Reads an SQL <code>XML value from the stream and returns it as a
     * <code>SQLXML object in the Java programming language.
     *
     * @return a <code>SQLXML object representing data of the SQL XML value
     * at the head of the stream; <code>null if the value read is
     * SQL <code>NULL
     * @exception SQLException if a database access error occurs
     * @since 1.6
     */
    public SQLXML readSQLXML() throws SQLException {
        return (SQLXML)getNextAttribute();
    }

    /**
     * Reads an SQL <code>ROWID value from the stream and returns it as a
     * <code>RowId object in the Java programming language.
     *
     * @return a <code>RowId object representing data of the SQL ROWID value
     * at the head of the stream; <code>null if the value read is
     * SQL <code>NULL
     * @exception SQLException if a database access error occurs
     * @since 1.6
     */
    public RowId readRowId() throws SQLException {
        return  (RowId)getNextAttribute();
    }


}

Other Java examples (source code examples)

Here is a short list of links related to this Java SQLInputImpl.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.