0

我的程序中有以下 SQL 语句:

"SELECT ANTAL_KALD,BESVARET_25_SEK,((BESVARET_25_SEK/nullif(ANTAL_KALD,0))*100) as answer_percent,TIDSPUNKT,QUEUE "
                +"FROM KS_DRIFT.PERO_NKM_KØ_OVERSIGT WHERE TIDSPUNKT BETWEEN '"+start.getYear()+"-"+start.getMonthOfYear()+"-"+start.getDayOfMonth()+
                "' AND '"+end.getYear()+"-"+end.getMonthOfYear()+"-"+end.getDayOfMonth()+
                "'";

这没有给我任何结果集

测试System.out.println(rs.next())false

但是,当我在我的数据库中写入以下内容时:

select QUEUE,
ANTAL_KALD,
BESVARET_25_SEK,
((BESVARET_25_SEK/nullif(ANTAL_KALD,0))*100) as answer_percent,
TIDSPUNKT
from KS_DRIFT.PERO_NKM_KØ_OVERSIGT Where TIDSPUNKT BETWEEN '2012-12-01' AND '2012-12-02' ORDER BY QUEUE

我得到了结果.. 那么问题是什么?它没有给我错误或任何东西

4

1 回答 1

1

假设startandendjava.sql.Date对象(如果不是,请转换),我建议您使用PreparedStatementwithsetDate()来设置日期,如下所示:

   String sql = "SELECT ANTAL_KALD,BESVARET_25_SEK, "+
               " ((BESVARET_25_SEK/nullif(ANTAL_KALD,0))*100) as answer_percent, "+
               " TIDSPUNKT,QUEUE "
               +"FROM KS_DRIFT.PERO_NKM_KØ_OVERSIGT "+
               " WHERE TIDSPUNKT BETWEEN ?  AND ?";
   PreparedStatement qStmt=connection.prepareStatement(sql);
   qStmt.setDate(1, start);
   qStmt.setDate(1, end);


   ResultSet rs = qStmt.executeQuery();

编辑:要从java.sql.DateJoda DateTimeObject 获取,请执行以下操作:

   java.sql.Date startDate = new java.sql.Date(start.toDate().getTime());
   java.sql.Date endDate   = new java.sql.Date(end.toDate().getTime());

在查询中使用这些转换后的对象。

于 2012-12-07T15:09:02.050 回答