Board index » jbuilder » Microsoft SQL & DataExpress

Microsoft SQL & DataExpress


2004-10-06 12:20:10 AM
jbuilder12
Hello All,
I am trying to store a Java Object (HTMLDocument) into a text data type of
MS-SQL Server ver 8.0. And I have been getting the same error of Invalid
type.
Initially the predetermined type of "text" by QueryDataSet is "STRING". This
was not working therefore I changed it to OBJECT. Somehow this still does
not work.
My Java Object is serializable.
Help will be appriciated.
Regards.
 
 

Re:Microsoft SQL & DataExpress

"Ali" < XXXX@XXXXX.COM >wrote in message
Quote
Hello All,
I am trying to store a Java Object (HTMLDocument) into a text data type of
MS-SQL Server ver 8.0. And I have been getting the same error of Invalid
type.
Initially the predetermined type of "text" by QueryDataSet is "STRING".
This
was not working therefore I changed it to OBJECT. Somehow this still does
not work.
My Java Object is serializable.
Help will be appriciated.

Regards.

Although I have not used MS-SQL Server in quite some time, I feel sure that
you will need to store the Object in the database in a BLOB type field.
The Data Express components do not support Blob types well, as they tend to
vary between RDBMS implementations. Not sure about MS-SQL, but below is an
Oracle example:
private boolean getAndWriteOracleBlobData(int id, String
fileName,java.io.InputStream data) {
boolean result = true; //assume true
String sql="Select ID, OBJECTDATA from <TABLE_NAME>where ID='"+id+"'
AND FILENAME='"+fileName+"' FOR UPDATE";
int idFile=0;
try {
//getConnection().setAutoCommit(false);
ResultSet rs=getConnection().createStatement().executeQuery(sql);
// getConnection().commit();
if(rs.next()){
idFile=rs.getInt("IDFILE");
/* Get the oracle type BLOB field to perform lock on the blob. The Select
FOR UPDATE performs a persistent lock for Oracle Databases.*/
oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob("OBJECTDATA");
/* now that we have the Blob, we need to get the stream to the BLOB
type field. We have to get a reference to the blob stream,
and assign this to our output stream to write to the stream. */
java.io.OutputStream bufStr = blob.getBinaryOutputStream();
//Get the size of the data stream we are intending to write to the
stream
int size=data.available();
//create an array of bytes of the same size as our data
byte[] b=new byte[size];
// Assign an int to represent the size of bytes that we are reading from
the stream
int numBytes=data.read(b);
// As long as numBytes is greater than zero, write to the OutputStream
(aka Blob field).
while(numBytes>0) {
bufStr.write(b,0,numBytes);
numBytes=data.read(b);
}
bufStr.flush(); /Make sure to flush, then close the
OutputStream..
bufStr.close();
rs.close(); //close current connection
}
catch(Exception ex) {
result=false;
getConnection().rollback();
System.err.writeln(ex.getMessage())
}
return result;
}
That is the way you do it in Oracle. There will be some variation for
SQL-Server, but the above code should give you a good starting point.
Good Luck..