FileDocCategorySizeDatePackage
SQLOutputImpl.javaAPI DocJava SE 5 API22355Fri Aug 26 14:57:52 BST 2005javax.sql.rowset.serial

SQLOutputImpl

public class SQLOutputImpl extends Object implements SQLOutput
The output stream for writing the attributes of a custom-mapped user-defined type (UDT) back to the database. The driver uses this interface internally, and its methods are never directly invoked by an application programmer.

When an application calls the method PreparedStatement.setObject, the driver checks to see whether the value to be written is a UDT with a custom mapping. If it is, there will be an entry in a type map containing the Class object for the class that implements SQLData for this UDT. If the value to be written is an instance of SQLData, the driver will create an instance of SQLOutputImpl and pass it to the method SQLData.writeSQL. The method writeSQL in turn calls the appropriate SQLOutputImpl.writeXXX methods to write data from the SQLData object to the SQLOutputImpl output stream as the representation of an SQL user-defined type.

Fields Summary
private Vector
attribs
A reference to an existing vector that contains the attributes of a Struct object.
private Map
map
The type map the driver supplies to a newly created SQLOutputImpl object. This type map indicates the SQLData class whose writeSQL method will be called. This method will in turn call the appropriate SQLOutputImpl/code> writer methods.
Constructors Summary
public SQLOutputImpl(Vector attributes, Map map)
Creates a new SQLOutputImpl object initialized with the given vector of attributes and type map. The driver will use the type map to determine which SQLData.writeSQL method to invoke. This method will then call the appropriate SQLOutputImpl writer methods in order and thereby write the attributes to the new output stream.

param
attributes a Vector object containing the attributes of the UDT to be mapped to one or more objects in the Java programming language
param
map a java.util.Map object containing zero or more entries, with each entry consisting of 1) a String giving the fully qualified name of a UDT and 2) the Class object for the SQLData implementation that defines how the UDT is to be mapped
throws
SQLException if the attributes or the map is a null value

        if ((attributes == null) || (map == null)) {
            throw new SQLException("Cannot instantiate a SQLOutputImpl " +
            "instance with null parameters");
        }        
        this.attribs = attributes; 
        this.map = map;
    
Methods Summary
public voidwriteArray(java.sql.Array x)
Writes an Array object in the Java programming language to this SQLOutputImpl object. The driver converts this value to a serializable SerialArray SQL ARRAY value before returning it to the database.

param
x an object representing an SQL ARRAY value
throws
SQLException if the SQLOutputImpl object is in use by a SQLData object attempting to write the attribute values of a UDT to the database.

        if (x == null) {
            attribs.add(x);
            return;
        }
        attribs.add(new SerialArray(x, map));
    
public voidwriteAsciiStream(java.io.InputStream x)
Writes a stream of ASCII characters to this SQLOutputImpl object. The driver will do any necessary conversion from ASCII to the database CHAR format.

param
x the value to pass to the database
throws
SQLException if the SQLOutputImpl object is in use by a SQLData object attempting to write the attribute values of a UDT to the database.

         BufferedReader bufReader = new BufferedReader(new InputStreamReader(x));
         try {
               int i;
               while( (i=bufReader.read()) != -1 ) {
                char ch = (char)i;
               
		StringBuffer strBuf = new StringBuffer();
		strBuf.append(ch);
		
		String str = new String(strBuf);                
                String strLine = bufReader.readLine();
                
                writeString(str.concat(strLine));
            }
          }catch(IOException ioe) {
            throw new SQLException(ioe.getMessage());
        }
    
public voidwriteBigDecimal(java.math.BigDecimal x)
Writes a java.math.BigDecimal object in the Java programming language to this SQLOutputImpl object. The driver converts it to an SQL NUMERIC before returning it to the database.

param
x the value to pass to the database
throws
SQLException if the SQLOutputImpl object is in use by a SQLData object attempting to write the attribute values of a UDT to the database.

        attribs.add(x);
    
public voidwriteBinaryStream(java.io.InputStream x)
Writes a stream of uninterpreted bytes to this SQLOutputImpl object.

param
x the value to pass to the database
throws
SQLException if the SQLOutputImpl object is in use by a SQLData object attempting to write the attribute values of a UDT to the database.

         BufferedReader bufReader = new BufferedReader(new InputStreamReader(x));
         try {
               int i;
             while( (i=bufReader.read()) != -1 ) {
                char ch = (char)i;
               
		StringBuffer strBuf = new StringBuffer();
		strBuf.append(ch);
		
		String str = new String(strBuf);                
                String strLine = bufReader.readLine();
                
                writeString(str.concat(strLine));
             }
        } catch(IOException ioe) {
            throw new SQLException(ioe.getMessage());
        }
    
