微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

   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";
        }
        if(i>=60 && i<1440)
        {
            id=i+ " hrs";
        }
        if(i>=1441 && i<10080)
        {
            id=i+" days";
        }
        else
        {
            id=i+" weeks";
        }
// returns as 4 date
        return id;
    }

我正在使用此System.err.println(difference(rs.getInt(“ fire”))));跟踪结果.我该如何进行这项工作,或者有更好的方法来实现这一目标?

解决方法:

您在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;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