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

相关文章

连接数据库的方式:第一种方式:ODBC:开放数据库连接是微软...
JDBCRequest 使用VariableNamesmysql:数据库连接池对象var...
 1.JDBCDBC(JavaDataBaseConnectivity):Java数据库连接技术...
1.需要jar包的支持:java.sqljavax.sqlmysql-conneter-java....
1.简介Activiti是一个业务流程管理(BPM)框架,它是覆盖了业务...
1.JDBC体系系统一组规范:接口JDBC接口(API)包括两个层次:...