• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

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.
0
ACEAFTY
Asked:
ACEAFTY
  • 5
  • 5
1 Solution
 
CEHJCommented:
There's probably a more elegant way but this should work:

String sql = "insert into table (objectfield) values (?)";
PreparedStatement ps = conn.prepareStatement(sql);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bout);
out.writeObject(yourObject);
byte[] bytes = bout.toByteArray();
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
ps.setBinaryStream(1, in, bytes.length);
ps.executeUpdate();
0
 
ACEAFTYAuthor Commented:
and how would you do retrieve that object from the database? do u have an example? CHEJ thank you
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
CEHJCommented:
You basically reverse all that, using the ResultSet.getBinaryStream as your starting point
0
 
ACEAFTYAuthor Commented:
if you can give me an example that will be very helpful and i will accept answer!
0
 
CEHJCommented:
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();
0
 
ACEAFTYAuthor Commented:
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)
0
 
ACEAFTYAuthor Commented:
InputStream in1 = rs.getBinaryStream("Job"); should be InputStream in1 = rs.getBinaryStream(1);

 
0
 
CEHJCommented:
>>ResultSet rs = stmt1.executeQuery(sql1);

should be followed by

while (rs.next()) {
    // all that code in here
}

0
 
ACEAFTYAuthor Commented:
ok great thanx for all ur help greatly appreciated
0
 
CEHJCommented:
;-)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now