7 Replies Latest reply: May 15, 2010 1:40 AM by 843790 RSS

    Serialize ResultSet and send over a network?

    793901
      hi

      I'm quiet new to Java, and I'm trying to send a ResultSet over a network but ResultSet isn't Serializable.
      I tried to cheat by putting my ResultSet into another Serializable object but it still won't work.
      Here's the line that gives me the error:
      CustomClass ola = ( CustomClass ) input.readObject();
      And here's what I tried:
      public class CustomClass implements Serializable
      {
           private ResultSet rr;
           public CustomClass(ResultSet r)
           {
                rr = r;
           }
      }
      And the error:
      java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: sun.jdbc.odbc.JdbcOdbcResultSet
           at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1333)
           at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1947)
           at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1871)
           at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)
           at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
           at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
           at Client.Client.runClient(Client.java:90)
           at Client.ClientStartUp.main(ClientStartUp.java:8)
      Caused by: java.io.NotSerializableException: sun.jdbc.odbc.JdbcOdbcResultSet
           at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1156)
           at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1509)
           at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1474)
           at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1392)
           at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1150)
           at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:326)
           at Server.ServerInstance.sendData(ServerInstance.java:42)
           at Server.ServerInstance.run(ServerInstance.java:66)
           at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
           at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
           at java.lang.Thread.run(Thread.java:619)
      Thank you for your time
        • 1. Re: Serialize ResultSet and send over a network?
          843790
          Why would you want to? Can't the thing you're sending to simply perform the query itself? Whatever your response, the answer is "you can't, and there's no earthly reason why you'd want to". If you think otherwise, think again. What makes you think this is necessary?
          • 2. Re: Serialize ResultSet and send over a network?
            793901
            Thanks for your reply.

            My best friend(Google) told me that there's indeed no way in sending a ResultSet. I don't need to care about performance so now everything is going over the net into some CachedRowSetImpl().

            The reason why I want this, is that I'll have to work on a project for school next semester, and I'm sure we'll have to write a piece of software that'll require a database.
            So I thought "why not have a central database and query it over the net". I'm not saying this is the best method, but I'm trying :P

            Do you have better ideas? I'll be glad to hear them, and I'm not too lazy to read tutorials :)

            Thanks for your help!!

            Andrew
            • 3. Re: Serialize ResultSet and send over a network?
              843790
              Andrew_ wrote:
              Thanks for your reply.

              My best friend(Google) told me that there's indeed no way in sending a ResultSet. I don't need to care about performance so now everything is going over the net into some CachedRowSetImpl().
              Think more about what you're going to do with this data, rather than how to shunt it about the place.
              The reason why I want this, is that I'll have to work on a project for school next semester, and I'm sure we'll have to write a piece of software that'll require a database.
              Kudos for getting a head-start, then. If only more students cared that much
              So I thought "why not have a central database and query it over the net". I'm not saying this is the best method, but I'm trying :P
              Which won't work, and makes no sense anyway. And isn't "having a central database" either!
              Do you have better ideas? I'll be glad to hear them,
              Yeh. A few. The clients can just query the database themselves. That's one way. Or, presumably once you read in the ResultSet, you do something else with the data. Use it to populate some Java objects (beans, for instance)? Why not serialize them and send them to the other machines? That way your database itself isn't exposed to other machines. If you're simply taking the raw data and sticking it straight into a Swing GUI or something, reconsider that design, it's a bad practice
              and I'm not too lazy to read tutorials :)
              Good. It really is the best way. Lots of luck!
              • 4. Re: Serialize ResultSet and send over a network?
                Alan.M
                Wrap your result set into a bean ( DynaBean see http://commons.apache.org/beanutils) and then serialize the DynaBeans and sent them.
                On the other side, do the deserialization and build you ResultSet by using a Vendor implelemtation of the ResultSet interface



                Regards,
                Alan Mehio
                London,UK
                • 5. Re: Serialize ResultSet and send over a network?
                  793901
                  Thanks for the replies people, I'll look into all that when I find some time. I need to dig into ASP.NET using C# and MS SQL Server first.
                  I'll update this when I'll have read about JavaBeans(never heard of that).

                  Thanks

                  Andrew
                  • 6. Re: Serialize ResultSet and send over a network?
                    jschellSomeoneStoleMyAlias
                    alan_mehio wrote:
                    Wrap your result set into a bean ( DynaBean see http://commons.apache.org/beanutils) and then serialize the DynaBeans and sent them.
                    On the other side, do the deserialization and build you ResultSet by using a Vendor implelemtation of the ResultSet interface

                    There is absolutely no way to "serialize" a result set. Just as there is no way to serialize a socket.

                    Both are associated with resources that only exist on one machine and those resources cannot be transferred to another machine. It is similar to attempting the serialize the hard drive (not the data but the physical device) and then sending that over a wire.

                    A result set, a socket and for that matter even a hard drive can however have the data extracted and that data can be sent over a wire.
                    • 7. Re: Serialize ResultSet and send over a network?
                      843790
                      One way is to use your own special class which is serializable. Means to fetch info. from ResultSet and store it in your own object which is serializable and pass it over network. But it requires lots of work.

                      Another best and generic way is to use CachedRowSetImpl class which extends ResultSet but it is Serializable.
                      So u can use it as u like to pass over network - from server side to client side or any way u want to use.

                      The detailed tutorial on how to use is given on the following link.

                      http://onjava.com/pub/a/onjava/2004/06/23/cachedrowset.html

                      Think this will help u.

                      Roshan Qureshi,
                      Integ EC.
                      Ahmedabad,
                      India.