Sunday, July 1, 2012

Storing/retrieving shared objects in a database

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.

Any help is greatly appreciated,

Thanks

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

No comments:

Post a Comment