Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 31st, 2002, 03:18 PM
acey acey is offline
Swede
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Location: Sweden
Posts: 47 acey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
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!

Reply With Quote
  #2  
Old July 31st, 2002, 10:54 PM
jnicholas jnicholas is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2001
Posts: 5 jnicholas User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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.

Reply With Quote
  #3  
Old August 1st, 2002, 03:21 AM
bricker42 bricker42 is offline
Moderator =(8^(|)
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2002
Location: Sacramento, CA
Posts: 1,710 bricker42 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 38 sec
Reputation Power: 8
Send a message via AIM to bricker42
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.

Reply With Quote
  #4  
Old August 1st, 2002, 04:25 PM
acey acey is offline
Swede
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Location: Sweden
Posts: 47 acey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Storing objects


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway