|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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. |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
|||
|
|||
|
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. ![]() |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Java Help > Storing objects |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|