You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Paul Kofon <pk...@hotmail.com> on 2002/06/12 19:38:55 UTC
Storing Java Objects in SQL
Hi all,
I'm trying to store objects (instances) of a class in Microsoft SQL Server
2K using the Microsoft-provided JDBC driver, but I've been without any luck
thus far.
I've used the setObject() method in the PreparedStatement class and set my
target sql type to "BINARY", yet it doesn't work, I keep getting an
SQLException. A look at the driver documentation shows that this method is
supported while set/getBlob() aren't.
Is what I'm trying to achieve impossible or am I doing something wrong?
I could serialize the objects to disk but I'll have files lying all over the
place - storing them in the database per user would be much neater.
I need help fast! I'm Thanks.
Regards,
Paul
_________________________________________________________________
Send and receive Hotmail on your mobile device: http://mobile.msn.com
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: Storing Java Objects in SQL
Posted by Ravishankar S <ra...@ionidea.com>.
Hi Paul,
i came across an intresting article in JGuru hop this helps...personally the
new type4 driver from MS sucks..i prefer the weblogic type 4 or the
sprinta2000 type 4 driver....the MS driver has a lot of bugs with executing
stored procedures,multiple resultsets scrollable resultset etc...almost as
bad as the JDBC-ODBC bridge driver........
How do I extract a BLOB from a database?
Location: http://www.jguru.com/faq/view.jsp?EID=1325
Created: Nov 29, 1999 Modified: 2002-03-23 20:02:48.66
Author: Lennart Jorelid (http://www.jguru.com/guru/viewbio.jsp?EID=15)
A BLOB (Binary Large OBject) is essentially an array of bytes (byte[]),
stored in the database. You extract the data in two steps:
Call the getBlob method of the Statement class to retrieve a java.sql.Blob
object
Call either getBinaryStream or getBytes in the extracted Blob object to
retrieve the java byte[] which is the Blob object.
Note that a Blob is essentially a pointer to a byte array (called LOCATOR in
database-talk), so the java.sql.Blob object essentially wraps a byte
pointer. Thus, you must extract all data from the database blob before
calling commit or
private void runGetBLOB()
{
try
{ // Prepare a Statement:
PreparedStatement stmnt = conn.prepareStatement("select aBlob from
BlobTable");
// Execute
ResultSet rs = stmnt.executeQuery();
while(rs.next())
{
try
{
// Get as a BLOB
Blob aBlob = rs.getBlob(1);
byte[] allBytesInBlob = aBlob.getBytes(1, (int)
aBlob.length());
}
catch(Exception ex)
{
// The driver could not handle this as a BLOB...
// Fallback to default (and slower) byte[] handling
byte[] bytes = rs.getBytes(1);
}
}
// Close resources
rs.close();
stmnt.close();
}
catch(Exception ex)
{
this.log("Error when trying to read BLOB: " + ex);
}
}
Comments and alternative answers
getBytes() position starts with 1.
Author: Joseph Shelby (http://www.jguru.com/guru/viewbio.jsp?EID=26292), Apr
30, 2001
The specification (javadocs) of java.sql.Blob.getBytes() states: Parameters:
pos - the ordinal position of the first byte in the BLOB value to be
extracted; the first byte is at position 1
Seems much of jdbc is designed for database guys who think the world starts
with 1, rather than java/c/c++ programmers who know better... ;-)
--Joe
Re: getBytes() position starts with 1.
Author: Joe Sam Shirah (http://www.jguru.com/guru/viewbio.jsp?EID=42100),
Mar 23, 2002
Thanks, Joe. I have corrected the code to start at 1. And, yes, most JDBC
origins are at 1 rather than zero as you point out.
regards,
ravi
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: Storing Java Objects in SQL
Posted by RNivas <rn...@hotpop.com>.
please explain more
Thanks
----- Original Message -----
From: "Paul Kofon" <pk...@hotmail.com>
To: <to...@jakarta.apache.org>
Sent: Wednesday, June 12, 2002 11:08 PM
Subject: Storing Java Objects in SQL
> Hi all,
> I'm trying to store objects (instances) of a class in Microsoft SQL Server
> 2K using the Microsoft-provided JDBC driver, but I've been without any
luck
> thus far.
> I've used the setObject() method in the PreparedStatement class and set my
> target sql type to "BINARY", yet it doesn't work, I keep getting an
> SQLException. A look at the driver documentation shows that this method is
> supported while set/getBlob() aren't.
> Is what I'm trying to achieve impossible or am I doing something wrong?
> I could serialize the objects to disk but I'll have files lying all over
the
> place - storing them in the database per user would be much neater.
> I need help fast! I'm Thanks.
>
> Regards,
>
> Paul
>
>
> _________________________________________________________________
> Send and receive Hotmail on your mobile device: http://mobile.msn.com
>
>
> --
> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
> For additional commands, e-mail:
<ma...@jakarta.apache.org>
>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
RE: Storing Java Objects in SQL
Posted by John Gregg <jo...@techarch.com>.
It's not so simple. setObject() only works if the object is an instance of
a java class that maps nicely to whatever SQL type is the type of the column
you're trying to set (e.g., java.lang.String --> VARCHAR). You can't just
call setObject() on any java object and expect it to work. Unless you
really want to store serialized java objects as binary data, you should
search the internet for "object-relational mapping" to find out a little
about how to attack this problem. Lately I've been doing something like
this: http://www.martinfowler.com/isa/OR-mapping.html
john
-----Original Message-----
From:
tomcat-user-return-22382-john.gregg=techarch.com@jakarta.apache.org
[mailto:tomcat-user-return-22382-john.gregg=techarch.com@jakarta.apache.
org]On Behalf Of Paul Kofon
Sent: Wednesday, June 12, 2002 12:39 PM
To: tomcat-user@jakarta.apache.org
Subject: Storing Java Objects in SQL
Hi all,
I'm trying to store objects (instances) of a class in Microsoft SQL Server
2K using the Microsoft-provided JDBC driver, but I've been without any luck
thus far.
I've used the setObject() method in the PreparedStatement class and set my
target sql type to "BINARY", yet it doesn't work, I keep getting an
SQLException. A look at the driver documentation shows that this method is
supported while set/getBlob() aren't.
Is what I'm trying to achieve impossible or am I doing something wrong?
I could serialize the objects to disk but I'll have files lying all over the
place - storing them in the database per user would be much neater.
I need help fast! I'm Thanks.
Regards,
Paul
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: Storing Java Objects in SQL
Posted by Eric Everman <ev...@precedadesign.com>.
I'm not all that familiar with MS SQL, but is it possible that there is a
size restriction on the binary field in the underlying db? Also, if you
would like to do this in a more structured way such as mapping property
values to db fields, check out the Hibernate project at www.sourceforge.org
Good luck,
Eric Everman
At 12:38 PM 6/12/2002, you wrote:
>Hi all,
>I'm trying to store objects (instances) of a class in Microsoft SQL Server
>2K using the Microsoft-provided JDBC driver, but I've been without any
>luck thus far.
>I've used the setObject() method in the PreparedStatement class and set my
>target sql type to "BINARY", yet it doesn't work, I keep getting an
>SQLException. A look at the driver documentation shows that this method is
>supported while set/getBlob() aren't.
>Is what I'm trying to achieve impossible or am I doing something wrong?
>I could serialize the objects to disk but I'll have files lying all over
>the place - storing them in the database per user would be much neater.
>I need help fast! I'm Thanks.
>
>Regards,
>
>Paul
>
>
>_________________________________________________________________
>Send and receive Hotmail on your mobile device: http://mobile.msn.com
>
>
>--
>To unsubscribe, e-mail: <ma...@jakarta.apache.org>
>For additional commands, e-mail: <ma...@jakarta.apache.org>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>