Ejemplo n.º 1
0
  /**
   * Returns a list of values, one per row, containing a map of column names to values of that row.
   * Note depending on the database, the column names may be uppercase (Oracle) or lowercase.
   *
   * @param databaseComponent database to query
   * @param query SQL query
   * @param parameters parameters to bind to the query
   * @throws SQLException if query fails
   */
  @Deprecated
  public static List<Map<String, Object>> getGridValues(
      DatabaseComponent databaseComponent, String query, Object... parameters) throws SQLException {
    PreparedStatement statement = null;
    ResultSet resultSet = null;
    List<Map<String, Object>> l = new ArrayList<Map<String, Object>>();
    try {
      statement = databaseComponent.getConnection().prepareStatement(query);
      bindParameters(statement, parameters);

      resultSet = statement.executeQuery();

      while (resultSet.next()) {
        Map<String, Object> row = new HashMap<String, Object>();
        l.add(row);

        ResultSetMetaData md = resultSet.getMetaData();
        String[] names = getColumns(md);

        for (String name : names) {
          Object o = resultSet.getObject(name);
          row.put(name, o);
        }
      }

    } finally {
      close(statement, resultSet);
    }
    return l;
  }
Ejemplo n.º 2
0
  /**
   * Executes a database update.
   *
   * @param databaseComponent
   * @param query
   * @param parameters
   * @return
   * @throws SQLException
   */
  @Deprecated
  public static int executeUpdate(
      DatabaseComponent databaseComponent, String query, Object... parameters) throws SQLException {
    PreparedStatement statement = null;
    try {
      statement = databaseComponent.getConnection().prepareStatement(query);
      bindParameters(statement, parameters);

      return statement.executeUpdate();
    } catch (SQLException e) {
      databaseComponent.removeConnection();
      throw e;
    } finally {
      close(statement, null);
    }
  }
Ejemplo n.º 3
0
  /**
   * Returns the result of a query as a single Double value. Returns {@link Double#NaN} if the query
   * fails.
   */
  @Deprecated
  public static Double getSingleNumericQueryValue(
      DatabaseComponent databaseComponent, String query, Object... parameters) {
    PreparedStatement statement = null;
    ResultSet resultSet = null;
    try {
      statement = databaseComponent.getConnection().prepareStatement(query);
      bindParameters(statement, parameters);
      resultSet = statement.executeQuery();

      if (resultSet.next()) {
        return resultSet.getDouble(1);
      }
    } catch (SQLException e) {
      databaseComponent.removeConnection();
    } finally {
      close(statement, resultSet);
    }

    return Double.NaN;
  }
Ejemplo n.º 4
0
  /**
   * Returns a mapping of rows as key-value pairs where the key is the first column (a string) and
   * the second column is a value numeric.
   *
   * @param databaseComponent the component to execute on
   * @param query the sql query to run
   * @param parameters any parameters to bind first
   * @return a Map<String,Double> of the keys against the value
   */
  @Deprecated
  public static Map<String, Double> getNumericQueryValueMap(
      DatabaseComponent databaseComponent, String query, Object... parameters) {
    PreparedStatement statement = null;
    ResultSet resultSet = null;
    try {
      Connection c = databaseComponent.getConnection();
      statement = c.prepareStatement(query);
      bindParameters(statement, parameters);

      resultSet = statement.executeQuery();

      Map<String, Double> map = new HashMap<String, Double>();

      while (resultSet.next()) {
        try {
          map.put(resultSet.getString(1), resultSet.getDouble(2));
        } catch (SQLException e) {
          // Ignore columns that can't be read as doubles
          if (LOG.isTraceEnabled()) {
            LOG.trace(
                "A query column value is not a double, ignoring:"
                    + ThrowableUtil.getAllMessages(e));
          }
        }
      }

      return map;
    } catch (SQLException e) {
      LOG.info("Unable to read value", e);
      databaseComponent.removeConnection();
    } finally {
      close(statement, resultSet);
    }

    return Collections.emptyMap();
  }
Ejemplo n.º 5
0
  /**
   * Executes a query, returning the results as a map where the keys are the column names and values
   * are the value of that column. Note depending on the database, the column names may be uppercase
   * (Oracle) or lowercase.
   *
   * @param databaseComponent
   * @param query SQL query string
   * @param parameters optional bind parameters
   * @return a map of query results
   */
  @Deprecated
  public static Map<String, Double> getNumericQueryValues(
      DatabaseComponent databaseComponent, String query, Object... parameters) {
    PreparedStatement statement = null;
    ResultSet resultSet = null;
    try {
      statement = databaseComponent.getConnection().prepareStatement(query);
      bindParameters(statement, parameters);

      resultSet = statement.executeQuery();

      Map<String, Double> row = new HashMap<String, Double>();

      ResultSetMetaData md = resultSet.getMetaData();
      String[] names = getColumns(md);

      if (resultSet.next()) {
        for (String name : names) {
          try {
            row.put(name, resultSet.getDouble(name));
          } catch (SQLException e) {
            // Ignore columns that can't be read as doubles
          }
        }
      }

      return row;
    } catch (SQLException e) {
      LOG.debug("Unable to read value", e);
      databaseComponent.removeConnection();
    } finally {
      close(statement, resultSet);
    }

    return Collections.emptyMap();
  }