writing a java object to an oracle 9i database [message #103454] |
Thu, 09 October 2003 07:57 |
James Stevens
Messages: 2 Registered: October 2003
|
Junior Member |
|
|
Hi peeps
I was wondering if you might be able to help me with a
java/oracle 9i problem that i have? I can't seem to
get the code to insert a java object into oracle to
work. Here is my code:
I first tried this:
CREATE TABLE ONLINEUSERS(USERID VARCHAR2(50) NOT NULL, PROFILE BLOB NOT NULL);
I have a java class called Profile which doesn't do
very much and I instantiate this and try to insert
the object into the table as follows:
String userId = "james";
Profile profile = new Profile();
(I have a PreparedStatement and Connection declared already)
pStatement = connection.prepareStatement
("INSERT INTO ONLINEUSERS (USERID,PROFILE) VALUES(?,?)");
{
pStatement.setString(1,userId);
pStatement.setObject(2,(Object)profile);
int count = pStatement.executeUpdate();
if(count == 0)
{
connection.rollback();
}
else
{
connection.commit();
}
This didn't work, so i tried the easier way of using
insert:
String query = "INSERT INTO ONLINEUSERS VALUES('"+userId+"', "+profile+")";
statement.execute(query);
This also fails on inserting profile.
I would be most grateful for any help you might give
me on inserting objects into the database and ALSO
retrieving them.
Many thanks
James
|
|
|