Re: getting data from 'image' field

From: Andrea Spinelli (aspinelli_at_no-spam-please-imteam.it)
Date: 07/27/04

  • Next message: Andrea Spinelli: "Re: clobs and blobs"
    Date: Tue, 27 Jul 2004 18:58:19 +0000 (UTC)
    
    

    "Soeren Degn Jahns" <nospam@nospam.dk> wrote in news:XF4Mc.27029
    $Vf.1471448@news000.worldonline.dk:

    >> how can i retrieve data from a image-field in a mssql database? i
    >> tried using 'ResultSet.getBlob(columnName)', but this throws an
    >> exception ("java.sql.SQLException: [Microsoft][SQLServer 2000 Driver
    >> for JDBC]Unsupported data conversion.")...
    >
    > In oracle I would use a "LONG RAW" to store such data in. In a MS SQL
    > Server I would think that you should use a BINARY or A VAR BINARY
    field.
    > However, then instead of using getBlob(...) you should use getObject
    (...)
    > that
    > will give you a byte array.
    >
    > // Soeren
    >
    >

    Excerpt from a working program:

    ResultSet rs = ...

                            logger.debug( "Column #" + i + " has name " + name
                                    + " and type Memo" );
    // works for postgres and sql server, but not for InterBase (!)
    // byte content[] = rs.getBytes( name ) ;
    // reported as M-0000042

                            InputStream is = null;
                            try {
                                    is = rs.getBinaryStream( name );
                            } catch( SQLException sex ){
                                    throw new Error(
                                            getClass().toString() + ".buildResultHash :
    error getting binary stream "
                                            + "for column "
                                            + name
                                            + " (index"
                                            + i
                                            + "). "
                                            + sex.getMessage()
                                    );
                                    
                            }
                            
                            if( is == null ){
                                    continue;
                            }

                            byte buf[] = new byte[1];
                            int bytesRead = 0;
                            int numBytes = 0;
                            int currentLen = 1;
                            byte content[] = new byte[currentLen];
                            
    // this code doubles the size of the buffer everytime
    // it exceeds the current capacity.
                            try {
                                    while ((bytesRead = is.read(buf)) != -1) {
                                            if( numBytes+bytesRead > currentLen ){
                                                    byte old[] = content;
                                                    content = new byte[currentLen*2];
                                                    for( int ct=0; ct < currentLen ; ct++
    ){
                                                            content[ct] = old[ct];
                                                    }
                                                    currentLen *= 2;
                                            }
                                            for( int ct=0;ct<bytesRead;ct++){
                                                    content[numBytes+ct] = buf[ct];
                                            }
                                            numBytes += bytesRead;
                                    }
                            } catch (IOException e) {
                                    throw new Error(
                                            getClass().toString()
                                            + ".buildResultHash [1] : "
                                            + e.getMessage()
                                    );
                            }

                            if( numBytes == 0 ){
                                    continue;
                            }
                            
                            if( content == null ){
                                    logger.debug( "... and is NULL!" );
                            } else {
                                    logger.debug( "... and contains "
                                            + content.length
                                            + " bytes!"
                                    );
                            }

    -- 
      Andrea Spinelli - IT&T srl aspinelli@no-spam-plase-imteam.it
      Via Sigismondi, 40 - 24018 Villa d'Alme' (BG)
      tel: +39+035636029 - fax: +39+035638129   
      http://www.imteam.it/
    

  • Next message: Andrea Spinelli: "Re: clobs and blobs"