2

I'm a java newbie but I'm not sure what I'm doing wrong. When I try to retrieve the last two dates from a database it only displays the year(while in mysql the same command provides the correct result).

Mysql command: SELECT DISTINCT date From fundanalysis ORDER BY date DESC LIMIT 2

Expected result:

2011-06-13
2011-06-08

Here's my java code:

        preparedStatement = con.prepareStatement("SELECT DISTINCT date From fundanalysis ORDER BY date DESC LIMIT 2");
        ResultSet numberofrowsresultset = preparedStatement.executeQuery();
        numberofrowsresultset.next();
        // most recent date
        currentdate.add(numberofrowsresultset.getInt("date"));
        System.out.print(numberofrowsresultset.getInt("date"));
        numberofrowsresultset.next();
        // last date before most recent
        currentdate.add(numberofrowsresultset.getInt("date"));
        return currentdate;

The final result is: [2011, 2011]

I basically want the exact same result as I get when I run the mysql query because I have to submit it as is to do another query later in the program.

pls help!

Lostsoul
  • 25,013
  • 48
  • 144
  • 239

4 Answers4

4

it is .getDate not .getInt

try:

numberofrowsresultset.getDate("date");
jargalan
  • 5,006
  • 5
  • 27
  • 36
2

Try use .getDate() instead of .getInt():

currentdate.add(numberofrowsresultset.getDate("date"));
jargalan
  • 5,006
  • 5
  • 27
  • 36
Marcin Michalski
  • 1,266
  • 13
  • 17
1

You are using .getInt which returns a numerical value. You need to use .getDate instead when you are getting a date value:

System.out.print(numberofrowsresultset.getDate("date"));
                                          ^^^^ change Int to Date
mellamokb
  • 56,094
  • 12
  • 110
  • 136
1

Date is not an integer so your '.getInt("date")' method is not returning the result you expect.

You need

java.sql.Date myDate = numberofrowsresultset.getDate("date"); 
James Anderson
  • 27,109
  • 7
  • 50
  • 78