当前位置: 代码迷 >> Java Web开发 >> 数据库分页查询赋值解决方案
  详细解决方案

数据库分页查询赋值解决方案

热度:180   发布时间:2016-04-17 10:18:34.0
数据库分页查询赋值
private final static String QUERYPAGE="SELECT R_ID,ROOM_ID,R_STATE,R_DATE,R_ENDDATE,R_GOODCOST,R_SERVE,R_CONTENTS,R_EMPLOYEE,OWNER_NAME,RN FROM" 
  +" (SELECT R_ID,ROOM_ID,R_STATE,R_DATE,R_ENDDATE,R_GOODCOST,R_SERVE,R_CONTENTS,R_EMPLOYEE,OWNER_NAME,RN FROM "
  +" (SELECT o.OWNER_NAME,"
  +" r.R_ID,r.ROOM_ID,r.R_STATE,r.R_DATE,r.R_ENDDATE,r.R_GOODCOST,r.R_SERVE,r.R_CONTENTS,r.R_EMPLOYEE,ROWNUM RN FROM OWNER o,REPAIR r "
  +" WHERE o.ROOM_ID=r.ROOM_ID AND ? AND ?) WHERE RN>=?) WHERE RN<=?";

-----------------------
pstmt = conn.prepareStatement(QUERYPAGE);
if(r_date!=null&&!"".equals(r_date)&&r_sDate!=null&&!"".equals(r_sDate)){  
pstmt.setString(1, " r.r_date between"+" to_date("+r_date+",yyyy-mm-dd)"+" and "+" to_date("+r_sDate+",yyyy-mm-dd) "); }else{
pstmt.setString(1, " 1=1");
}
if(r_state!=null&&!"".equals(r_state)){  
pstmt.setString(2, " r.R_STATE LIKE "+r_state); }else{
pstmt.setString(2, " 1=1");
}
pstmt.setInt(3, start);
pstmt.setInt(4, end);  
rs = pstmt.executeQuery();

为什么第一第二占位符 赋不上值呢?求解!!!谢谢
PS:去掉1,2占位符 运行正确

------解决方案--------------------
晕,不知道楼上怎么回答的,这怎么可能对,用?号就不行,除非改成拼sql的办法。

首先setString的索引是从1开始的,不是0.

当然问题不在这个,是因为?号,比如" r.R_STATE LIKE "+r_state解析完是 r.R_STATE LIKE 123

这是sql变成

WHERE o.ROOM_ID=r.ROOM_ID AND 'r.R_STATE LIKE 123'

注意,它把你传的值都作为字符串来处理了,包括你想当然的r.R_STATE字段,
它不会把它当做表的字段,而是普通的一个字符串而已。


探讨

pstmt.setString(0, " r.r_date between"+" to_date("+"'"+r_date+"'"+",'yyyy-mm-dd')"+" and "+" to_date("+"'"+r_sDate+"'"+",'yyyy-mm-dd') ");
还是不对呢 --
  相关解决方案