Exemplo n.º 1
0
  /**
   * This Method return an Object[] containing String[] in each item, that represent the result of
   * the specified query as a Matrix of Strings
   *
   * @param SQL Query [String]
   * @param Number of Columns involved [int]
   * @return Object[] or null
   */
  public Object[] obtainTable(Object... prop) {
    try {
      this.rs = conex.select((String) prop[0]);
      rs.next();
      int cols = 0;
      String[] names = new String[0];
      boolean hash = false;
      if (prop.length > 1 && prop[1].getClass().getSimpleName().equalsIgnoreCase("Integer")) {
        cols = (Integer) prop[1];
      } else if (prop.length > 1
          && prop[1].getClass().getSimpleName().equalsIgnoreCase("Boolean")) {
        if ((Boolean) prop[1]) {
          String sql = ((String) prop[0]).toLowerCase();
          names = sql.substring(sql.indexOf("select ") + 7, sql.indexOf(" from")).split(",");
          cols = names.length;
          hash = true;
        } else {
          String sql = ((String) prop[0]).toLowerCase();
          cols = sql.substring(sql.indexOf("select ") + 7, sql.indexOf(" from")).split(",").length;
        }
      } else {
        throw new InvalidParameterException();
      }

      if (hash) {
        ArrayList<Hashtable<String, Object>> array = new ArrayList<Hashtable<String, Object>>();
        do {
          Hashtable<String, Object> table = new Hashtable<String, Object>(cols);
          for (int i = 1; i < cols + 1; i++) {
            table.put(names[i - 1].trim(), rs.getObject(i));
          }
          array.add(table);
        } while (rs.next());
        return array.toArray();
      } else {
        ArrayList<String[]> array = new ArrayList<String[]>();
        do {
          String[] objs = new String[cols];
          for (int i = 1; i < cols + 1; i++) {
            objs[i - 1] = rs.getObject(i).toString();
          }
          array.add(objs);
        } while (rs.next());
        return array.toArray();
      }

    } catch (Exception e) {
      return null;
    }
  }
Exemplo n.º 2
0
  /**
   * Return the Object from the Database that represent the [completely] specified query
   *
   * @param Object
   * @param SQL Query [String]
   * @return True if the object could be restored, False in the other case.
   */
  public boolean obtainSelect(Object object, String sql) {
    try {
      this.rs = conex.select(sql);
      this.arrayRs.push(this.rs);

      if (rs.next() && (this.manager.result2Object(this, object, rs) != null)) {
        return true;
      }
    } catch (Exception e) {
      return false;
    } finally {
      if (!this.startObtainAll) {
        this.arrayRs.pop();
        if (!this.arrayRs.isEmpty()) {
          this.rs = this.arrayRs.peek();
        }
      } else {
        this.startObtainAll = false;
      }
    }

    return false;
  }