import java.util.*;
import org.exolab.castor.jdo.*;
import java.net.*;
public class CustomerManager {
JDO jdo;
Database db;
public CustomerManager() throws DatabaseNotFoundException,
PersistenceException {
//定义一个JDO对象
jdo = new JDO();
jdo.setDatabaseName("CustomerDemo");
jdo.setConfiguration("database.xml");
jdo.setClassLoader(getClass().getClassLoader());
//获得连接数据库
db = jdo.getDatabase();
}
/**
* 用于读取用户
* @param id Customer 对象的主键
*/
public Customer loadCustomer(Integer id) throws DatabaseNotFoundException,
PersistenceException {
Customer result = null;
//开始事务
db.begin();
result = (Customer) db.load(Customer.class, id);
//完成事务,关闭数据库
db.commit();
db.close();
return result;
}
/**
* 用于建立用户
* @param Customer newCustomer 新对象
*/
public void createCustomer(Customer newCustomer) throws
DatabaseNotFoundException,
PersistenceException {
Customer result = null;
db.begin();
//新建Customer
db.create(newCustomer);
db.commit();
db.close();
}
/**
* 更新旧的对象
*/
public Customer updateCustomer(Customer updateCustomer) throws
DatabaseNotFoundException,
PersistenceException {
db.begin();
//更新Customer
db.update(updateCustomer);
db.commit();
db.close();
return null;
}
public void removeCustomer(Customer removeCustomer) throws
DatabaseNotFoundException,
PersistenceException {
db.begin();
//删除Customer
db.remove(removeCustomer);
db.commit();
db.close();
}
} |