Click to See Complete Forum and Search --> : insert java Object into oracle database..


Thilkumar82
08-12-2008, 06:27 AM
Hi,
I want to insert java Object into oracle database..

I am using BLOB column and code as follows ..

Data1 data1=new Data1();
data1.a=500;
data1.b=600;
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:oci:@mydb", "username", "pwd");
PreparedStatement pst = con.prepareStatement("insert into obj_temp(user_id,data_object) values (?,?)");
pst.setInt(1,10);
pst.setObject(2,data1);

when executing this .. i am getting "Invalid column type" SQLException..
I am doing this for later use of this object from database..
here i want to get A's value 500 and B's value 600 when i retrieving the object from datadase..

Thanks in Advance,
Thilkumar

chazzy
08-12-2008, 08:23 AM
what datatype is 'data_object'?

Thilkumar82
08-12-2008, 08:36 AM
i am using datatype of 'data_object' is BLOB..
tell me if possible for any other datatype?

Thanks,
Thilkumar

chazzy
08-12-2008, 10:26 AM
ok, no, blob is the right type. is Data1 serializable? can you try serializing it through a string writer first and try inserting that way? can you post the full stack trace, it's better than just giving a couple of words...

Thilkumar82
08-13-2008, 02:35 AM
Thanks chazzy,

I got solution for inserting in db.. but when i selecting from db it gives following error,
java.io.StreamCorruptedException: invalid stream header

Regards,
Thilkumar

chazzy
08-13-2008, 10:02 AM
again, you'll need to post some code to understand how you're selecting it from the DB.

Thilkumar82
08-14-2008, 03:11 AM
Code is as follows..

rs = stmt.getResultSet();
while(rs.next())
{
InputStream is = rs.getBlob("data_object").getBinaryStream();
ObjectInputStream oip = new ObjectInputStream(is);
Object object = oip.readObject();
oip.close();
is.close();
Data1 objData1 = (Data1) object;

Regards,
Thilkumar

chazzy
08-14-2008, 08:18 AM
and which line is throwing the exception? what is the full stack trace?

Thilkumar82
08-14-2008, 08:22 AM
Hi chazzy,
Sorry , i gor the solution to select object from DB.the above code is corrected one. it is working fine.. thanks,

Thilkumar