Java如何正确转换jdbc查询的结果

时间:2015-08-20 06:02:58

标签: java postgresql jdbc

(Postgres 9.4)我有一个返回整数 4 的简单查询,然后我捕获该数字并循环遍历if语句并返回编辑结果。答案应该是 4分钟,但我一直 4周。由于某种原因,这不起作用,例如这是我的代码

library(ggplot2)
DF <- data.frame(v1=c(5, 10, 15, 20, 25, 30), v2=c(2, 3, 1, 3, 4, 2))

p <- ggplot(DF) + geom_bar(aes(x=v1, y=v2),stat="identity")
p <- p + theme_classic()
p <- p + labs(title='Chart Title', x='series 1', y='')
p

我正在使用 System.err.println(差异(rs.getInt(“fire”))); 来跟踪结果。我怎样才能完成这项工作或有更好的方法来实现这一目标?

1 个答案:

答案 0 :(得分:3)

if-else语句中有错误。尝试以下

try {
        Connection con = null;
        ResultSet rs;
    con=DB.getConnection();


       // this fire returns as an Integer 4
        PreparedStatement ps =con.prepareStatement("SELECT EXTRACT
(EPOCH FROM(last_reply-created_on)/60):: integer as fire from streams where id=65");
        rs= ps.executeQuery();


        while (rs.next()) {
         // I then put this method through
            System.err.println(difference(rs.getInt("fire")));
        }
        con.close();
        return ok();
    } catch (Exception e) {
        System.err.println(e.getMessage());
    }
private static String difference(Integer i) {
String id="";
    if(i<60)
    {
     4 is obviously less than 60 but it is not working
        id= i+ " min";
    }else if(i>=60 && i<1440)
    {
        id=i+ " hrs";
    }else if(i>=1441 && i<10080)
    {
        id=i+" days";
    }
    else
    {
        id=i+" weeks";
    }
// returns as 4 date
    return id;
}