Lostsoul
Lostsoul

Reputation: 26009

java mysql select statement not returning full date

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!

Upvotes: 2

Views: 450

Answers (4)

Marcin Michalski
Marcin Michalski

Reputation: 1266

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

currentdate.add(numberofrowsresultset.getDate("date"));

Upvotes: 2

James Anderson
James Anderson

Reputation: 27478

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"); 

Upvotes: 1

mellamokb
mellamokb

Reputation: 56769

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

Upvotes: 1

jargalan
jargalan

Reputation: 5194

it is .getDate not .getInt

try:

numberofrowsresultset.getDate("date");

Upvotes: 4

Related Questions