我的程序中有以下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
我得到结果..那么问题出在哪里呢?它没有给我错误或任何东西
假设start
和end
是java.sql.Date
对象(如果不是,请转换),我建议您使用PreparedStatement
与setDate()
设置日期,如下所示:
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();
编辑:要从Joda DateTime
对象获取java.sql.Date
,请执行以下操作:
java.sql.Date startDate = new java.sql.Date(start.toDate().getTime());
java.sql.Date endDate = new java.sql.Date(end.toDate().getTime());
在查询中使用这些转换后的对象。