读书人

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

发布时间: 2012-09-28 00:03:35 作者: rapoo

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);


ps.setInt(1, bid);
ps.setInt(2, pageIndex);
ps.setInt(3, pageSize);


ResultSet rs = ps.executeQuery();


这里有错,sql语句中只有一个占位符(也就是?),ps也就只能set一次,
但是你set了3次,所以参数的索引越界了
[解决办法]
你值给了一个问号 却用pst给配置了3个参数。你要么查询语句把参数都连接起来,要么就三个问号站位,下面配三个参数。如果你上面站位一个问号,就pst值需要set一个另外两个在查询语句里拼接吧
[解决办法]
你SQL只有一个参数,你却传了 3个进去,肯定会报越界的错误了
[解决办法]
传进去的参数,与占位符的参数个数要匹配起来。

读书人网 >Java Web开发

热点推荐