Este ejemplo demuestra la simple que es crear una base de datos y
almacenar objetos. También ilustra dos métodos de consulta:
Query-By-Example (QBE) y el más flexible S.O.D.A. Aquí puede bajarse el código fuente de este artículo. Para ejecutarlo sólo tiene que añadir el fichero JAR de db4o a su CLASSPATH y ejecutar la clase principal Db4oTest.java.
Las dos clases del ejemplo representan un Team y un Player de Baseball. Para hacer las cosas más interesantes también tenemos una clase Pitcher. La clase Pitcher es una subclase de Player y añade un campo extra sobre los heredados. Team tiene un atributo que es una lista de objetos Player, que puede, por supuesto, incluir objetos Pitcher. Los objetos Team, Player, y Pitcher
son objetos "normales" de Java, sin código de persistencia. No se
requieren atributos de clave única, porque la base de datos de objetos
almacena automáticamente los objetos con un identificador de objeto
único (OID).
La clase Player
public class Player {
protected String name;
protected int squadNumber;
protected float battingAverage;
protected Team team;
public Player(String name, int squadNumber,
float battingAverage){
this.name = name;
this.squadNumber = squadNumber;
this.battingAverage = battingAverage;
}
public void setName(String n){this.name = n;}
public String getName(){return this.name;}
public void setSquadNumber(int s){this.squadNumber = s;}
public int getSquadNumber(){return this.squadNumber;}
public void setBattingAverage(final float b) {
this.battingAverage = b; }
public float getBattingAverage(){
return this.battingAverage;}
public void setTeam(Team t) {this.team = t;}
public Team getTeam() {return this.team;}
public String toString() {
return name + ":" + battingAverage;
}
}
La clase Pitcher
public class Pitcher extends Player{
private int wins;
public Pitcher(String name, int squadNumber,
float battingAverage, int wins) {
super(name,squadNumber,battingAverage);
this.wins = wins;
}
public void setWins(final int w){this.wins = w;}
public int getWins() {return this.wins;}
public String toString() {
return name + ":" + battingAverage + ", " + wins;
}
}
La clase Team
import java.util.List;
import java.util.ArrayList;
public class Team {
private String name;
private String city;
private int won;
private int lost;
private List players;
public Team(String name, String city,
int won, int lost){
this.name = name;
this.city = city;
this.won = won;
this.lost = lost;
this.players = new ArrayList();
}
public void addPlayer(Player p) {
players.add(p);
}
public void setName(String n){this.name = n;}
public String getName(){return this.name;}
public void setStadium(String c){this.city = c;}
public String getCity(){return this.city;}
public void setPlayers(List p){players = p;}
public List getPlayers(){return players;}
public void setWon(int w) {this.won = w;}
public int getWon(){return this.won;}
public void setLost(int l) {this.lost = l;}
public int getLost() {return this.lost;}
public String toString() {
return name;
}
}
Primero, creamos algunos datos de prueba con los que trabajar:
// Create Players
Player p1 = new Player("Barry Bonds", 25, 0.362f);
Player p2 = new Player("Marquis Grissom", 9, 0.279f);
Player p3 = new Player("Ray Durham", 5, 0.282f);
Player p4 = new Player("Adrian Beltre", 29, 0.334f);
Player p5 = new Player("Cesar Izturis", 3, 0.288f);
Player p6 = new Player("Shawn Green", 15, 0.266f);
// Create Pitchers
Player p7 = new Pitcher("Kirk Rueter",46, 0.131f, 9);
Player p8 = new Pitcher("Kazuhisa Ishii",17, 0.127f, 13);
// Create Teams
Team t1 = new Team("Giants", "San Francisco", 91, 71);
Team t2 = new Team("Dodgers", "Los Angeles", 93, 69);
// Add Players to Teams
t1.addPlayer(p1); p1.setTeam(t1);
t1.addPlayer(p2); p2.setTeam(t1);
t1.addPlayer(p3); p3.setTeam(t1);
t2.addPlayer(p4); p4.setTeam(t2);
t2.addPlayer(p5); p5.setTeam(t2);
t2.addPlayer(p6); p6.setTeam(t2);
// Add Pitchers to Teams
t1.addPlayer(p7); p7.setTeam(t1);
t2.addPlayer(p8); p8.setTeam(t2);
Blog referente al grupo de investigación y desarrollo (MHProject) realizado en la Universidad Pública de Navarra por más de 12 personas. El proyecto se basa en el desarrollo e investigación de aplicaciones y sistemas para Televisión Digital Interactiva desarroladas sobre Java y basadas en el estandard abierto MHP (Multimedia Home Platform).
Hola que tal soy Alejandro Fanjul, webmaster y fundador de MHProject, para cualquier consulta por favor dirigirse a: alex.fanjul@mhproject.org
Lun | Mar | Mie | Jue | Vie | Sab | Dom |
---|---|---|---|---|---|---|
<< < | ||||||
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 |
Documento Introducción a la Televisión DigitalPresentacion Introducción a la Television DigitalCompresión Vídeo standard MPEG2Compresión Imagen standard JPEGSDI: Señal de Vídeo en estudiosSistemas Vídeo Digital - DVBTelevisión Digital Interactiva - MHP
]más
Palbin es un Servicio que te permite crear tu Tienda Online fácil y rápidamente, y vender por Internet sin necesidad de conocimientos técnicos.
¿Qué ventajas tiene?
]más
Enlace a web con applet incrustado. Necesario aceptar la firma digital.
]más
]más
Esta obra está bajo una licencia de Creative Commons.