java jdbc查询结果,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;

}

标签:postgresql,jdbc,java

来源: https://codeday.me/bug/20191119/2039294.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值