在java中结合for循环和sql

时间:2012-06-23 05:29:31

标签: java sql

我有2个输入日期:myStartDatemyEndDate和带有列的表TEST_TABLE

TEST_ID, TEST_USER,TEST_START, TEST_END

我需要检查myStartDate和myEndDate之间的日期范围是否在TEST_TABLE中有相应的记录。 我还需要确保不检索重复记录。

以下是我到目前为止的逻辑示例:

假设,

myStartDate=06/06/2012;myEndDate=06/09/2012
int diff = myEndDate - myStartDate; //In this case = 3

String myQuery = "SELECT * FROM TEST_TABLE WHERE"+ myStartDate +"BETWEEN TEST_START AND TEST_END OR "+ (myStartDate +1) +" BETWEEN TEST_START AND TEST_END OR"+ (myStartDate+2)+"BETWEEN TEST_START AND TEST_END OR"+(myStartDate+3)+"BETWEEN TEST_START AND TEST_END";

List <TestTableData> myList = new List();
//Exceute query & save results into myList using add method

想知道是否有办法在java代码中使用for loop测试myStartDate和myEndDate之间的日期范围,而不是上面myQuery中使用的方法。另外,我怎么能避免重复。

Java新手,所以任何帮助都会受到赞赏!

2 个答案:

答案 0 :(得分:0)

使用ResultSet迭代输出,如下面的代码。

while (res.next()) {
        String col1= res.getString("col1");
        String col2 = res.getString("col2");

      }

如果您使用Array实现,则不允许重复元素,因此无需检查。

但如果您必须使用列表,则可以使用以下代码删除任何重复的元素。

public static void removeDuplicates(List list)
  {
    Set set = new HashSet();
    List newList = new ArrayList();
    for (Iterator iter = list.iterator(); iter.hasNext(); ) {
      Object element = iter.next();
      if (set.add(element))
        newList.add(element);
    }
    list.clear();
    list.addAll(newList);
  }

答案 1 :(得分:0)

我认为你要问的是关于如何读取数据库以及如何处理java中的日期的一些通用问题。我将在下面给你一些示例代码。但我建议您查看java数据库教程http://docs.oracle.com/javase/tutorial/jdbc/index.html和java.util.Date api doc http://docs.oracle.com/javase/1.5.0/docs/api/java/sql/Date.html以获取更多信息。

以下是一些示例代码,专门演示了如何实现您的问题:

    // get the input dates
    // they are hard coded in this example
    // but would probably normally be passed in
    String startDateStr = "2/3/03";
    String endDateStr = "3/1/03";

    // unfortunately, there are 2 "Date" classes in this code and you need to differentiate
    // java.util.Date is the standard java class for manipulating dates
    // java.sql.Date is used to handle dates in the database
    // name conflicts like this are rare in Java
    SimpleDateFormat dateFmt = new SimpleDateFormat("M/d/yy");
    java.util.Date myStartDate = dateFmt.parse(startDateStr);
    java.util.Date myEndDate = dateFmt.parse(endDateStr);

    // conneect to the database
    // I am using mysql and its driver class is "com.mysql.jdbc.Driver"
    // if using a different database, you would use its driver instead
    // make sure the jar containing the driver is in your classpath (library list)
    // you also have to know the url string that connects to your database
    Class.forName("com.mysql.jdbc.Driver").newInstance(); // loads the driver
    Connection dbConn = DriverManager.getConnection(
       "jdbc:mysql://localhost/testdb", "(db user)", "(db password)"
    );

    // get the database rows from the db table
    // my table is named "testtable"
    // my columns are named "DateStart" and "DateEnd"
    Statement st = dbConn.createStatement();
    String sqlStr = "Select * from testtable";
    ResultSet rs = st.executeQuery(sqlStr);

    // loop through the rows until you find a row with the right date range
    boolean foundRange = false;
    while (rs.next()) {
        java.util.Date dbStartDate = rs.getDate("DateStart");
        java.util.Date dbEndDate = rs.getDate("DateEnd");
        if (myStartDate.before(dbStartDate)) continue;
        if (myEndDate.after(dbEndDate)) continue;
        foundRange = true;
        break;
    }

    if (foundRange) {
        // code that executes when range is found in db
    } else {
        // code that executes if range not found in db
    }

    dbConn.close();

希望这有助于您开始使用。