Click here to Skip to main content
16,013,548 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
try {

    ps = connection.prepareStatement("SELECT * FROM project.order WHERE PO_Date >="+startDate+" AND PO_Date <= "+endDate+"");
    System.out.println("value of ps "+ps);

    ResultSet rs = ps.executeQuery();
    System.out.println("value of rs "+rs);
    while (rs.next()) {
        System.out.println("value of while loop "+rs.next());
        ArrayList<String> arrlist = new ArrayList<String>();

        System.out.println(rs.getInt(1));
        System.out.println(rs.getDate(2));
        System.out.println(rs.getString(4));
        System.out.println(rs.getString(5));
        System.out.println(rs.getString(6));

        System.out.println("******************");

        Iterator<String> itr = arrlist.iterator();
        while (itr.hasNext()) {
            System.out.println(itr.next());

        }
    rs.close();
} 
    }catch (Exception e) {
    System.out.println(e.getMessage());
}
return id;


What I have tried:

And also tried this too but the problem is it is not comparing the whole date format
after converting to date format it is not executing the query..please help me to solve the problem

public int getdata(String startDate, String endDate) {
PreparedStatement ps;
int id = 0;
try {
/*
* SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
* java.util.Date startDat = formatter.parse(startDate);
* java.util.Date endDat = formatter.parse(endDate);
*/
// ps = connection.prepareStatement("select * from project.order Where PO_Date Between " + startDate + "' AND '" + endDate + "'");
//ps = connection.prepareStatement("select * from project.order where PO_Date between ? AND DATE_ADD( ?, INTERVAL 1 DAY) ");
//ps = connection.prepareStatement("SELECT * FROM project.order WHERE PO_Date between ? AND ?");
ps = connection.prepareStatement("SELECT * FROM project.order WHERE PO_Date >= ? AND PO_Date <= ?");
//ps = connection.prepareStatement("SELECT * FROM project.order WHERE PO_Date(now())>= ? AND PO_Date(now())<=?");
/*
* ps.setDate(1, new java.sql.Date(startDate)); ps.setDate(2, new
* java.sql.Date(endDate.getTime()));
*/
ps.setString(1, startDate);
ps.setString(2, endDate);

ResultSet rs = ps.executeQuery();
// System.out.println("value of rs "+rs);

while (rs.next()) {
ArrayList<string> arrlist = new ArrayList<string>();
System.out.println(rs.getString(2));
System.out.println(rs.getInt(1));
System.out.println(rs.getString(4));
System.out.println(rs.getString(5));
System.out.println(rs.getString(6));
System.out.println("***************");
// System.out.print(rs.getDate("endDate"));
Iterator<string> itr = arrlist.iterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}
}
rs.close();

} catch (Exception e) {
System.out.println(e.getMessage());
}
return id;
}
}
Posted
Updated 29-Feb-16 20:42pm
Comments
Taimoor Hassun 1-Mar-16 2:12am    
please first provide database column datatype .
Member 12360714 1-Mar-16 2:26am    
PO_No integer
PO_Date Date
Supplier String(varchar)
Currency varch
CurrencyRate varchar
Richard MacCutchan 1-Mar-16 4:22am    
You need to convert your date strings to proper Date/DateTime types to do the compare.
Member 12360714 1-Mar-16 5:29am    
@Richard MacCutchan i have done but after that the query does not execute

1 solution

try this, hope it would solve your problem and help you to trace your error.
C#
try {
        Date start_date= df.parse(startDate);
        Date end_date= df.format(endDate);
        ps = connection.prepareStatement("SELECT * FROM project.order WHERE PO_Date >="+start_date+" AND PO_Date <= "+end_date+"");
    System.out.println("value of ps "+ps);
    } catch (ParseException e) {
        e.printStackTrace();
    }
 
Share this answer
 
Comments
Member 12360714 1-Mar-16 3:32am    
@Taimoor Hassun what is df
iam getting df can not be resolved
Taimoor Hassun 1-Mar-16 4:23am    
df is object of dateformat class
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
Member 12360714 1-Mar-16 5:13am    
@Taimoor Hassun iam getting Type mismatch: cannot convert from String to Date
in df.format(endDate);
Date end_date= df.format(endDate);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900