例程16-1
/**
* 保存一个未付清的订单和聚集对象
*/
private void saveOutstandingOrder(Connection connection) throws SQLException
{
PreparedStatement orderStatement = null;
PreparedStatement orderItemStatement = null;
Vector items;
// 编写插入或更新语句
if (isReadFromOutstanding() )
{
orderStatement = buildOutstandingUpdateStatement(connection);
orderItemStatement = connection.prepareStatement(OrderItem.getOutstandingUpdateSQL());
}
else
{
orderStatement = buildOutstandingInsertStatement(connection);
orderItemStatement = connection.prepareStatement(OrderItem.getOutstandingInsertSQL());
}
// 保存订单和订单联系信息
orderStatement.executeUpdate();
orderStatement.close();
// 保存未付清的订单项到事务
items = getOrderItems();
for ( int i = 1; i <= items.size(); i++ )
{
OrderItem item = (OrderItem) items.elementAt(i);
item.saveOrderItem(orderItemStatement, getOrderID(), i);
}
orderItemStatement.close();
}
/**
* 创建插入订单到OutstandingOrder表的语句
*/
public PreparedStatement buildOutstandingInsertStatement(Connection connection)
throws SQLException
{
PreparedStatement statement =
connection.prepareStatement("INSERT INTO OutstandingOrder VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
OrderContact ship = this.getShipTo();
OrderContact bill = this.getBillTo();
// 从对象添加订单信息
statement.setInt(1, getOrderID());
statement.setInt(2, getCustomerNumber());
statement.setDate(3, getOrderDate());
statement.setDouble(4, getFederalTax().getNumber());
statement.setDouble(5, getStateTax().getNumber());
statement.setDouble(6, getLocalTax().getNumber());
statement.setDouble(7, getSubtotal().getNumber());
// 为正在出货的订单添加联系信息
statement.setString(8, ship.getContactName());
statement.setString(9, ship.getStreet());
statement.setString(10, ship.getCity());
statement.setString(11, ship.getState());
statement.setString(12, ship.getCountry());
statement.setString(13, ship.getZip());
statement.setString(14, ship.getPhoneNumber());
// 为付账了的订单添加联系信息
statement.setString(15, bill.getContactName());
statement.setString(16, bill.getStreet());
statement.setString(17, bill.getCity());
statement.setString(18, bill.getState());
statement.setString(19, bill.getCountry());
statement.setString(20, bill.getZip());
statement.setString(21, bill.getPhoneNumber());
return statement;
}
例程16-2
/**
* 当订单未付清时,应答代表插入订单项的SQL语句串
*/
static public String getOutstandingInsertSQL()
{
return "INSERT INTO OutstandingOrderItem VALUES(?,?,?,?,?)";
}
/**
* 当订单完成时,应答代表插入订单项的SQL语句串
*/
static public String getFulfilledInsertSQL()
{
return "INSERT INTO FulfilledOrderItem VALUES(?,?,?,?,?)";
}
/**
* 保存订单项到合适的表
*
* @param PreparedStatement statement 插入订单项到正确的表的SQL语句
* @param int orderID 订单的独一无二的标识符
* @param in sequenceNumber 订单项出现在订单上的位置
*/
public void saveOrderItem(
PreparedStatement statement,
int orderID,
int sequenceNumber)
throws SQLException
{
statement.setInt(1, orderID);
statement.setInt(2, sequenceNumber);
statement.setInt(3, getItemNumber());
statement.setInt(4, getNumberOrdered());
statement.setFloat(5, getSubtotal());
statement.executeUpdate();
}
例程16-3
/**
* 完成订购
*
* @param orderID 标识要完成的订购
* @param date 订单标识未完成的数据
*/
public void fulfillOrder(int orderID, java.sql.Date date)
throws RemoteException
{
try
{
// 检索现有的未付清订单
retrieveOutstandingOrder(orderID);
// 完成订单
setFulfilledDate(date);
fulfillOrder();
}
catch ( Exception ex )
{
throw new RemoteException(ex.getMessage());
}
}
/**
* 完成此订单
*/
protected void fulfillOrder() throws RemoteException
{
UserTransaction transaction = null;
Connection connection = null;
try
{
connection = getConnection();
// 开始一个事务
transaction = getTransaction();
transaction.begin();
// 增加对未付清订单的删除
deleteOutstandingOrder(connection);
// 增加对完成订单的插入
saveFulfilledOrder(connection);
// 提交事务
transaction.commit();
}
catch ( Exception ex )
{
try
{
if ( transaction != null )
transaction.rollback();
}
catch ( Exception exp )
{
}
throw new RemoteException(ex.getMessage());
}
finally
{
cleanup(connection, null);
}
}
/**
* 用所给id的未付清订单的数据刷新对象
*/
public void retrieveOutstandingOrder(int orderID)
throws SQLException
{
OrderContact ship, bill;
int shipToID, billToID;
// 从OutstandingOrder表获取记录
Connection connection = getConnection();
PreparedStatement statement =
connection.prepareStatement("SELECT * FROM OutstandingOrder WHERE OrderID = ?");
statement.setInt(orderID);
ResultSet rs = statement.executeUpdate();
rs.next();
setOrderID(orderID);
setCustomerNumber(rs.getInt(1));
setOrderDate(rs.getDate(2));
setFederalTax(rs.getDouble(3));
setStateTax(rs.getDouble(4));
setLocalTax(rs.getDouble(5));
setSubtotal(rs.getDouble(6));
// 为出货和付款的订单添加联系信息
ship = new OrderContact();
ship.setContactName(rs.getString(8));
ship.setStreet(rs.getString(9));
ship.setCity(rs.getString(10));
ship.setState(rs.getString(11));
ship.setCountry(rs.getString(12));
ship.setZip(rs.getString(13));
ship.setPhoneNumber(rs.getString(14));
setShipTo(ship);
bill = new OrderContact();
bill.setContactName(rs.getString(15));
bill.setStreet(rs.getString(16));
bill.setCity(rs.getString(17));
bill.setState(rs.getString(18));
bill.setCountry(rs.getString(19));
bill.setZip(rs.getString(20));
bill.setPhoneNumber(rs.getString(21));
cleanup(connection, statement);
// 从OutstandingOrder表读取记录
try
{
Vector items = OrderItem.retrieveOutstandingOrderItems(orderID);
setOrderItems(items);
}
catch ( Exception ex )
{
// 处理异常
}
}
/**
* 删除未付清的订单
*/
private void deleteOutstandingOrder(Connection connection)
throws SQLException {
PreparedStatement ps = null;
Vector items;
// 增加对未付清订单事务的删除
ps = buildOutstandingDeleteStatement(connection);
ps.executeUpdate();
ps.close();
// 增加对未付清订单项事务的删除
items = getOrderItems();
for ( int i = 1; i <= items.size(); i++ )
{
OrderItem item = (O