-2

How do I get current date details in java? I have developed a class:

 public class RetailerWs {

 public String customerData(){
  String customerInfo = "";
  try{
  Class.forName("com.mysql.jdbc.Driver");
  Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/pro","root","");
   //Find customer information where the customer ID is maximum
   PreparedStatement statement =  con.prepareStatement("select * from orders where status='Q' AND date='2012-08-01'");
   ResultSet result = statement.executeQuery();
   int count=0;
    count++;
  // System.out.println(count);
   while(result.next()){
  System.out.println(count);
  }
      }
 catch(Exception exc){
 System.out.println(exc.getMessage());
  }

 return customerInfo;
 }

}

This is another class.

  public class Demo {
  public static void main(String[] args){
    RetailerWs obj = new RetailerWs();
    System.out.println(obj.customerData());
  }

 }

if I run demo, it calls RetailerWs after that it displays the value of count. The problem is the currently I insert the date manually, i.e.

   PreparedStatement statement =  con.prepareStatement("select * from orders where status='Q' AND date='2012-08-01'");

But I want a query that fetches the current date. How can I create and call it? Please help me.

okay_google
  • 339
  • 4
  • 17
Krishna Veni
  • 2,217
  • 8
  • 27
  • 53

4 Answers4

4

you can try

select * from orders where status='Q' AND date = CURDATE();
Shades88
  • 7,934
  • 22
  • 88
  • 130
  • i have updated dis code.if i run the demo class it is displayed 1 1.but i wish to display 2. (the above query match 2).but it is display 1 1.but i wish to display 2. how is to do. – Krishna Veni Aug 01 '12 at 09:40
  • please reply me.if i run the demo class it is displayed 1 1.but i wish to display 2. (the 2 data is matched for above query).but it is display 1 1.but i wish to display 2. how is to do – Krishna Veni Aug 01 '12 at 10:26
  • my god...what are you trying to do? You are SOP-ing count variable the as many number of times as there are records! What exactly is the purpose of your code? What is the point of putting SOP in while loop? If you function already has an SOP then why put call of your function in another SOP? – Shades88 Aug 01 '12 at 10:29
  • And your function returns customerInfo as blank. If this is something that you wish to do intentionally, then you are gravely wrong. Return the count and remove those SOPs in your function. – Shades88 Aug 01 '12 at 10:30
  • To see why you are seeing `1 1` and not `2`. Figure it out yourself, you are incrementing `count` only once. So it will be `1`. Then in loop you are displaying it's value. So....find out – Shades88 Aug 01 '12 at 10:32
1

You can use calendar implement it like :

public static Date getToday(Date date) {
    Calendar c = Calendar.getInstance();
    c.setTime(date);
    c.set(Calendar.HOUR_OF_DAY, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MILLISECOND, 0);
    return c.getTime();
}
Jason
  • 1,241
  • 8
  • 16
1

Replace the date in your prepared statement by a question mark and use the setDate() method to set its value :

statement = con.prepareStatement("select * from orders where status='Q' AND date=?");
statement.setDate(1, new Date());

The first parameter of setDate() is the index (which starts at 1) of the ?. It would be good practice to use this mechanism for the other parameters (Q) as well.

kgautron
  • 7,915
  • 9
  • 39
  • 60
  • 2
    What don't you understand? This is the full code for setting the date in your query, which is what you asked. Maybe you want us to do all your work so you don't have to spend any time understanding what you are doing? – kgautron Aug 01 '12 at 09:59
  • Please remember that SO is not a cut, copy, paste site. We expect some level of effort from your end. If you didn't understand some part of the code feel free to post that part as a comment. – Ajay George Aug 01 '12 at 10:05
  • sorry.here i have updated dis query select * from orders where status='Q' AND date = CURDATE(); .afterthat i run the demo class it is displayed 1 1.but i wish to display 2. (the above query match 2).but it is display 1 1.but i wish to display 2. how is to do. – Krishna Veni Aug 01 '12 at 10:18
0

If the date column includes Time part, you need to modify the query like below

select * from orders where status='Q' AND 
date >= CURDATE() and date<CURDATE() +interval 1 day ; 
Madhivanan
  • 13,470
  • 1
  • 24
  • 29