使用预准备语句查询列表(JDBC)

时间:2012-10-28 21:16:15

标签: java sql database jdbc prepared-statement

我现在有一个方法,如下所示:

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(); }
    }
}

现在,我想传递一个日期列表(List<Date> dates),而不是传递一个Date对象。我想我在技术上可以多次调用foo,同时遍历列表,但有没有办法可以实现这一点而无需多次调用foo?

2 个答案:

答案 0 :(得分:2)

不要传递单个Date对象,而应考虑将Date对象的ArrayList传递给foo(...)方法并使用它。

你可以选择几种方式。

选项1:通过更改参数多次执行PreparedStatement

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
    }  
}

选项2 :创建一个SQL查询,考虑列表中的日期数,执行此语句,然后使用结果集。

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
    }

}

答案 1 :(得分:0)

这是仅使用一个数据库调用的解决方案。这不会检查null或空dateList,相反它假设至少有一个元素。

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(); }
    }
}