Link to home
Start Free TrialLog in
Avatar of ACEAFTY
ACEAFTYFlag for United Kingdom of Great Britain and Northern Ireland

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.
Avatar of zzynx
zzynx
Flag of Belgium image

ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ACEAFTY

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
Avatar of ACEAFTY

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.toByteArray()));
Object retrievedObject = in.readObject();
Avatar of ACEAFTY

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.toByteArray()));
            Object retrievedObject = ois.readObject();
            Job recievedJob = (Job) retrievedObject;
            System.out.println("done");
            System.out.println(recievedJob.getDateCreated());

i get this exception

java.sql.SQLException: Before start of result set
        at com.mysql.jdbc.ResultSet.checkRowPos(ResultSet.java:4424)
        at com.mysql.jdbc.ResultSet.getBinaryStream(ResultSet.java:620)
        at com.mysql.jdbc.ResultSet.getBinaryStream(ResultSet.java:646)
        at JobApp.DataStore.connect(DataStore.java:49)
        at JobApp.DataStore.main(DataStore.java:79)
Avatar of ACEAFTY

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
}

Avatar of ACEAFTY

ASKER

ok great thanx for all ur help greatly appreciated
;-)