File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes ArrayIndexOutOfBoundsException when saving Serializable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "ArrayIndexOutOfBoundsException when saving Serializable" Watch "ArrayIndexOutOfBoundsException when saving Serializable" New topic

ArrayIndexOutOfBoundsException when saving Serializable

T Bush

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);
result = baos.toByteArray();
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(" set ")
.append("= ? ")
.append(" where ")
.append(" = ?");
PreparedStatement ps = myConn.prepareStatement(myBuffer.toString());
ps.setBytes(1, myBuf);
ps.setLong(2, myUp.getKey());

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

Todd Bush<BR>AIM: RalphWiggam14
I agree. Here's the link:
subject: ArrayIndexOutOfBoundsException when saving Serializable
jQuery in Action, 3rd edition