Hi,

I'm looking at using the H2 engine for a persistant java object store
and I ran across a strange problem. I can store and retrieve java
objects no problem in the normal case. However, when I update a row
using an updatable result set, the object in that row is no longer
returned as an object, but rather as an array of bytes.

I'm including below a simple code example which demonstrates the
problem. I store a String as an object in a row, fetch it out again
correctly, then update the row, and fetch again.

The second fetch results in a byte array instead of the correct
object. This happens regardless of the object type.

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

public class UpdateRow {

        static int execute(Connection conn, String sql) throws SQLException {
                Statement stmt = conn.createStatement();
                int rv = stmt.executeUpdate(sql);
                stmt.close();
                return rv;
        }

        public static void main(String[] args) throws SQLException,
ClassNotFoundException, IOException {
                Class.forName("org.h2.Driver");
                File file = File.createTempFile("test", "db");
                Connection conn =
DriverManager.getConnection("jdbc:h2:"+file.getAbsolutePath());
                execute(conn,"CREATE TABLE object_index (id integer primary key,
object other, number integer)");

                PreparedStatement stmt = conn.prepareStatement("INSERT INTO
object_index (id,object)  VALUES (1,?)");
                stmt.setObject(1,"hello", Types.JAVA_OBJECT);
                stmt.execute();

                // select object (as string) from row
                Statement s = conn.createStatement();
                ResultSet rs = s.executeQuery("SELECT object,id,number FROM
object_index WHERE id =1");
                if(rs.next()) {
                        // prints out "hello"
                        System.out.println(rs.getObject(1));
                }
                rs.close();
                s.close();

                // update row
                s =
conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE);
                rs = s.executeQuery("SELECT object,id,number FROM object_index 
WHERE
id =1");
                if(rs.next()) {
                        // prints out "hello"
                        System.out.println(rs.getObject(1));
                        rs.updateInt(2, 1);
                        rs.updateRow(); // succeeds
                }
                rs.close();
                s.close();

                // select again
                s = conn.createStatement();
                rs = s.executeQuery("SELECT object,id,number FROM object_index 
WHERE
id =1");
                if(rs.next()) {
                        // XXX: is no longer a string - is a byte array[] !
                        System.out.println(rs.getObject(1));
                }
                rs.close();
                s.close();

                conn.close();
        }
}



-- Joel Carranza
[EMAIL PROTECTED]

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "H2 
Database" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/h2-database?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to