ACEAFTY
asked on
Storing Java Objects in SQL Database
I am using Java and MySQL to store java bjects namely Job as a Java Object, how would I store the java object to the db I have a table set up with BLOB as the type. I want to store this java object to the field and retrieve it. Can anyone give me any examples or pointers to do this. Any help or advice will be greatly appreciated thank you.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
and how would you do retrieve that object from the database? do u have an example? CHEJ thank you
You basically reverse all that, using the ResultSet.getBinaryStream as your starting point
ASKER
if you can give me an example that will be very helpful and i will accept answer!
Something like
ByteArrayOutputStream out = new ByteArrayOutputStream();
InputStream in = rs.getBinaryStream(1);
int buf = -1;
while ((buf = in.read()) > -1) {
out.write(buf);
}
in.close();
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(out.t oByteArray ()));
Object retrievedObject = in.readObject();
ByteArrayOutputStream out = new ByteArrayOutputStream();
InputStream in = rs.getBinaryStream(1);
int buf = -1;
while ((buf = in.read()) > -1) {
out.write(buf);
}
in.close();
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(out.t
Object retrievedObject = in.readObject();
ASKER
stil not working
i have done this
String sql1 = "SELECT Job FROM jobtable";
Statement stmt1 = conn.createStatement();
ResultSet rs = stmt1.executeQuery(sql1);
ByteArrayOutputStream out1 = new ByteArrayOutputStream();
InputStream in1 = rs.getBinaryStream("Job");
int buf = -1;
while ((buf = in1.read()) > -1) {
out1.write(buf);
}
in1.close();
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(out1. toByteArra y()));
Object retrievedObject = ois.readObject();
Job recievedJob = (Job) retrievedObject;
System.out.println("done") ;
System.out.println(recieve dJob.getDa teCreated( ));
i get this exception
java.sql.SQLException: Before start of result set
at com.mysql.jdbc.ResultSet.c heckRowPos (ResultSet .java:4424 )
at com.mysql.jdbc.ResultSet.g etBinarySt ream(Resul tSet.java: 620)
at com.mysql.jdbc.ResultSet.g etBinarySt ream(Resul tSet.java: 646)
at JobApp.DataStore.connect(D ataStore.j ava:49)
at JobApp.DataStore.main(Data Store.java :79)
i have done this
String sql1 = "SELECT Job FROM jobtable";
Statement stmt1 = conn.createStatement();
ResultSet rs = stmt1.executeQuery(sql1);
ByteArrayOutputStream out1 = new ByteArrayOutputStream();
InputStream in1 = rs.getBinaryStream("Job");
int buf = -1;
while ((buf = in1.read()) > -1) {
out1.write(buf);
}
in1.close();
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(out1.
Object retrievedObject = ois.readObject();
Job recievedJob = (Job) retrievedObject;
System.out.println("done")
System.out.println(recieve
i get this exception
java.sql.SQLException: Before start of result set
at com.mysql.jdbc.ResultSet.c
at com.mysql.jdbc.ResultSet.g
at com.mysql.jdbc.ResultSet.g
at JobApp.DataStore.connect(D
at JobApp.DataStore.main(Data
ASKER
InputStream in1 = rs.getBinaryStream("Job"); should be InputStream in1 = rs.getBinaryStream(1);
>>ResultSet rs = stmt1.executeQuery(sql1);
should be followed by
while (rs.next()) {
// all that code in here
}
should be followed by
while (rs.next()) {
// all that code in here
}
ASKER
ok great thanx for all ur help greatly appreciated
;-)
http://forum.java.sun.com/thread.jspa?forumID=48&threadID=448964