[prev in list] [next in list] [prev in thread] [next in thread] 

List:       mysql-java
Subject:    How to store a serialized object in MySQL?
From:       "Gary Bentley" <gb () opengroup ! org>
Date:       2000-07-31 17:37:12
[Download RAW message or body]

Meant to send this a couple of days ago, not sure where it went...

Basically this class will write a serializable object to a database and then
read it back.

I would put this functionality into some kind of convenience class.  i.e.
have a static method
to write the object and a method to read it back.  Probably a good idea to
store the
class type against the object as well.

There are better ways of doing this but they would be messier...

G.

Here's the code:

import java.io.*;
import java.util.*;
import java.sql.*;

public class JObjectWriteTest
{

    public JObjectWriteTest ()
    {

	try
	{

	    Class.forName ("org.gjt.mm.mysql.Driver");

	} catch (Exception e) {

	    System.out.println ("Cant load driver");

	    return;

	}

	try
	{

	    ByteArrayOutputStream bytes = new ByteArrayOutputStream ();

	    ObjectOutputStream out = new ObjectOutputStream (bytes);

	    Vector data = new Vector ();
	    data.add ("Test1");
	    data.add ("Test2");

	    out.writeObject (data);

	    String vectordata = bytes.toString ();

	    // Write to the database.
	    Connection cnt = DriverManager.getConnection
("jdbc:mysql://<yourhost>/<yourdb>",
							              "<yourusername>",
							              "<yourpassword>");

	    Statement st = cnt.createStatement ();

	    st.executeUpdate ("INSERT INTO <tablename> SET <fieldname>=\"" +
vectordata + "\"");

	    // Now read it back...
	    ResultSet rs = st.executeQuery ("SELECT * FROM <tablename>");

	    // Get the result set.
	    rs.next ();

	    // Get the data, you can be cleverer than this...
	    String vectdata = rs.getString (1);

          // Get the data as bytes.
	    byte bytedata[] = vectdata.getBytes ();

          // Write the bytes to an input stream.
	    ByteArrayInputStream inbytes = new ByteArrayInputStream (bytedata);

          // Get the object in an object stream.
	    ObjectInputStream in = new ObjectInputStream (inbytes);

          // Read the object and cast...
	    Vector storedata = (Vector) in.readObject ();

          // Prove it's been read back and act smug.
	    System.out.println ("Size of Vector: " + storedata.size ());

	    for (int i = 0; i < storedata.size (); i++)
	    {

		System.out.println ("i: " + i + " data: " + (String) storedata.get (i));

	    }

	// Pesky exception handling...
	} catch (IOException e) {

	    System.out.println ("Got IO exception: " + e.toString ());

	} catch (SQLException se) {

	    System.out.println ("Got SQL exception: " + se.toString ());

	} catch (ClassNotFoundException ce) {

	    System.out.println ("Got Class Not Found exception: " + ce.toString
());

	}

    }

    public static void main (String[] args)
    {

	JObjectWriteTest o = new JObjectWriteTest ();

    }

}


---------------------------------------------------------------------
Please check "http://www.mysql.com/Manual_chapter/manual_toc.html" before
posting. To request this thread, e-mail java-thread1479@lists.mysql.com

To unsubscribe, send a message to the address shown in the
List-Unsubscribe header of this message. If you cannot see it,
e-mail java-unsubscribe@lists.mysql.com instead.

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic