1

I've been trying to come up with a couple of SQL statements on how to get this right but no luck. I've tried between, >= and =<. Basically, the SQL statement that I've used is working but to an extent only.

My code works like this: the user will choose a date range (from date and to date) and the program will retrieve and show the data it has within those range. Like I said, it works but it also shows the days from the other months when what I want to show is just those particular days that the user picked. eg. from July 1, 2016 to July 5, 2016. What's happening is any month of the year that has those dates will show as well which makes that particular method a bit useless.

Any help or any explanation why is this so would be appreciated.

Below is my code:

stringFromDate = sdf.format(fromDate.getDate());

stringToDate = sdf.format(toDate.getDate());

String query = "Select * from tblSavings where date between '" + stringFromDate+ "' and '" + stringToDate+"'";

try{ 

    pstmt = conn.prepareStatement(query);

    rs = pstmt.executeQuery();

    tblList.setModel(DbUtils.resultSetToTableModel(rs));
7
  • 3
    SQL Injection alert - you should not concatenate together your SQL statements - use parametrized queries instead to avoid SQL injection Commented Jul 7, 2016 at 7:04
  • 3
    Most likely the format of stringFromDate and stringToDate are incorrect, and this is why the query is not working as you expect. Update and show us the actual raw query being executed. Also, what version of SQL is being used? Commented Jul 7, 2016 at 7:05
  • @marc_s I'm looking at the link you gave me right now. Thanks. Commented Jul 7, 2016 at 7:16
  • @TimBiegeleisen I'm using SQLite. Thanks! What I don't get is why is it getting the days from the other months. I really thought it was working but then this. I'm a bit moronic when it comes to terminologies and would like to give you what you asked but I'm afraid you jargoned the hell out of me. Thanks still. :) Commented Jul 7, 2016 at 7:16
  • 3
    Show us the literal values of stringFromDate and stringToDate. Commented Jul 7, 2016 at 7:22

2 Answers 2

1

You can use SimpleDateFormat to get a string which is in proper format to be used as a date in SQLite, the proper format being yyyy-MM-dd:

String pattern = "yyyy-MM-dd";
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
String stringFromDate = dateFormat.format(fromDate.getDate());
Sign up to request clarification or add additional context in comments.

1 Comment

This fixed it! Thanks a lot!
1

Seems like you're trying to re-invent the wheel here. PreparedStatements were created exactly for this usecase - setting variable values in a predefined structure. In your case, with the setDate method. From the context I'm guessing your fromDate and toDate variables are java.util.Date instances, so you'll have to convert them to java.sql.Dates

java.sql.Date fromDateToBind = new java.sql.Date(fromDate);
java.sql.Date toDateToBind = new java.sql.Date(toDate);

String query = "Select * from tblSavings where date between ? and ?";

try{ 
    pstmt = conn.prepareStatement(query);
    pstmt.setDate(1, fromDateToBind);
    pstmt.setDate(2, toDateToBind);
    rs = pstmt.executeQuery();

    // use the results...
} // etc...

1 Comment

I've used what Tim suggested and it worked. I'll keep this in mind as well and will try on the next project I'm gonna make. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.