• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Select Where Between syntax

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Goodday,

please i am getting a jtable to display values between a date period. when this part of syntax executes, it displays all the values in the database without displaying the range i asked it to.

please help.
thank you in advance

rs=stat.executeQuery("SELECT * FROM Incomedb WHERE Date BETWEEN'"+getfrmdate+"'AND'"+gettodate+"'");
//System.out.println(getfrmdate + ""+""+ gettodate);
while(rs.next()){
partdb = rs.getString("Particular");
amtdb = rs.getDouble("Amount");
datedb = rs.getString("Date");

gettotal = amtdb+gettotal;
// System.out.println(partdb);
Vector tmp = new Vector();
tmp.addElement(partdb);
tmp.addElement(amtdb);
tmp.addElement(datedb);

data1.addElement(tmp);
}
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The syntax is probably right. I'd suggest printing out the full SQL query text and running it in an SQL client to see how it works.

One possible caveat is that your code probably relies on implicit conversion of the strings you're stuffing into the SQL query to dates. This is bad, you should avoid implicit conversions. It is possible that the database expects a different date format from what you've used (eg. DD/MM/YYYY instead of MM/DD/YYYY), which could lead into providing an unexpected output or an error, depending on the dates. All this assumes that the Date column is actually stored as a date in the database, not as a text. (Storing dates as texts in the database results in different class of problems and should not be really done either.)

The correct way is to use a PreparedStatement and set the dates directly, not converting them to text.

Also, you should not use SELECT *, use column lists instead.
 
kelly devon
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Martin thanks for bringing this to light i have seen my mistake!!!
Thank you so much
 
Martin Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome!
 
It sure was nice of your sister to lend us her car. Let's show our appreciation by sharing this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic