Thread: Storing objects

    #1
  1. No Profile Picture
    Swede
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Location
    Sweden
    Posts
    47
    Rep Power
    13

    Question Storing objects


    Hello!

    I'm in the beginning of a project, and wondering if I - in any way - can store Java-objects/instances in a DBMS ( In my case MySQL )?

    Imagine I have these classes:
    Code:
    class User implements Serializeable {
         String UserName;
    }
    class Driver implements Serializeable {
         User user;
    
         public Driver( String name ) {
              this.user = new User();
              this.user.UserName = name;
         }
    }
    class Passenger implements Serializeable {
         User user;
    
         public Passenger( String name ) {
              this.user = new User();
              this.user.UserName = name;
         }
    }
    class Car implements Serializeable {
         Driver driver;
         Passenger passenger;
    
         public Car( Driver driver, Passenger passenger ) {
              this.driver = driver;
              this.passenger = passenger;
         }
    }
    class Garage implements Serializeable {
         Car[] cars;
         private int i = 0;
    
         public Garage() {
              this.cars = new Car[ 3 ];
         }
    
         public void Add( Car car ) {
              if( this.i != 3 ) {
                   this.cars[ this.i++ ];
              }
              else {
                   throw new Exception();
              }
         }
    }
    From my baseclass, imagine I call them like this:
    Code:
    Garage garage = new Garage();
    Car car;
    
    car = new Car( new Driver( "acey" ), new Passenger( "Rudolph" ) );
    garage.Add( car );
    
    car = new Car( new Driver( "Rudolph" ), new Passenger( "acey" ) );
    garage.Add( car );
    After these declerations, I want to store the instance garage of the class Garage in my DBMS ( MySQL ). Can I do this? As you can see I implemented Serializeable, do this have any affect?

    Can I store an instance and still retrive the values stored in the instance, when I get the data from the DBMS?

    Thanks in advance!
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2001
    Posts
    5
    Rep Power
    0
    You can do this manually but you might want to look at using a package for this. There are several object/relational mapping packages.

    ObjectRelationalBridge is a Jakarta/Apache project:
    http://jakarta.apache.org/ojb/index.html

    Castor can also persist objects to XML:
    http://www.castor.org/

    There are several other but those have the most online support/tutorials.
  4. #3
  5. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    Are you wanting to store the data from your objects in a table, or are you wanting to store your whole object?
    Just a note, you can't query the contents of the object using SQL if you serialize it and write it to the database. You probably want to use one of those object/relational mapping packages.
  6. #4
  7. No Profile Picture
    Swede
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Location
    Sweden
    Posts
    47
    Rep Power
    13
    Uhm, nevermind. Thanks for your answers, thou.

    I realized I would loose the SQL-integration with this type of solution, so I keep storing the values.

IMN logo majestic logo threadwatch logo seochat tools logo