gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:42:10 27 4
gpt4 key购买 nike

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

关于Java 如何正确转换 jdbc 查询的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32110638/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com