当前位置: 代码迷 >> Java Web开发 >> sql语句中索引越界的有关问题?
  详细解决方案

sql语句中索引越界的有关问题?

热度:7397   发布时间:2013-02-25 21:11:06.0
sql语句中索引越界的问题??
查询是没有问题的,我在查询中加了分页的代码后就出现了如下的错误,

我估计是如下的原因照成的,如果不给ps.setInt(2, pageIndex);ps.setInt(3, pageSize); 也会 报错,给 了也报错,不知道要怎么改,求各位高手帮帮忙。。。
ps.setInt(1, bid);
ps.setInt(2, pageIndex);
ps.setInt(3, pageSize);




报错如下:
java.sql.SQLException: Parameter index out of range (2 > number of parameters, which is 1).

//dao 类

public Map selectArticle(int pageIndex,int pageSize,int bid) throws Exception{

//String sql="select * from article where bid=? ";

Map map = new HashMap();
ArticleInfo artInfo = null;
List<ArticleInfo> artList = new ArrayList<ArticleInfo>();

String sql="select * from article where bid=? ";
sql+=" order by bid limit "+((pageIndex-1)*pageSize)+","+pageSize+"";

System.out.println(sql + "<<<<<<<<<<<<<<<<<");

Connection con = this.getCon();
PreparedStatement ps = con.prepareStatement(sql);
ps.setInt(1, bid);
ps.setInt(2, pageIndex);
ps.setInt(3, pageSize);


ResultSet rs = ps.executeQuery();

while(rs.next()){
artInfo = new ArticleInfo();

artInfo.setAid(rs.getInt("aid"));
artInfo.setTitle(rs.getString("title"));
artInfo.setBid(rs.getInt("bid"));
artList.add(artInfo);
}

map.put("artList", artList);
map.put("totalSize", this.getTotalCnt(sql));

this.Close(con, ps, rs);
return map;

}

//action 类

 //文章查询 根据版块 bid查询
public String execute(){

try{
ArticleDao dao = new ArticleDao();
//List<ArticleInfo> artList = dao.selectArticle(this.getBid());

//分页
String pageIndex = this.getRequest().getParameter("pageIndex");
String pageSize = this.getRequest().getParameter("pageSize");

pageIndex = pageIndex == null ? "1" : pageIndex;
pageSize = pageSize == null ? "10" : pageSize;

this.getRequest().setAttribute("pageIndex", pageIndex);
this.getRequest().setAttribute("pageSize", pageSize);

Map map=dao.selectArticle(Integer.parseInt(pageIndex), Integer.parseInt(pageSize), this.getBid());

this.getSession().setAttribute("bid", bid);
this.getSession().setAttribute("totalSize", map.get("totalSize"));
this.getSession().setAttribute("artList", map.get("artList"));



}
catch(Exception ex){
ex.printStackTrace();
}

return this.SUCCESS;
}


// basedao 类

//分页
public int getTotalCnt(String sql) throws Exception{

StringBuffer cntSql = new StringBuffer("select count(*) ");
String sqlold = sql.substring(sql.indexOf(" from"),sql.indexOf("limit"));
cntSql.append(sqlold);

int count = 0;

Connection con = this.getCon();
PreparedStatement ps = con.prepareStatement(cntSql.toString());
ResultSet rs = ps.executeQuery();

if(rs.next())
count = rs.getInt(1);
else 
count = 0;
this.Close(con, ps, rs);
return count;
}



------解决方案--------------------------------------------------------
String sql="select * from article where bid=? ";
sql+=" order by bid limit "+((pageIndex-1)*pageSize)+","+pageSize+"";

System.out.println(sql + "<<<<<<<<<<<<<<<<<");

Connection con = this.getCon();
PreparedStatement ps = con.prepareStatement(sql);
  相关解决方案