Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9322149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T04:01:09+00:00 2026-06-19T04:01:09+00:00

I would like to put all the data from the following sql in a

  • 0

I would like to put all the data from the following sql in a map( because this sql is called many times and instead of going to db everytime ),
but I am wondering how to implement the <= for the timestamp

EDIT:

I am using Oracle, just updated the tags, however, I am using PreparedStatement in java which caches queries, without being recompiled, but our program doesn’t have a cache solution to cache the data from the table. going to the db and getting the data is taking 2 ms roundtrip, but getting the data from the HashMap would take a nano second. This query is being executed around 20,000 times and we would like to load all the data initially and put it inside the Hashmap.

END OF EDIT.

SELECT ar   
FROM table1   
WHERE fk_col1 = ?   
AND timestamp_col <= ?   
ORDER BY date DESC 

The way I did is as follows: but I am not sure, the timestamp_col in equals and hashCode is right. Could you suggest the modifications?

public class Table1Key
{
    private String fk_col1;
    private java.sql.Timestamp timestamp_col;
    //setters and getters here.
    //implementing hashCode and equals.
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((fk_col1 == null) ? 0 : fk_col1.hashCode());
        result = prime * result
                + ((timestamp_col == null) ? 0 : timestamp_col.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Table1Key other = (Table1Key) obj;
        if (fk_col1 == null) {
            if (other.fk_col1 != null)
                return false;
        } else if (!fk_col1.equals(other.fk_col1))
            return false;
        if (timestamp_col == null) {
            if (other.timestamp_col != null)
                return false;
        } else if (!timestamp_col.equals(other.timestamp_col))
            return false;
        return true;
    }
}

…

private Map<Table1Key, String> map = Functions.getHashMapInstance();

public class Functions {
...
  public static <K,V> HashMap<K,V> getHashMapInstance() {
      return new HashMap<K,V>();
  }
}

So, I would populate the map like the following:

private void populateMap() throws SQLException {
      try {
        ps = conn.prepareStatement(table1Sql);
        ps.setFetchSize(20000);
        ResultSet rs = ps.executeQuery();
        while(rs.next()) {
            Table1Key rdk = new Table1Key();
            String ar = rs.getString(1);
            rdk.setFk_col1(rs.getString(2));
            rdk.setTimestampCol(rs.getTimestamp(3));
            if(actualRateMap.get(rdk) == null) {
                actualRateMap.put(rdk, ar);
            }
        }
    } catch (SQLException e) {
        e.printStackTrace();
        throw e;
    } finally {
        ps.close();
    }
  }

//set the key here.

Table1Key tk = new Table1Key();
tk.setFk_col1(col1);
tk.setTimestampCol(timestamp);
String ar = actualRateMap.get(tk);

//My main concern here is .. this will work if the sql has timestamp_col = ?, but what if the timestamp_col is < than what is present in the map?

if(actualRate != null) {
    Logger.info("Actual Rate:"+actualRate);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T04:01:10+00:00Added an answer on June 19, 2026 at 4:01 am

    HashMap doesn’t do the job for your case, rather TreeMap can help.

    a): RangeMap solution

    Guava RangeMap is designed to handle such case:

    //Initial RangeMap  
    final RangeMap<java.sql.Timestamp, String> cache = TreeRangeMap.create();
    ...
    String value = cache.get(thisTimestamp);
    if(value == null){
         String queryFromDB = ...;//Query from DB
         cache.put(Range.atMost(thisTimestamp), queryFromDB);
         value = queryFromDB;
    }
    

    Of course, ‘fk_coll’ is problem. so you could define Map<String/*fk_coll*/, RangeMap<java.sql.Timestamp, String>> to handle the case.

    b): TreeMap solution

    final TreeMap<java.sql.Timestamp, String> cache = new TreeMap<>();
    ...
    //Get least key greater than or equal to the 'thisTimestamp' 
    Map.Entry<java.sql.Timestamp, String> entry = cache.ceilingEntry(thisTimestamp);
    if(entry == null){
       String queryFromDB = ...;//Query from DB
       cache.put(thisTimestamp, queryFromDB);
       value = queryFromDB;
    } else {
       value = entry.getValue();
    }
    

    and

    HashMap<String/*fk_coll*/, TreeMap<java.sql.Timestamp, String>>
    

    handles ‘fk_coll’.

    plus: evict is a problem in any cache case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm rendering a form. I would like to put a mark next to all
I would like to put a button on top of a Google Map next
I would like to put newlines in a property value of a manifest. Its
I would like to put a placeholder into e.g. the footer.html and have it
I would like to put jQuery at the bottom of the body tag but
I would like to put a time & date stamp on each row added
I would like to put a large variable definition in a separate file for
I would like to put my local OS X wordpress site in my ~users/Sites/public
I would like to put some extra merged cells for grouping reasons on the
Imagine a user that would like to put a form on their website that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.