src/dao/custom/impl/DataLayer.java
public static boolean placeOrder(OrderTM order, List<OrderDetailTM> orderDetails){
    Connection connection = DBConnection.getInstance().getConnection();
    try {
      connection.setAutoCommit(false);
      PreparedStatement pstm = connection.prepareStatement("INSERT INTO `Order` VALUES (?,?,?)");
      pstm.setObject(1, order.getOrderId());
      pstm.setObject(2, order.getOrderDate());
      pstm.setObject(3, order.getCustomerId());
      int affectedRows = pstm.executeUpdate();

      if (affectedRows == 0) {
        connection.rollback();
        return false;
      }

      for (OrderDetailTM orderDetail: orderDetails) {
        pstm = connection.prepareStatement("INSERT INTO OrderDetail VALUES (?,?,?,?)");
        pstm.setObject(1, order.getOrderId());
        pstm.setObject(1, orderDetail.getCode());
        pstm.setObject(1, orderDetail.getQty());
        pstm.setObject(1, orderDetail.getUnitPrice());
        affectedRows = pstm.executeUpdate();

        if (affectedRows == 0){
          connection.rollback();
          return false;
        }

        pstm = connection.prepareStatement("UPDATE Item SET qtyOnHand = qtyOnHand - ? WHERE code= ?");
        pstm.setObject(1, orderDetail.getQty());;
        affectedRows = pstm.executeUpdate();

        if (affectedRows == 0){
          connection.rollback();
          return false;
        }
      }
      connection.commit();
      return true;
    } catch (SQLException throwables) {
      throwables.printStackTrace();
      try {
        connection.rollback();
      } catch (SQLException e) {
        e.printStackTrace();
      }
      return false;
    } finally {
      try {
        connection.setAutoCommit(true);
      } catch (SQLException throwables) {
        throwables.printStackTrace();
      }
    }
  }
//this method goes to business layer
this method goes to business layer
this method goes to business layer
public static boolean placeOrder(OrderTM order, List<OrderDetailTM> orderDetails){
    Connection connection = DBConnection.getInstance().getConnection();
    try {
      connection.setAutoCommit(false);
      PreparedStatement pstm = connection.prepareStatement("INSERT INTO `Order` VALUES (?,?,?)");
      pstm.setObject(1, order.getOrderId());
      pstm.setObject(2, order.getOrderDate());
      pstm.setObject(3, order.getCustomerId());
      int affectedRows = pstm.executeUpdate();
      if (affectedRows == 0) {
        connection.rollback();
        return false;
      }
      for (OrderDetailTM orderDetail: orderDetails) {
        pstm = connection.prepareStatement("INSERT INTO OrderDetail VALUES (?,?,?,?)");
        pstm.setObject(1, order.getOrderId());
        pstm.setObject(1, orderDetail.getCode());
        pstm.setObject(1, orderDetail.getQty());
        pstm.setObject(1, orderDetail.getUnitPrice());
        affectedRows = pstm.executeUpdate();
        if (affectedRows == 0){
          connection.rollback();
          return false;
        }
        pstm = connection.prepareStatement("UPDATE Item SET qtyOnHand = qtyOnHand - ? WHERE code= ?");
        pstm.setObject(1, orderDetail.getQty());;
        affectedRows = pstm.executeUpdate();
        if (affectedRows == 0){
          connection.rollback();
          return false;
        }
      }
      connection.commit();
      return true;
    } catch (SQLException throwables) {
      throwables.printStackTrace();
      try {
        connection.rollback();
      } catch (SQLException e) {
        e.printStackTrace();
      }
      return false;
    } finally {
      try {
        connection.setAutoCommit(true);
      } catch (SQLException throwables) {
        throwables.printStackTrace();
      }
    }
  }
public static boolean placeOrder ( OrderTM order , List < OrderDetailTM > orderDetails ) { Connection connection = DBConnection . getInstance ( ) . getConnection ( ) ; try { connection . setAutoCommit ( false ) ; PreparedStatement pstm = connection . prepareStatement ( "INSERT INTO `Order` VALUES (?,?,?)" ) ; pstm . setObject ( 1 , order . getOrderId ( ) ) ; pstm . setObject ( 2 , order . getOrderDate ( ) ) ; pstm . setObject ( 3 , order . getCustomerId ( ) ) ; int affectedRows = pstm . executeUpdate ( ) ; if ( affectedRows == 0 ) { connection . rollback ( ) ; return false ; } for ( OrderDetailTM orderDetail : orderDetails ) { pstm = connection . prepareStatement ( "INSERT INTO OrderDetail VALUES (?,?,?,?)" ) ; pstm . setObject ( 1 , order . getOrderId ( ) ) ; pstm . setObject ( 1 , orderDetail . getCode ( ) ) ; pstm . setObject ( 1 , orderDetail . getQty ( ) ) ; pstm . setObject ( 1 , orderDetail . getUnitPrice ( ) ) ; affectedRows = pstm . executeUpdate ( ) ; if ( affectedRows == 0 ) { connection . rollback ( ) ; return false ; } pstm = connection . prepareStatement ( "UPDATE Item SET qtyOnHand = qtyOnHand - ? WHERE code= ?" ) ; pstm . setObject ( 1 , orderDetail . getQty ( ) ) ; ; affectedRows = pstm . executeUpdate ( ) ; if ( affectedRows == 0 ) { connection . rollback ( ) ; return false ; } } connection . commit ( ) ; return true ; } catch ( SQLException throwables ) { throwables . printStackTrace ( ) ; try { connection . rollback ( ) ; } catch ( SQLException e ) { e . printStackTrace ( ) ; } return false ; } finally { try { connection . setAutoCommit ( true ) ; } catch ( SQLException throwables ) { throwables . printStackTrace ( ) ; } } }
this method goes to business layer
this method goes to business layer