public voidwriteBlob(java.sql.Blob x)
Writes a Blob object in the Java programming language to this SQLOutputImpl object. The driver converts it to a serializable SerialBlob SQL BLOB value before returning it to the database.

param
x an object representing an SQL BLOB value
throws
SQLException if the SQLOutputImpl object is in use by a SQLData object attempting to write the attribute values of a UDT to the database.

        if (x == null) {
            attribs.add(x);
            return;
        }
        attribs.add(new SerialBlob(x));
    
public voidwriteBoolean(boolean x)
Writes a boolean in the Java programming language to this SQLOutputImpl object. The driver converts it to an SQL BIT before returning it to the database.

param
x the value to pass to the database
throws
SQLException if the SQLOutputImpl object is in use by a SQLData object attempting to write the attribute values of a UDT to the database.

        attribs.add(new Boolean(x));
    
public voidwriteByte(byte x)
Writes a byte in the Java programming language to this SQLOutputImpl object. The driver converts it to an SQL BIT before returning it to the database.

param
x the value to pass to the database
throws
SQLException if the SQLOutputImpl object is in use by a SQLData object attempting to write the attribute values of a UDT to the database.

        attribs.add(new Byte(x));
    
public voidwriteBytes(byte[] x)
Writes an array of bytes in the Java programming language to this SQLOutputImpl object. The driver converts it to an SQL VARBINARY or LONGVARBINARY before returning it to the database.

param
x the value to pass to the database
throws
SQLException if the SQLOutputImpl object is in use by a SQLData object attempting to write the attribute values of a UDT to the database.

        attribs.add(x);
    
public voidwriteCharacterStream(java.io.Reader x)
Writes a stream of Unicode characters to this SQLOutputImpl object. The driver will do any necessary conversion from Unicode to the database CHAR format.

param
x the value to pass to the database
throws
SQLException if the SQLOutputImpl object is in use by a SQLData object attempting to write the attribute values of a UDT to the database.

         BufferedReader bufReader = new BufferedReader(x);
         try {
             int i;
             while( (i = bufReader.read()) != -1 ) {
                char ch = (char)i;
		StringBuffer strBuf = new StringBuffer();
		strBuf.append(ch);
		
		String str = new String(strBuf);                
                String strLine = bufReader.readLine();
                
                writeString(str.concat(strLine));
             }   
         } catch(IOException ioe) {
         
         }
    
public voidwriteClob(java.sql.Clob x)
Writes a Clob object in the Java programming language to this SQLOutputImpl object. The driver converts it to a serializable SerialClob SQL CLOB value before returning it to the database.

param
x an object representing an SQL CLOB value
throws
SQLException if the SQLOutputImpl object is in use by a SQLData object attempting to write the attribute values of a UDT to the database.

        if (x == null) {
            attribs.add(x);
            return;
        }
        attribs.add(new SerialClob(x));
    
public voidwriteDate(java.sql.Date x)
Writes a java.sql.Date object in the Java programming language to this SQLOutputImpl object. The driver converts it to an SQL DATE before returning it to the database.

param
x the value to pass to the database
throws
SQLException if the SQLOutputImpl object is in use by a SQLData object attempting to write the attribute values of a UDT to the database.

        attribs.add(x);
    
public voidwriteDouble(double x)
Writes a double in the Java programming language to this SQLOutputImpl object. The driver converts it to an SQL DOUBLE before returning it to the database.

param
x the value to pass to the database
throws
SQLException if the SQLOutputImpl object is in use by a SQLData object attempting to write the attribute values of a UDT to the database.

        attribs.add(new Double(x));
    
public voidwriteFloat(float x)
Writes a float in the Java programming language to this SQLOutputImpl object. The driver converts it to an SQL REAL before returning it to the database.

param
x the value to pass to the database
throws
SQLException if the SQLOutputImpl object is in use by a SQLData object attempting to write the attribute values of a UDT to the database.

        attribs.add(new Float(x));
    
public voidwriteInt(int x)
Writes an int in the Java programming language to this SQLOutputImpl object. The driver converts it to an SQL INTEGER before returning it to the database.

param
x the value to pass to the database
throws
SQLException if the SQLOutputImpl object is in use by a SQLData object attempting to write the attribute values of a UDT to the database.

        attribs.add(new Integer(x));
    
public voidwriteLong(long x)
Writes a long in the Java programming language to this SQLOutputImpl object. The driver converts it to an SQL BIGINT before returning it to the database.

