Question

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

Was it helpful?

Solution

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

OTHER TIPS

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

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.

  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,

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

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 "

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top