student
student

Reputation: 167

Querying list with prepared statement (JDBC)

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?

Upvotes: 3

Views: 5296

Answers (2)

Sujay
Sujay

Reputation: 6783

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
    }

}

Upvotes: 2

Alexis Pigeon
Alexis Pigeon

Reputation: 7512

This is a solution using only one database call. This doesn't check for null or empty dateList, on the contrary it assumes there is at least one element.

public void foo(List<Date> dateList) {
    PreparedStatement stmt; 
    ResultSet rs;

    try {
        // Step 1 : build the query string, based on the number of elements in the list
        StringBuilder sql = new StringBuilder("select * from some_table p where (p.start_date <=? and ?<= p.end_date)");
        if (dateList.size() > 1) {
            for (int i = 1; i < dateList.size(); i++) {
                sql.append(" or (p.start_date <=? and ?<= p.end_date)");
            }
        }

        stmt = getConnection().preparedStatement(sql.toString());

        // Step 2 : pass the actual list of dates to the query
        for (int i = 0; i < dateList.size(); i++) {
            java.sql.Date date = new java.sql.Date(dateList.get(i).getTime());
            stmt.setDate((i * 2) + 1, date);
            stmt.setDate((i * 2) + 2, date);
        }

        rs = stmt.executeQuery();
        //... 
    } finally {
        if (rs != null) { rs.close(); }
        if (stmt != null) { stmt.close(); }
    }
}

Upvotes: 0

Related Questions