param
x the value to pass to the database
throws
SQLException if the SQLOutputImpl object is in use by a SQLData object attempting to write the attribute values of a UDT to the database.

        attribs.add(new Long(x));
    
public voidwriteObject(java.sql.SQLData x)
Writes to the stream the data contained in the given SQLData object. When the SQLData object is null, this method writes an SQL NULL to the stream. Otherwise, it calls the SQLData.writeSQL method of the given object, which writes the object's attributes to the stream.

The implementation of the method SQLData.writeSQ calls the appropriate SQLOutputImpl.writeXXX method(s) for writing each of the object's attributes in order. The attributes must be read from an SQLInput input stream and written to an SQLOutputImpl output stream in the same order in which they were listed in the SQL definition of the user-defined type.

param
x the object representing data of an SQL structured or distinct type
throws
SQLException if the SQLOutputImpl object is in use by a SQLData object attempting to write the attribute values of a UDT to the database.

        
        /*
         * Except for the types that are passed as objects
         * this seems to be the only way for an object to
         * get a null value for a field in a structure.
         *
         * Note: this means that the class defining SQLData 
         * will need to track if a field is SQL null for itself
         */
        if (x == null) {
            attribs.add(x);
            return;
        }
        
        /* 
         * We have to write out a SerialStruct that contains
         * the name of this class otherwise we don't know
         * what to re-instantiate during readSQL()
         */
        attribs.add(new SerialStruct((SQLData)x, map));
    
public voidwriteRef(java.sql.Ref x)
Writes a Ref object in the Java programming language to this SQLOutputImpl object. The driver converts it to a serializable SerialRef SQL REF value before returning it to the database.

param
x an object representing an SQL REF value
throws
SQLException if the SQLOutputImpl object is in use by a SQLData object attempting to write the attribute values of a UDT to the database.

        if (x == null) {
            attribs.add(x);
            return;
        }
        attribs.add(new SerialRef(x));
    
public voidwriteShort(short x)
Writes a short in the Java programming language to this SQLOutputImpl object. The driver converts it to an SQL SMALLINT before returning it to the database.

param
x the value to pass to the database
throws
SQLException if the SQLOutputImpl object is in use by a SQLData object attempting to write the attribute values of a UDT to the database.

        attribs.add(new Short(x));
    
public voidwriteString(java.lang.String x)
Writes a String in the Java programming language to this SQLOutputImpl object. The driver converts it to an SQL CHAR, VARCHAR, or LONGVARCHAR before returning it to the database.

param
x the value to pass to the database
throws
SQLException if the SQLOutputImpl object is in use by a SQLData object attempting to write the attribute values of a UDT to the database.

        //System.out.println("Adding :"+x);
        attribs.add(x);
    
public voidwriteStruct(java.sql.Struct x)
Writes a Struct object in the Java programming language to this SQLOutputImpl object. The driver converts this value to an SQL structured type before returning it to the database.

This method should be used when an SQL structured type has been mapped to a Struct object in the Java programming language (the standard mapping). The method writeObject should be used if an SQL structured type has been custom mapped to a class in the Java programming language.

param
x an object representing the attributes of an SQL structured type
throws
SQLException if the SQLOutputImpl object is in use by a SQLData object attempting to write the attribute values of a UDT to the database.

        SerialStruct s = new SerialStruct(x,map);;
        attribs.add(s);        
    
public voidwriteTime(java.sql.Time x)
Writes a java.sql.Time object in the Java programming language to this SQLOutputImpl object. The driver converts it to an SQL TIME before returning it to the database.

param
x the value to pass to the database
throws
SQLException if the SQLOutputImpl object is in use by a SQLData object attempting to write the attribute values of a UDT to the database.

        attribs.add(x);
    
public voidwriteTimestamp(java.sql.Timestamp x)
Writes a java.sql.Timestamp object in the Java programming language to this SQLOutputImpl object. The driver converts it to an SQL TIMESTAMP before returning it to the database.

param
x the value to pass to the database
throws
SQLException if the SQLOutputImpl object is in use by a SQLData object attempting to write the attribute values of a UDT to the database.

        attribs.add(x);
    
public voidwriteURL(java.net.URL url)
Writes an java.sql.Type.DATALINK object in the Java programming language to this SQLOutputImpl object. The driver converts this value to a serializable SerialDatalink SQL DATALINK value before return it to the database.

param
url an object representing a SQL DATALINK value
throws
SQLException if the SQLOutputImpl object is in use by a SQLData object attempting to write the attribute values of a UDT to the database.

        if (url == null) {
            attribs.add(url);
            return;
        }
        attribs.add(new SerialDatalink(url));