0

我有 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

想知道是否有任何方法可以使用for loopjava 代码中的 a 来测试 myStartDate 和 myEndDate 之间的日期范围,而不是上面使用的方法myQuery。此外,我怎样才能避免重复。

Java新手,所以任何帮助将不胜感激!

4

2 回答 2

0

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

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

      }

如果您使用Arrayimplementation ,它不允许重复元素,因此无需检查一个。

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

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);
  }
于 2012-06-23T06:09:00.860 回答
0

我认为您要问的是一些关于如何读取数据库以及如何在 java 中处理日期的一般性问题。下面我会给你一些示例代码。但我建议您查看 java 数据库教程http://docs.oracle.com/javase/tutorial/jdbc/index.html和 java.util.Date api 文档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();

希望这可以帮助您入门。

于 2012-06-23T21:54:52.420 回答