> 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/