On 01/07/2012 20:26, Peter wrote:
Hi guys,

Basically, as the title says, I would like to store a shared object in a MySQL 
database
and retrieve it at a later time. So I wrote a simple shared class Test:

public class Test implements Serializable {
     private int statusId;
     public Test() {
     }
     public Test(int statusId) {
         this.statusId = statusId;
     }
}

Now I have an rpc that stores an instance of the Test class into a 
single-column table
(the MySQL type is BLOB). This is the rpc implementation:

     public void storeTest(Test test) {
         try {
             Class.forName("com.mysql.jdbc.Driver");
             Connection conn = 
DriverManager.getConnection(MySQLConnection.DB_URL,
MySQLConnection.DB_USER, MySQLConnection.DB_PWD);
             PreparedStatement statement = conn.prepareStatement("insert into 
test_table
(test_object) values (?)");
             statement.setObject(1, test);
             statement.executeUpdate();
         } catch (SQLException e) {
         } catch (ClassNotFoundException e) {
         }
     }

For retrieval, I have another rpc as follows (server side code again):

     public Test getTest() {
         Test t = new Test();
         try {
             Class.forName("com.mysql.jdbc.Driver");
             Connection conn = 
DriverManager.getConnection(MySQLConnection.DB_URL,
MySQLConnection.DB_USER, MySQLConnection.DB_PWD);
             PreparedStatement statement = conn.prepareStatement("select 
test_object from
test_table");
             ResultSet resultSet = statement.executeQuery();
             resultSet.next();
             t = (Test) resultSet.getObject(1);
         } catch (SQLException e) {
         } catch (ClassNotFoundException e) {
         }
         return t;
     }

The storeTest(Test test) method runs fine (at least I think it does) - no 
errors are
returned and the record is inserted into the MySQL table. However, the method 
getTest()
fails during the conversion *t = (Test) resultSet.getObject(1)*. Now I've done 
some
digging and apparently the object returned by resultSet.getObject(1) is an 
instance of a
class called *[B*, which seems to be a JNI byte array which can't be converted 
into Test.

It is unclear to me why this happens. I would expect that the BLOB column 
contains all the
necessary info to rebuild the Test object. I'm pretty sure I'm missing something
fundamental here, but can't quite figure it out.

I have no experience with storing object data in a blob of a SQL base. Maybe it acts like serialization (I see you made your class serializable).

The first Google hit I got uses a slightly different, more indirect method:
http://www.easywayserver.com/blog/how-to-serializable-object-in-java-2/

The second hit explains why your method doesn't work, and why ObjectInputStream is necessary: http://hans.liss.pp.se/node/700

--
Philippe Lhoste
--  (near) Paris -- France
--  http://Phi.Lho.free.fr
--  --  --  --  --  --  --  --  --  --  --  --  --  --



--
You received this message because you are subscribed to the Google Groups "Google 
Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to 
google-web-toolkit+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-web-toolkit?hl=en.

Reply via email to