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

  • Home
  • SEARCH
  • 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 8745393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:00:15+00:00 2026-06-13T12:00:15+00:00

I currently have a method right now that looks like the following: public void

  • 0

I currently have a method right now that looks like the following:

public void foo(Date date) {
    PreparedStatement stmt; 
    ResultSet rs;
    java.sql.Date sDate = new java.sql.Date(date.getTime());

    try {
        String sql = "select * from some_table p where p.start_date <=? and ?<= p.end_date";
        stmt = getConnection().preparedStatement(sql);
        stmt.setDate(1, sDate);
        stmt.setDate(2, sDate);
        rs = stmt.executeQuery();
        //... 
    } finally {
        if (rs != null) { rs.close(); }
        if (stmt != null) { stmt.close(); }
    }
}

Now instead of passing one Date object, I would like to pass in list of date (List<Date> dates). I guess I technically can call foo multiple times, while iterating through the list, but is there a way I can achieve this without having to call foo multiple times?

  • 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-13T12:00:17+00:00Added an answer on June 13, 2026 at 12:00 pm

    Instead of passing a single Date object, consider passing an ArrayList of Date objects to your foo(...) method and working with that.

    The you ‘ve a couple of options to work with.

    Option 1: Execute your PreparedStatement multiple times by changing the parameters

    public void foo(ArrayList<Date> dateList) {
        if(dateList == null)
            return;
    
        PreparedStatement stmt = null;
        ResultSet rs = null;    
        java.sql.Date sDate = null;
        try{
            stmt = getConnection().preparedStatement("select * from some_table p where p.start_date <=? and ?<= p.end_date");
    
            for(Date date: dateList){
                try{
                    sDate = new java.sql.Date(date.getTime());
                    stmt.clearParameters(); //Clear current parameter values
                    stmt.setDate(1, sDate);
                    stmt.setDate(2, sDate);
                    rs = stmt.executeQuery();
    
                    //perform your operations
                }finally{
                    sDate = null;
                    //mange your resultset closing
                }
            }
        }finally{
            //your resource management code
        }  
    }
    

    Option 2: Create a SQL query taking into account the number of dates you’ve in your list, execute this statement and then work with the resultset.

    public void foo(ArrayList<Date> dateList) {
        if(dateList == null)
            return;
    
        PreparedStatement stmt = null;
        ResultSet rs = null;    
        java.sql.Date sDate = null;
        StringBuilder builder = new StringBuilder();
    
        try{
            //1. Create your dynamic statement
            builder.append("SELECT * FROM some_table p WHERE \n");
            for(int index = 0; index < dateList.length; index++){
                if(index > 0)
                    builder.append(" OR \n");
                builder.append("(p.start_date <=? and ?<= p.end_date)");
            }
    
            stmt = getConnection().preparedStatement(builder.toString());
    
            //2. Set the parameters
            int index = 1;
            for(Date date: dateList){
                try{
                    sDate = new java.sql.Date(date.getTime());
                    stmt.setDate(index, sDate);
                    stmt.setDate(index+1, sDate);
                    index += 2;
                }finally{
                    sDate = null;
                    //mange your resultset closing
                }
            }
    
            //3. execute your query
            rs = stmt.executeQuery();
    
            //4. perform your operations
        }finally{
            builder = null;
            //your resource management code
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i currently have a method that checks what is around the centre item in
I currently have this method: -(void)seperateGuides { DLog(GetGuideListCount: %i, [[appDelegate getGuideList] count]); columnOneArray =
I currently have the following controller method in a Rails app: def index @entries
Currently I have created a ABCFactory class that has a single method creating ABC
I have a method in Java that concatenates 2 Strings. It currently works correctly,
All, I have a method that is currently used to invoke DLLs of return
I'm currently calling Trace (method below) from a game loop. Right now all I'm
Right now I have an Accordion component in Flex that has two children, I
I currently have this working but it requires me to have a static method
Hey, I currently have a Currency Format method: private string FormatCurrency(double moneyIn) { CultureInfo

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.