开发者

java mysql select statement not returning full date

开发者 https://www.devze.com 2023-03-13 19:17 出处:网络
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 resu

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!


it is .getDate not .getInt

try:

numberofrowsresultset.getDate("date");


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

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


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


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

精彩评论

暂无评论...
验证码 换一张
取 消