MySQL jdbc, cannot insert object

Hi all,

I am having troubles inserting an object into a MySQL database. I tried to insert it with both the BinaryInputStream and setObject functions, but to no avail. If I run the query in mysql-query-browser, it runs fine (as long as I have nulled out the ** INPUT STREAM ** from doing a PreparedStatement.toString(). I have googled my exception line numbers but nothing turns up. I don't believe it is bad SQL syntax as I can run it in the query-browser tool, so I am stumped as to where else I can look.

Here is the code I am using:

//st.setObject(j, value);

ByteArrayInputStream input_stream = io.serialize(value);

st.setBinaryStream(j, input_stream, input_stream.available());

// serialize an object to a string which can be stored into a mysql database

publicstatic ByteArrayInputStream serialize(Object input_object)

{

try

{

ByteArrayOutputStream os =new ByteArrayOutputStream();

ObjectOutput object_output =new ObjectOutputStream(os);

object_output.writeObject(input_object);

return(new ByteArrayInputStream(os.toByteArray()));

//return(os.toString());

}

catch(IOException e)

{

e.printStackTrace();

return(null);

}

}

statement:com.mysql.jdbc.PreparedStatement@5f1eb199: insert into web_application.session (remote_address,update_time,user,browser_name,history,session_id,start_time) values('127.0.0.1','2007-06-24 10:14:34',null,'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1.4) Gecko/20070601 Firefox/2.0.0.4',** STREAM DATA **,'FCBB07D8B524044B79F546337512DAA1','2007-06-24 10:14:34')

createSQLException.com.mysql.jdbc.SQLError=SQLError.java,957

checkErrorPacket.com.mysql.jdbc.MysqlIO=MysqlIO.java,2938

sendCommand.com.mysql.jdbc.MysqlIO=MysqlIO.java,1601

sqlQueryDirect.com.mysql.jdbc.MysqlIO=MysqlIO.java,1710

execSQL.com.mysql.jdbc.Connection=Connection.java,2436

executeInternal.com.mysql.jdbc.PreparedStatement=PreparedStatement.java,1402

executeUpdate.com.mysql.jdbc.PreparedStatement=PreparedStatement.java,1593

insert.io.database=database.java,206

insert.application.table=table.java,131

insert.application.session=session.java,123

<init>.application.session=session.java,50

[3038 byte] By [walterjwhitea] at [2007-11-27 8:39:53]
# 1
Try posting this on the JDBC forum: http://forum.java.sun.com/forum.jspa?forumID=48
gymma at 2007-7-12 20:38:04 > top of Java-index,Java Essentials,Java Programming...
# 2
To upload your object to the DB, do this1 - Select the row you want with a ResultSet2 - use rs.getBinaryStream() to get a OutputStream3 - write your object to the stream and close itPost If you still need helpcya
pbulgarellia at 2007-7-12 20:38:04 > top of Java-index,Java Essentials,Java Programming...
# 3

Thanks for your replies - I do not have a ResultSet unless I'm updating the table. I can insert data into the database with setObject(), but when I try to do a get object, all I get is an address:

B@7cd0a5d9

That is why I tried to use setBinaryStream, unfortunately, that also does not work for me yet.

Walter

walterjwhitea at 2007-7-12 20:38:04 > top of Java-index,Java Essentials,Java Programming...
# 4

> Thanks for your replies - I do not have a ResultSet

> unless I'm updating the table. I can insert data

> into the database with setObject(), but when I try to

> do a get object, all I get is an address:

>

> > B@7cd0a5d9

>

You mean that's the string that gets printed out?

That class doesn't override toString, that's all.

http://developer.java.sun.com/developer/Books/effectivejava/Chapter3.pdf

jverda at 2007-7-12 20:38:04 > top of Java-index,Java Essentials,Java Programming...
# 5

Ok,

I figured it out on my own from here:

http://www.velocityreviews.com/forums/t128721-how-to-convert-a-byte-array-to-objectinputstream.html

What was happening was, I apparently cannot just call setObject or getObject, those don't work. What I need to do is use inputStream.Maybe there is an underlying bug as you would assume that is what it calls anyway. So now, I'm storing objects and reading objects.

Thanks for you guys' help hopefully this can be of help to others, maybe this should be passed on to the developers of this driver.

Walter

walterjwhitea at 2007-7-12 20:38:04 > top of Java-index,Java Essentials,Java Programming...
# 6

> What was happening was, I apparently cannot just call

> setObject or getObject, those don't work. What I

> need to do is use inputStream.Maybe there is an

> underlying bug

Highly unlikely. Almost certainly your own mistake or misunderstanding.

> as you would assume that is what it

> calls anyway.

I wouldn't.

jverda at 2007-7-12 20:38:04 > top of Java-index,Java Essentials,Java Programming...