user3159272
user3159272

Reputation:

Get records between two String dates

I know that data type of Date column should be Date/Time, but in my case it is Text.

Now I want to fetch records between two dates.

String Date1=jTextField.getText();
String Date2=jTextField2.getText();

pre=conn.prepareStatement("select * from Table where Date between "Date1" and "Date2"");

ResultSet rs=pre.executeQuery();

I know that this query does not help me. But I am totally new to sql query so please help me to solve this problem.

Thank you in advance.

EDIT:

I am using MS Access Database.

At this stage I am just printing records using System.out.println(rs.getString(1));

Upvotes: 0

Views: 1561

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201497

You can use the CDate function, and the ORDER BY is

// Java style variable names.
String date1 = jTextField.getText();
String date2 = jTextField2.getText();

// using CDate...
pre=conn.prepareStatement("select * from Table where Date "
    + "between CDate(?) and CDate(?) order by Date");
pre.setString(1, date1); // bind param 1
pre.setString(2, date2); // bind param 2

ResultSet rs=pre.executeQuery();

It is also possible to convert the String(s) into Date(s) in Java using a SimpleDateFormat, but that solution is more complex. At this point, you should probably stick with the simplest (correct) solution.

Upvotes: 2

Related Questions