簡體   English   中英

Java如何正確轉換jdbc查詢的結果

[英]Java How can I correctly transform a result from a jdbc query

(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(差異(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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM