This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JDBC and the fly likes ArrayIndexOutOfBoundsException when saving Serializable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "ArrayIndexOutOfBoundsException when saving Serializable" Watch "ArrayIndexOutOfBoundsException when saving Serializable" New topic
Author

ArrayIndexOutOfBoundsException when saving Serializable

T Bush
Greenhorn

Joined: Jul 31, 2000
Posts: 13
I'm getting a com.inet.tds.SQLException: java.lang.ArrayIndexOutOfBoundsException
when I'm attempting to persist my object as an array of bytes to a SQL Server db. The column that holds the object is of datatype image. Anyone know why I'm getting the exception?
here's my code.
how I make it a byte array
private byte[] serializeUserPreference(UserPreference obj) {
byte[] result = new byte[BYTE_MAX];
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
result = baos.toByteArray();
oos.flush();
oos.close();
}
catch(Exception e) {
;
}
return result;
}

and how I persist...
Connection myConn = aTransaction.getConnection();
StringBuffer myBuffer = new StringBuffer();
byte[] myBuf = serializeUserPreference(myUp);
myBuffer.append("update ")
.append(TABLE_NAME)
.append(" set ")
.append(DATA_FIELD)
.append("= ? ")
.append(" where ")
.append(KEY_FIELD_NAME)
.append(" = ?");
PreparedStatement ps = myConn.prepareStatement(myBuffer.toString());
ps.setBytes(1, myBuf);
ps.setLong(2, myUp.getKey());
ps.execute();

where the constants are correct for column and table names
Thanks
[This message has been edited by T Bush (edited February 07, 2001).]


Todd Bush<BR>AIM: RalphWiggam14
 
Consider Paul's rocket mass heater.
 
subject: ArrayIndexOutOfBoundsException when saving Serializable
 
Similar Threads
ByteArrayStream steam!
How to insert an array into Blob using JDBC
Storing Object into Database
Storing serialized objects
Getting java.util.zip.ZipException: oversubscribed dynamic bit lengths tre