Java 数据对象(JDO)介绍(三)
2008-01-05 19:21:38 来源:WEB开发网核心提示:简介下面的例子示范了JDO的基础应用,代码示范了如何向数据库添加数据和取回数据,Java 数据对象(JDO)介绍(三),所有的代码使用的都是SolarMetric's Kodo JDO实现,包括创建数据库的机制,使域对象类扩展PersistenceCapable接口的增强和厂商特定的PersistenceMan
简介
下面的例子示范了JDO的基础应用。代码示范了如何向数据库添加数据和取回数据。所有的代码使用的都是SolarMetric's Kodo JDO实现。包括创建数据库的机制,使域对象类扩展PersistenceCapable接口的增强和厂商特定的PersistenceManagerFactory实现使用的都是Kodo JDO实现。其他的部分使用的是标准的JDO实现。
域对象
The samples will work with a small set of classes, which rePResent a Fleet of Vehicle objects. The two specific types of Vehicles defined are Bicycle and MotorVehicle. MotorVehicle objects have an Engine attribute.
例子是用了一个很小的类,这个类描述了一个Vehicle(交通工具) 对象的Fleet(车队)。Vehicle 定义了两个特定的对象Bicycle 和MotorVehicle 。MotorVehicle对象有一个Engine 属性。
域对象源代码
/**
* Fleet.java
*/
package com.ociweb.jdodemo;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
public class Fleet {
private List vehicles = new Vector();
public void addVehicle(Vehicle vehicle) {
vehicles.add(vehicle);
}
public Iterator getVehicles() {
return vehicles.iterator();
}
public String toString() {
StringBuffer buffer = new StringBuffer("Fleet:\n");
Iterator iter = getVehicles();
while (iter.hasNext()) {
buffer.append("\t" + iter.next() + "\n");
}
return buffer.toString();
}
}
/**
* Vehicle.java
*/
package com.ociweb.jdodemo;
public class Vehicle {
private int numberOfWheels;
public Vehicle(int numberOfWheels) {
this.numberOfWheels = numberOfWheels;
}
public int getNumberOfWheels() {
return numberOfWheels;
}
}
/**
* Bicycle.java
*/
package com.ociweb.jdodemo;
public class Bicycle extends Vehicle {
private String model;
public Bicycle(String model) {
super(2);
this.model = model;
}
public String toString() {
return "Bike: Model " + model;
}
}
/**
* MotorVehicle.java
*/
package com.ociweb.jdodemo;
public class MotorVehicle extends Vehicle {
private Engine engine;
public MotorVehicle(int numberOfWheels, Engine engine) {
super(numberOfWheels);
this.engine = engine;
}
public String toString() {
return "MotorVehicle With " + getNumberOfWheels()
+ " Wheels. " + engine;
}
}
/**
* Engine.java
*/
package com.ociweb.jdodemo;
public class Engine {
private int numberOfCylinders;
public Engine(int numberOfCylinders) {
this.numberOfCylinders = numberOfCylinders;
}
public int getNumberOfCylinders() {
return numberOfCylinders;
更多精彩
赞助商链接