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?
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
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.