package oracle.otnsamples.sqlj.optimization; import java.sql.SQLException;
import oracle.sqlj.runtime.Oracle; import sqlj.runtime.ref.DefaultContext;
public class OptimizeSample{ OptimizeSampleFrame gui; private String quantity = null; long orderID = 0; private int customerID = 0; private String orderDate = null; private int cacheSize = 3;
public OptimizeSample() { try{ DefaultContext.setDefaultStmtCacheSize(cacheSize); gui = new OptimizeSampleFrame(this); } catch(Exception ex){ System.out.println(" Error : Couldn't Initialize frame: "+ex.toString()); System.exit(1); } }
public static void main(String args[]){ OptimizeSample optimizeSample = new OptimizeSample(); optimizeSample.dbConnect(); optimizeSample.displayOrders(); }
private void dbConnect(){ try{ gui.putStatus(" Connecting to Database ...."); Oracle.connect(this.getClass(), "../../../../Connection.properties"); gui.putStatus(" Connected to Database ..."); } catch(SQLException ex){ System.out.println("Error while Connecting to the database: "
+ ex.toString()); System.exit(1); } }
public void dispatchEvent (String eventName) { if (eventName.equals(" Exit ")){ exitApplication(); } else if (eventName.equals(" Show Order Item Details ")){ showOrderItems(); } else if (eventName.equals(" Show Details For Update ")){ showOrderItemsForUpdate(); } else if (eventName.equals(" Update Quantity ")){ updateQuantity(); } else if (eventName.equals(" Refresh ")){ gui.orderTableModel.clearTable(); gui.orderTablePane.repaint(); displayOrders(); } }
private void displayOrders() { long orderID = 0; int customerID = 0; long initialTime = 0; long finalTime = 0; long orderTotal = 0;
DefaultContext.setDefaultStmtCacheSize(cacheSize);
objectIter tempIter=null;
try {
while(tempIter.next()){ gui.addOrdersToJTable(new String(tempIter.order_ID()+""), new String(tempIter.customer_ID()+""),tempIter.order_date() ); } tempIter.close();
} catch(SQLException ex) { gui.putStatus("Error while Displaying all Orders: "+ex.toString()); } finally { try { tempIter.close(); } catch(SQLException sqe){ gui.putStatus("Error while closing Iterator : "+sqe.toString()); } } }
private void updateQuantity() { int qty = Integer.parseInt(gui.txtQuantity.getText()); long orderID = Long.parseLong(gui.txtOrderID.getText()); int productID = Integer.parseInt(gui.txtProductID.getText());
try{
if(!(qty== Integer.parseInt(quantity))){
} } catch(SQLException sqe){ gui.putStatus("Error while Updating Quantity : "+sqe.toString()); } }
private void showOrderItemsForUpdate(){ int selectedRow = gui.orderItemsTable.getSelectedRow(); if (selectedRow==-1) { gui.putStatus(" Select an Order from the Table ..."); return; }
String productID = (String)gui.orderItemsTableModel.getValueAt(selectedRow,1);
String qty = (String)gui.orderItemsTableModel.getValueAt(selectedRow,2);
this.quantity = qty; gui.setLabelText("Update Order Items for OrderID = "+orderID); gui.showOrderItemInfo(new String(orderID+""), productID, quantity); }
int getOrderID(){ int selectedRow = gui.orderTable.getSelectedRow();
if(selectedRow==-1) { gui.putStatus(" Select an Order from the Table ..."); return -1; } int orderID = Integer.parseInt((String) gui.orderTableModel.getValueAt(selectedRow,0)); return orderID; }
private void showOrderItems(){ int selectedRow = gui.orderTable.getSelectedRow(); orderDate = (String)gui.orderTableModel.getValueAt(selectedRow,2); customerID = Integer.parseInt((String) gui.orderTableModel.getValueAt(selectedRow,1)); orderID = getOrderID(); if(orderID==-1) return; int orderItemID = 0; int productID = 0; int quantity = 0;
objectIter tempIter = null;
try { while(tempIter.next()){ gui.addOrderItemsToJTable((tempIter.line_item_id()+""), (tempIter.product_Id()+""),(tempIter.quantity()+"")); } gui.setLabelText(" Order Items for OrderID = "+orderID); } catch(SQLException ex) { gui.putStatus("Error while Displaying Order Items: "+ex.toString()); } finally{ try{ tempIter.close(); } catch(SQLException sqe){ gui.putStatus("Error while closing Iterator: "+sqe.toString()); } } gui.putStatus(" Select an Order Item..."); }
private void exitApplication(){ try { Oracle.close(); } catch (SQLException ex){ gui.putStatus(" Error while Closing the connection: "+ex.toString()); } System.exit(0); } }
|