Ejemplo n.º 1
0
  public void addLine(Line toAdd) {
    connect();
    try {
      // Create a stmt object
      stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

      // Query the database, storing the result
      // in an object of type ResultSet
      rs =
          stmt.executeQuery(
              "SELECT * from Line WHERE "
                  + "ReceiptId='"
                  + toAdd.getReceiptId()
                  + "' AND "
                  + "ItemId='"
                  + toAdd.getItemId()
                  + "' AND "
                  + "Price='"
                  + toAdd.getPrice()
                  + "' AND "
                  + "Quantity='"
                  + toAdd.getQuantity()
                  + "'");

      // Check if User is already in the database
      while (rs.next()) {
        System.out.println("Line already exists in DB.");
        disconnect();
        return;
      } // end while loop

      stmt.executeUpdate(
          "INSERT INTO Line(ReceiptId, ItemId, Price, Quantity) VALUES ('"
              + toAdd.getReceiptId()
              + "', '"
              + toAdd.getItemId()
              + "', '"
              + toAdd.getPrice()
              + "', '"
              + toAdd.getQuantity()
              + "')");
    } catch (Exception e) {
      e.printStackTrace();
    } // end catch
    finally {
      disconnect();
    }
  }
Ejemplo n.º 2
0
  public Item getItem(Line line) {
    Item toReturn = null;
    connect();
    try {
      // Create a stmt object
      stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

      // Query the database, storing the result
      // in an object of type ResultSet
      rs = stmt.executeQuery("SELECT * from Item WHERE " + "ItemId='" + line.getItemId() + "'");

      // Check if User is already in the database
      while (rs.next()) {
        String itemId = rs.getString("ItemId");
        String name = rs.getString("Name");
        String locationId = rs.getString("LocationId");
        toReturn = new Item(itemId, name, locationId);
      } // end while loop
    } catch (Exception e) {
      e.printStackTrace();
    } // end catch
    finally {
      disconnect();
    }
    return toReturn;
  }