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
 

"IIOP Explained"
Volume: 3 Issue: 9, p. 50

	

Listing 1.
 
CREATE TABLE Customer ( 
CustomerId INTEGER NOT NULL PRIMARY KEY, 
Name VARCHAR(32), 
Address VARCHAR(100) 
) 

CREATE TABLE Orders ( 
OrderId INTEGER NOT NULL PRIMARY KEY, 
CustomerId INTEGER NULL REFERENCES Customer, 
Item VARCHAR(20), 
UnitPrice NUMERIC, 
Quantity INTEGER 
) 

Listing 2.
 
// Customer class 
public class Customer implements PersistenceCapable { 
private int customerid; 
private String name; 
private String address; 
private DCollection ordersForCustomer; 

// Accessor methods 
public int getCustomerid() { 
return customerid; 
} 
public void setCustomerid(int customerid) { 
this.customerid = customerid; 
} 

public String getName() { 
return name; 
} 
public void setName(String name) { 
this.name = name; 
} 

public String getAddress() { 
return address; 
} 
public void setAddress(String address) { 
this.address = address; 
} 

public Iterator getOrdersForCustomer() { 
return ordersForCustomer.iterator(); 
} 
public void addOrdersForCustomer(OrdersordersForCustomer) { 
this.ordersForCustomer.add(ordersForCustomer); 
} 
public void removeOrdersForCustomer(OrdersordersForCustomer) { 
this.ordersForCustomer.remove(ordersForCustomer); 
} 

// Constructors 
public Customer (Database db, int p_customerid) { 
this.customerid = p_customerid; 
} 
} 

// Orders class 
public class Orders implements PersistenceCapable { 
private int orderid; 
private String item; 
private long unitprice; 
private int quantity; 
private Customer customerForOrders; 

// Accessor methods 
public int getOrderid() { 
return orderid; 
} 
public void setOrderid(int orderid) { 
this.orderid = orderid; 
} 

public String getItem() { 
return item; 
} 
public void setItem(String item) { 
this.item = item; 
} 

public long getUnitprice() { 
return unitprice; 
} 
public void setUnitprice(long unitprice) { 
this.unitprice = unitprice; 
} 

public int getQuantity() { 
return quantity; 
} 
public void setQuantity(int quantity) { 
this.quantity = quantity; 
} 

public Customer getCustomerForOrders() { 
return customerForOrders; 
} 
public void setCustomerForOrders(CustomercustomerForOrders) { 
this.customerForOrders = customerForOrders; 
} 

// Constructors 
public Orders (Database db, int p_orderid) { 
this.orderid = p_orderid; 
} 
} 

Listing 3.
 
public void addOrdersForCustomer(Orders ordersForCustomer) { 
// check the credit history before adding the order 
if ( CreditHistory.getCreditHistory(this) == CreditHistory.BAD){ 
System.out.println("Sorry, bad credit history"); 
return; 
} 
// customer has a good credit history 
this.ordersForCustomer.add(ordersForCustomer); 
} 

Listing 4.
 
// open the database 
Database db = Database.open("jdbc:microsoft://thestork:1433/orderentry", 
"scott", "tiger"); 

// start a transaction to create a new Customer 

Transaction t = new Transaction(); 
t.begin(Transaction.OPTIMISTIC); 
Customer c = new Customer(db, 1000); 
c.setName("Foor Bar"); 
c.setAddress("901 San Antonio Road, Palo 
Alto CA-94303"); 
// do some more stuff like create orders for this customer etc.. 
try { 
t.commit(); 
} catch (RetryException re) { 
System.out.println( "The transaction did not go through. Try 
again."); 
} 

// close the database 
db.close();
  
      
 

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.