Cant get records between two dates

我的未来我决定 提交于 2019-12-08 13:37:14

问题


My problem is that I can't fetch all records that are between two dates.

I have two JDateChoosers. When I select two dates like '10-apr-2011' to '20-apr-2011' I want all the records between those dates to be displayed in my JList. But I can't get any results in the JList.

I am using mysql database.

  private void Display(java.awt.event.ActionEvent evt) {

    try 
    { 

        Class.forName("com.mysql.jdbc.Driver");  
        Connection con= 
           (Connection)DriverManager.getConnection(
             "jdbc:mysql://localhost:3306/test","root","ubuntu123");   
        java.util.Date jd =  jDateChooser1.getDate();
        java.util.Date jd1 = jDateChooser2.getDate();

        // PreparedStatement stmt = (PreparedStatement) con.prepareStatement("select date from invoice where date = ?);

        // PreparedStatement pstmt = (PreparedStatement) con.prepareStatement("SELECT date FROM invoice WHERE date BETWEEN ' ' AND ' '");

        PreparedStatement pstmt = (PreparedStatement) con.prepareStatement("SELECT date FROM invoice WHERE date >= '+jd + ' AND date <= '+jd1 + '");

        pstmt.execute();  

        ResultSet rs = pstmt.getResultSet();

        int i =0;
        DefaultListModel listModel = new DefaultListModel();

        while(rs.next())
        {   

            String [] data;
            data = new String[100];
            data [i] = rs.getString("date");
            jList1.setModel(listModel);
            listModel.addElement(data [i]);
            i = i+1;

        }
    } 
    catch (Exception e) 
    {
        System.out.println("2nd catch  " + e);
    }        
}

Can anyone tell me where my mistake is? Thanks in advance..


回答1:


Since this is a PreparedStatement you can try:

PreparedStatement pstmt = (PreparedStatement) con.prepareStatement("SELECT date FROM invoice WHERE date >= ? AND date <= ?");
pstmt.setDate(1,new java.sql.Date(jd.getTime()));
pstmt.setDate(2,new java.sql.Date(jd1.getTime()));
ResultSet rs = pstmt.executeQuery();



回答2:


You need to take out jList1.setModel(listModel); out of the loop

while(rs.next())
    {   

        String [] data;
        data = new String[100];
        data [i] = rs.getString("date");
        //jList1.setModel(listModel);
        listModel.addElement(data [i]);
        i = i+1;

       }

jList1.setModel(listModel);



回答3:


You need to alter your query. Something like this:-

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String jdStr = sdf.format(jd);
String jd1Str = sdf.format(jd1);
PreparedStatement pstmt = (PreparedStatement) con.prepareStatement("SELECT date FROM invoice WHERE date >= '" + jdStr + "' AND date <= '" + jd1Str + "'");

Previously, in your query, the 2 parameters, jd & jd1 were not getting append. This change will now append it in the query. The problem was with the jd & jd1 not correctly being appended in the query.

Note:- I've added a SDF so that you could format your date in format needed and append it to the query.




回答4:


  1. never to create an GUI Objects inside hard and long running JDBC, nor inside try - catch - finally, on exception those Object never will be created

  2. DefaultListModel listModel = new DefaultListModel(); should be created an local variable, and then isn't required to recreate a new XxxListModel on runtime

  3. have to remove all elements from listModel, otherwise new Items will be appended to the end of JList

  4. definition for String [] data; and data = new String[100]; and data [i] = rs.getString("date"); inside while(rs.next()) { are quite useless, because database records are stored from this array in XxxListModel, and accessible for other usage for elsewhere

  5. Connection, PreparedStatement and ResultSet should be closed in finally block (try - catch - finally), otheriwe these Objects stays (and increasing) in JVM memory,




回答5:


are you sure dates in SQL and date from java.util.Date are in the same format?

Try using

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = formatter.format(todaysDate);

to check whenever they are same.

jd and jd1 by default would differ by SQL date @see SQL Dates




回答6:


This is not the correct way of using PreparedStatement, which is already prepared to handle Dates, so you should not covert them to String. Your code should look like this:

PreparedStatement pstmt = ...
pstmt.setDate(...)

Also your query String is not really using the jd as a variable, you misused ' and "



来源:https://stackoverflow.com/questions/16165759/cant-get-records-between-two-dates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!