• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 258
  • 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
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

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