Parameter index out of range (1 > number of parameters, which is 0).

今天调数据库课程设计(Java+MySql)的时候通过Tomcat测试的时候总是抛出“Parameter index out of range (1 > number of parameters, which is 0).”的异常,系统提示出错的行是如下标示“抛出异常”的行。
[codesyntax lang=”java” lines=”normal”]

public List<Book> searchBorrowed(String id) throws Exception {
	List<Book> allBooks = new ArrayList<Book>();
	PreparedStatement pstmt = null;
	ResultSet rs = null;
	String sql = "select b.name as name,b.author as author,b.category as category,borr.btime as btime,borr.rtime as rtime from borrowed borr,books b where borr.bid=b.id and isreturn=0 and pid=?";
	pstmt = conn.prepareStatement(sql);
	pstmt.setString(1, id);//抛出异常!
	rs = pstmt.executeQuery();
	while (rs.next()) {
		Book book = new Book();
		book.setName(rs.getString("name"));
		book.setAuthor(rs.getString("author"));
		book.setCategory(rs.getString("category"));
		book.setBtime(Long.getLong(rs.getString("btime")));
		book.setRtime(Long.getLong(rs.getString("rtime")));
		allBooks.add(book);
	}
	return allBooks;
}

[/codesyntax]
 
经过多次测试,甚至编写了一个测试用的类直接本地运行调用这个函数却没有出错,Google搜索了下,大家出现这个问题大部分都是ParparedStatement的用法有问题或者Sql语句写的有问题,查了很久,也没找到原因,而最后看到 这里 最后一段话,令我恍然大悟:
[codesyntax lang=”text”]

Please forget about it. I found why the error was occurring.
I was actually using one single PreparedStatement for two different
methods (silly me), and when the flow returned from the called method,
it still was the first preparedstatement rather than the new one,
hence it couldn't found the parameter in the proper indexed position.

[/codesyntax]
 
大体意思是说他使用同一个pstmt执行了两条查询以后出现了这个问题,仔细查看源代码,发现我使用了数据库连接之后没有释放,再次调用的时候,系统就没有再实例化新的pstmt而是直接把刚才用过的这个实例返回给我了,而那个测试用的类没有出现这个问题是因为他仅仅执行了一个SQL查询就退出了,不像我实际中使用的前面还执行了一个登录的过程。知道了问题所在解决起来也便简单的多了,使用代理模式重构代码,通过代理来访问这个具体的数据库操作,从代理中来对数据库的连接与关闭进行处理,这样即更好的解耦,又使得我不用去关注数据库的连接与关闭操作,直接交给代理完成就可以了 :)