brokermodeldbimpl.java

来自「java写的股票交易系统」· Java 代码 · 共 816 行 · 第 1/2 页

JAVA
816
字号
          " not found");
      }
      //** 4 Assign con by invoking the obtainConnection()
      //**   for example see similar step in ssnExists method
      con = obtainConnection();
      //** 5 Assign stmt by invoking createStatement on con
      //**   for example see similar step in ssnExists method
      stmt = con.createStatement();
      // The following creates the required INSERT SQL string
      request = 
        "UPDATE Customer SET "
        + " cust_name=" + "'" + name + "'" + ","
        + " address=" +"'" + addr + "'"
        + " WHERE ssn=" +"'" + id + "'";
      //** 6 Assign rowCount with the value returned by invoking 
      //**   executeUpdate(request) method on the stmt object
      rowCount = stmt.executeUpdate(request);
      //** 7 Invoke the fireModelChangeEvent with cust as param.
      //**   Q? why is this step necessary?
      fireModelChangeEvent(cust);
    } catch (Exception e) {
      e.printStackTrace();
      throw new BrokerException
        ("BrokerDbImpl.updateCustomer\n" +e);
    }
  }

  
// Customer segment state query methods
  /**-------------------------------------------------------------
   * Given an id, returns the Customer from the model
   */
  //modified  by  ourteam 051228
  //begin
  public Customer getCustomer(String id,String name,String addr)
    throws BrokerException {
    try {
      //modified by ourteam 051228
      //begin 
      //String name;
      //String addr;
      //end
      Connection con;
      Statement stmt;
      ResultSet result = null;
      String request;
      Customer cr = null;
      //** 1 Assign con by invoking the obtainConnection()
      //**   for example see similar step in ssnExists method
      con = obtainConnection();
      //** 2 Assign stmt by invoking createStatement on con
      //**   for example see similar step in ssnExists method
      stmt = con.createStatement();
      // The following creates the required SELECT SQL string
      //modified  by ourteam 051228
      //begin 
      request = 
        "SELECT ssn, cust_name, address FROM Customer "
         + " WHERE ssn like '" +id.trim()+ "%' and cust_name like '" +name.trim()
         +"%' and address like '"+addr.trim()+"%'";
      //end
      //** 3 Assign result with the value returned by invoking 
      //**   executeQuery(request) method on the stmt object 
      System.out.println(request);
      result = stmt.executeQuery(request);

      // The following statement checks if query successful
      if (result.next()) {
        //** 4 Assign name with the value returned by invoking 
        //**   getString(1) method on the result object
        //modified by ourteam 051228
        //begin
        id= result.getString(1);//end
        name = result.getString(2);
        //** 5 Assign addr with the value returned by invoking 
        //**   getString(2) method on the result object
        addr = result.getString(3);
        //** 6 Create a Customer object using (id, name, addr) 
        //**   and assign this object to cr
        cr = new Customer(id, name, addr);
      }
      else {
        // if query failed 
        throw new BrokerException("Record for " + id +
          " not found");
      }        
      // return cr
      return cr;
    } catch (SQLException e) {
      e.printStackTrace();
      throw new BrokerException("BrokerDbImpl.getCustomer\n" +e);
    }
  }
  
  //modified  by  ourteam 051228
  //begin
    public Portfolio getPortfolio(String id,String name,Share[] shares)
    throws BrokerException {
    try {
      String strID,strName,strAddr;
      strID="";strName="";strAddr="";
      Connection con;
      Statement stmt;
      ResultSet result = null;
      String request;
      Portfolio cr = null;
      //** 1 Assign con by invoking the obtainConnection()
      //**   for example see similar step in ssnExists method
      con = obtainConnection();
      //** 2 Assign stmt by invoking createStatement on con
      //**   for example see similar step in ssnExists method
      stmt = con.createStatement();
      // The following creates the required SELECT SQL string
      //modified  by  ourteam 051228
      //begin 
      request = "SELECT ssn,cust_name,address FROM Customer WHERE "
         + " ssn like '" +id.trim()+ "%' and cust_name like '" +name.trim() +"%'";
      //end
      //** 3 Assign result with the value returned by invoking 
      //**   executeQuery(request) method on the stmt object 
      System.out.println(request);
      result = stmt.executeQuery(request);

      // The following statement checks if query successful
      if (result.next()) {
        //** 4 Assign name with the value returned by invoking 
        //**   getString(1) method on the result object
        //modified by ourteam 051228
        //begin
        strID = result.getString(1);
        strName = result.getString(2);
        strAddr = result.getString(3);
        //** 5 Assign addr with the value returned by invoking 
        //**   getString(2) method on the result object

        //** 6 Create a Customer object using (id, name, addr) 
        //**   and assign this object to cr
        //modified by ourteam 051228
        //begin
        //get shares object array
        request = "SELECT Shares.symbol,quantity,price FROM Shares, Stock WHERE "
        			+ " Shares.ssn like '" +strID.trim()+ "%' "
        			+ " AND Shares.symbol=Stock.symbol";
      	System.out.println(request);
      	ArrayList alShares=new ArrayList();
      	Stock stock=null;
      	result = stmt.executeQuery(request);
      	while(result.next()){
      		stock=new Stock(result.getString(1),Float.parseFloat(result.getString(3)));
      		alShares.add(new Share(stock,Integer.parseInt(result.getString(2))));
      	}
      	
        cr = new Portfolio(new Customer(strID,strName,strAddr), alShares);
      }
      else {
        // if query failed 
        throw new BrokerException("Record for " + id +
          " not found");
      }
      // return cr
      return cr;
    } catch (SQLException e) {
      e.printStackTrace();
      throw new BrokerException("BrokerDbImpl.getPortfolio()\n" +e);
    }
  }
  
  //end

  /**-------------------------------------------------------------
   * Returns all customers in the broker model
   */
  public Customer[] getAllCustomers()
    throws BrokerException{
    String id;
    String name;
    String addr;
    Connection con;
    Statement stmt;
    ResultSet result = null;
    String request;
    Customer cr = null;
    Customer[] all;
    Customer[] temp = new Customer[1];
    ArrayList aList = new ArrayList(1);
    try {
      //** 1 assign con by invoking the obtainConnection()
      //**   for example see similar step in ssnExists method
      con = obtainConnection();
      //** 2 assign stmt by invoking createStatement on con
      //**   for example see similar step in ssnExists method
      stmt = con.createStatement();
      //** the following creates the required SELECT SQL string
      request = "SELECT * FROM CUSTOMER";
      //** 3 assign result with the value returned by invoking 
      //**   executeQuery(request) method on the stmt object
      result = stmt.executeQuery(request);
      while (result.next()) {
        //A* 4 assign id with the value returned by invoking 
        //**   getString(1) method on the result object
        id = result.getString(1);
        //A* 5 assign name with the value returned by invoking 
        //**   getString(2) method on the result object
        name = result.getString(2);
        //A* 6 assign addr with the value returned by invoking 
        //**   getString(3) method on the result object
        addr = result.getString(3);
        //A* 7 create a Customer object using (id, name, addr) 
        //**   and assign this object to cr
        cr = new Customer(id, name, addr);
        //A* 8 use the ArrayList add method to add cr to aList
        aList.add(cr);
      }
      if (aList.size() > 0) {
        //Q? for an explanation of the following line of code
        //** lookup javadoc for 
        all = (Customer[]) aList.toArray(temp);
      }
      else {
        all = null;
      }
      return all;
    } catch (SQLException e) {
      all = null;
      e.printStackTrace();
      throw new BrokerException
        ("BrokerDbImpl.getAllCustomers\n" + e);
    }
  }
  
  public void test() {
    try {  
      // getCustomer
      Customer cust = this.getCustomer("111-11-1111","Test Customer","++++++++++++++");
      System.out.println("testing getCustomer:\n" +
        "ssn 111-11-1111 " + cust);

      // updateCustomer - change addr for "111-11-1111"
      System.out.println("testing updateCustomer ");
      String addrOrig = cust.getAddr();
      cust.setAddr("++++++++++++++");
      this.updateCustomer(cust);
      cust = this.getCustomer("111-11-1111","Test Customer","++++++++++++++");
      System.out.println("after update:\nssn 111-11-1111 " 
        + cust);

      // updateCustomer - restore addr for "111-11-1111"
      System.out.println("restoring to before updateCustomer ");
      cust.setAddr(addrOrig);
      this.updateCustomer(cust);
      cust = this.getCustomer("111-11-1111","Test Customer","++++++++++++++");
      System.out.println("back to original:\nssn 111-11-1111 " 
        + cust);

      // getAllCustomers
      Customer[] custs = this.getAllCustomers();
      for (int i=0; i<custs.length; i++) {
        System.out.println("custs[" + i + "] " + custs[i].getName());
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  
  //modified  by ourteam 051228
  //begin
  public Stock getStock(String symbol, String price)
    throws BrokerException {
    	//add
    	try {
     
      Connection con;
      Statement stmt;
      ResultSet result = null;
      String request;
      Stock cr = null;
      //** 1 Assign con by invoking the obtainConnection()
      //**   for example see similar step in ssnExists method
      con = obtainConnection();
      //** 2 Assign stmt by invoking createStatement on con
      //**   for example see similar step in ssnExists method
      stmt = con.createStatement();
      // The following creates the required SELECT SQL string
      //modified  by ourteam 051228
      //begin 
      String strCond="";
      
      //if (symbol==null || symbol.trim().equals("")) symbol = "0";
      if (price==null || price.trim().equals(""))
        strCond = "";
      else
      	strCond = " and price="+price.trim();

      request = 
        "SELECT symbol, price FROM  stock "
         + " WHERE symbol like '" +symbol.trim()+ "%'" + strCond;
      
      System.out.println(request);
      result = stmt.executeQuery(request);

      // The following statement checks if query successful
      if (result.next()) {
        //** 4 Assign name with the value returned by invoking 
        //**   getString(1) method on the result object
        //modified by ourteam 051228
        //begin
        symbol= result.getString(1);//end
        price = result.getString(2);
        //** 5 Assign addr with the value returned by invoking 
        //**   getString(2) method on the result object
       
        //** 6 Create a Customer object using (id, name, addr) 
        //**   and assign this object to cr
        cr = new Stock(symbol, Float.parseFloat(price));
      }
      else {
        // if query failed 
        throw new BrokerException("Record for " + symbol +
          " not found");
      }        
      // return cr
      return cr;
    } catch (SQLException e) {
      e.printStackTrace();
      throw new BrokerException("BrokerDbImpl.getStock\n" +e);
    }
    	
  }
  //end
  
  //modified  by ourteam 051228
  //begin
   public Stock[] getAllStocks()
    throws BrokerException{
    String symbol;
    String price;
    //String addr;
    Connection con;
    Statement stmt;
    ResultSet result = null;
    String request;
    Stock cr = null;
    Stock[] all;
    Stock[] temp = new Stock[1];
    ArrayList aList = new ArrayList(1);
    try {
      //** 1 assign con by invoking the obtainConnection()
      //**   for example see similar step in ssnExists method
      con = obtainConnection();
      //** 2 assign stmt by invoking createStatement on con
      //**   for example see similar step in ssnExists method
      stmt = con.createStatement();
      //** the following creates the required SELECT SQL string
      request = "SELECT * FROM STOCK";
      //** 3 assign result with the value returned by invoking 
      //**   executeQuery(request) method on the stmt object
      result = stmt.executeQuery(request);
      while (result.next()) {
        //A* 4 assign id with the value returned by invoking 
        //**   getString(1) method on the result object
        symbol = result.getString(1);
        //A* 5 assign name with the value returned by invoking 
        //**   getString(2) method on the result object
        price = result.getString(2);
        //A* 6 assign addr with the value returned by invoking 
        //**   getString(3) method on the result object
        //addr = result.getString(3);
        //A* 7 create a Customer object using (id, name, addr) 
        //**   and assign this object to cr
        cr = new Stock(symbol,Float.parseFloat(price));
        //A* 8 use the ArrayList add method to add cr to aList
        aList.add(cr);
      }
      if (aList.size() > 0) {
        //Q? for an explanation of the following line of code
        //** lookup javadoc for 
        all = (Stock[]) aList.toArray(temp);
      }
      else {
        all = null;
      }
      return all;
    } catch (SQLException e) {
      all = null;
      e.printStackTrace();
      throw new BrokerException
        ("BrokerDbImpl.getAllStocks\n" + e);
    }
  }
  //end
  
    public static void main(String args[]) {
    String hostName = null;
    if (args.length < 1) {
       hostName = "localhost";
    }
    else {
      hostName = args[0];
    }
    try {
      BrokerModelDbImpl brokerDb=new BrokerModelDbImpl(hostName);
      brokerDb.test();
    } catch(Exception e) {
      System.out.println(e.toString());
    }
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?