HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Strategies for Storing Java Objects in Relational Databases"
Vol. 6, Issue 8, p. 70

	


Listing 1: Embedded SQL Code in the Order Class

    /**
     * Save an order and its aggregate order Items
     */
    private void save(Connection connection) throws SQLException {


        PreparedStatement orderStatement = null;
        PreparedStatement orderItemStatement = null;
        Vector items;


        //  Build statements to either insert or update
        if (isPersistent() ) {
            orderStatement = connection.prepareStatement( "Update INTO Order VALUES(?,?,?,?,?,?,?)");
            orderItemStatement = connection.prepareStatement(OrderItem.getUpdateSQL());
        } else {
            orderStatement = connection.prepareStatement( "INSERT INTO Order VALUES(?,?,?,?,?,?,?)");
            orderItemStatement = connection.prepareStatement(OrderItem.getInsertSQL());
        }


   //  Add the order information from this object
        orderStatement.setInt(1, getOrderID());
        orderStatement.setInt(2, getCustomerNumber());
        orderStatement.setDate(3, getOrderDate());
        orderStatement.setDouble(4, getFederalTax().getNumber());
        orderStatement.setDouble(5, getStateTax().getNumber());
        orderStatement.setDouble(6, getLocalTax().getNumber());
        orderStatement.setDouble(7, getSubtotal().getNumber());


        //  Save the order and order contact information
        orderStatement.executeUpdate();
        orderStatement.close();


        //  Save the order items
        items = getOrderItems();
        for ( int i = 1; i <= items.size(); i++ ) {
            OrderItem item = (OrderItem) items.elementAt(i);
            item.save(orderItemStatement, getOrderID(), i);
        }
        orderItemStatement.close();


    }
    /**
     * Refreshes this object with the data for
     * the order with the given id.
     */
    public void retrieve(Connection connection)
        throws SQLException
    {
        OrderContact ship, bill;
        int shipToID, billToID;


        // Retrieve the record from the Order table


        PreparedStatement statement =
        connection.prepareStatement("SELECT * FROM Order WHERE OrderID = ?");
        statement.setInt(getOrderID());
        ResultSet rs = statement.executeUpdate();
        rs.next();


        setCustomerNumber(rs.getInt(2));
        setOrderDate(rs.getDate(3));
        setFederalTax(rs.getDouble(4));
        setStateTax(rs.getDouble(5));
        setLocalTax(rs.getDouble(6));
        setSubtotal(rs.getDouble(7));
        setIsPersistent(true);


        // Read the records from the OrderItem table
        try {
            Vector items = OrderItem.retrieveOrderItems(orderID);
            setOrderItems(items);
        } catch ( Exception ex ) {
            //  Handle the exception appropriately...
        }
    }


    /**
     * Delete the order and Its order Items
     */
    private void delete(Connection connection)
    throws SQLException {


        PreparedStatement ps = null;
        Vector items;


   ps = connection.prepareStatement( "DELETE Order WHERE OrderID = ?");


        ps.setInt(1, getOrderID());
        ps.executeUpdate();
        ps.close();


        //  Add deletion of the order items to the transaction
        items = getOrderItems();


        for ( int i = 1; i <= items.size(); i++ ) {
            OrderItem item = (OrderItem) items.elementAt(i);
            ps = item.delete(connection);
            ps.executeUpdate();
        }
    }



  Listing 2: Delegating Persistence of Order to OrderData
 
  /**
     * Save an order and its aggregate order Items
     */
    private void save(Connection connection) throws SQLException {
        OrderData.save(this, connection);
    }


    /**
     * Refreshes this object with the data for
     * the order with the given id.
     */
    public void retrieve(Connection connection)
        throws SQLException
    {
        OrderData.retrieve(this, connection);
    }


    /**
     * Delete the order Its order Items
     */
    private void delete(Connection connection)
    throws SQLException {
        OrderData.delete(this, connection);
    }

  
 
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.