Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / July 2004

Tip: Looking for answers? Try searching our database.

getting data from 'image' field

Thread view: 
Georg Andersson - 23 Jul 2004 09:43 GMT
hello

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.")...

best regards
Soeren Degn Jahns - 23 Jul 2004 10:14 GMT
> 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
Andrea Spinelli - 27 Jul 2004 19:58 GMT
"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
[quoted text clipped - 8 lines]
>
> // 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!"
                );
            }

Signature

 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/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.