public List bookQuery(String type,String publish,String name){
//创建list集合
List list=null;
//实力化BaseDao类
BaseDao baseDao=new BaseDao();
//创建Connection对象
Connection con=null;
//创建PreparedStatement对象
PreparedStatement pStatement=null;
//创建结果集
ResultSet rs=null;
//需要执行在SQL语句
String sql="select * from book where book_type like '%?%' or book_publish like '%?%' or book_name like '%?%'";
try{
//得到Connection链接
con=baseDao.getConnection();
//通过Connection对象得到pStatement对象
pStatement=con.prepareStatement(sql);
//为参数赋值
pStatement.setString(1,type);
pStatement.setString(2,publish);
pStatement.setString(3,name);
//返回结果集
rs=pStatement.executeQuery();
//如果返回在结果集中有对象则循环读取
while(rs.next()){
//给实体类中在属性进行赋值
String bookname=rs.getString("book_name");
String bookpublish=rs.getString("book_publish");
String booktype=rs.getString("book_type");
String bookdate=rs.getString("book_date");
//实力化BookEntity类
BookEntity bookEntity=new BookEntity(bookname,bookpublish,booktype,bookdate);
//将bookEntity对象添加到List集合中
list.add(bookEntity);
}
}catch(Exception e){
//如果出现异常将其打印出来
e.printStackTrace();
}finally{
//关闭结果集
baseDao.closeRs(rs);
//关闭pStatement对象
baseDao.closepStatement(pStatement);
//关闭Connection链接
baseDao.closeConnection(con);
}
//返回list
return list;
}
这是在java类中写的方法。
<%
//支持中文输入
request.setCharacterEncoding("gbk");
//获取输入的图书名称
String name=request.getParameter("name");
//获取输入的图书类别
String type=request.getParameter("type");
//获取输入的图书出版社
String publish=request.getParameter("publish");
//实力化BookDao对象
BookDao bookDao=new BookDao();
//得到list集合
List list= bookDao.bookQuery(type,publish,name);
if(list==null){
out.println("没有您要查询在图书,请换一个关键字重新查询");
}else{
//循环遍历出list集合中的对象
for(int i=1;i<=list.size();i++){
BookEntity bookEntity=(BookEntity)list.get(i);
%>
<table>
<tr>
<td><a href="add.jsp">增加图书</a></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>图书名称</td>
<td>技术类别</td>
<td>出版社</td>
<td>出版时间</td>
</tr>
<tr>
<td><%=bookEntity.getBook_name() %></td>
<td><%=bookEntity.getBook_type() %></td>
<td><%=bookEntity.getBook_publish() %></td>
<td><%=bookEntity.getBook_date()%></td>
</tr>
<%
}
}
%>
</table>
</form>
这是jsp页面。运行时报错:com.microsoft.sqlserver.jdbc.SQLServerException: 索引 1 超出范围。
sql语句没问题
//创建list集合
List list=null;
//实力化BaseDao类
BaseDao baseDao=new BaseDao();
//创建Connection对象
Connection con=null;
//创建PreparedStatement对象
PreparedStatement pStatement=null;
//创建结果集
ResultSet rs=null;
//需要执行在SQL语句
String sql="select * from book where book_type like '%?%' or book_publish like '%?%' or book_name like '%?%'";
try{
//得到Connection链接
con=baseDao.getConnection();
//通过Connection对象得到pStatement对象
pStatement=con.prepareStatement(sql);
//为参数赋值
pStatement.setString(1,type);
pStatement.setString(2,publish);
pStatement.setString(3,name);
//返回结果集
rs=pStatement.executeQuery();
//如果返回在结果集中有对象则循环读取
while(rs.next()){
//给实体类中在属性进行赋值
String bookname=rs.getString("book_name");
String bookpublish=rs.getString("book_publish");
String booktype=rs.getString("book_type");
String bookdate=rs.getString("book_date");
//实力化BookEntity类
BookEntity bookEntity=new BookEntity(bookname,bookpublish,booktype,bookdate);
//将bookEntity对象添加到List集合中
list.add(bookEntity);
}
}catch(Exception e){
//如果出现异常将其打印出来
e.printStackTrace();
}finally{
//关闭结果集
baseDao.closeRs(rs);
//关闭pStatement对象
baseDao.closepStatement(pStatement);
//关闭Connection链接
baseDao.closeConnection(con);
}
//返回list
return list;
}
这是在java类中写的方法。
<%
//支持中文输入
request.setCharacterEncoding("gbk");
//获取输入的图书名称
String name=request.getParameter("name");
//获取输入的图书类别
String type=request.getParameter("type");
//获取输入的图书出版社
String publish=request.getParameter("publish");
//实力化BookDao对象
BookDao bookDao=new BookDao();
//得到list集合
List list= bookDao.bookQuery(type,publish,name);
if(list==null){
out.println("没有您要查询在图书,请换一个关键字重新查询");
}else{
//循环遍历出list集合中的对象
for(int i=1;i<=list.size();i++){
BookEntity bookEntity=(BookEntity)list.get(i);
%>
<table>
<tr>
<td><a href="add.jsp">增加图书</a></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>图书名称</td>
<td>技术类别</td>
<td>出版社</td>
<td>出版时间</td>
</tr>
<tr>
<td><%=bookEntity.getBook_name() %></td>
<td><%=bookEntity.getBook_type() %></td>
<td><%=bookEntity.getBook_publish() %></td>
<td><%=bookEntity.getBook_date()%></td>
</tr>
<%
}
}
%>
</table>
</form>
这是jsp页面。运行时报错:com.microsoft.sqlserver.jdbc.SQLServerException: 索引 1 超出范围。
sql语句没问题
解决方案 »
- 求高手解释,myEclipse中运行tomcat出现下面信息,当还能测试web应用,如www.localhost:1212/xx.jsp,为什么?
- 遮罩层对flash(mapabc电子地图)不好用 显示不出来
- jsp问题
- 请教:有个jsp和servlet程序,部署不了。并报错。
- 数据库准确插人问题
- 请教一个数据库查询问题,路过的请看看
- 求'jsp应用开发详解'第2或3版 pdf 电子版
- ResourceBundle类如何使用问题?
- 在b/s结构中,如何通过ie启动客户端的应用程序(.exe)!
- 在tomcat和apache下能运行EJB吗?
- MyEclipse6.5搭建JSP开发环境,要装Tomcat插件和Lomboz插件,应该用什么版本的插件?
- stuts2 的集合展现问题
改为String sql="select * from book where book_type like ? or book_publish like ? or book_name like ?"; pStatement.setString(1,type);
pStatement.setString(2,publish);
pStatement.setString(3,name); 改为pStatement.setString(1,"%"+type+"%");
pStatement.setString(2,"%"+publish+"%");
pStatement.setString(3,"%"+name+"%"); 试试看?
'%?%'这种写法的含义是匹配含有引号的记录,所以pStatement.setString(1,type)时报索引位置错误异常。
org.apache.jasper.JasperException: An exception occurred processing JSP page /list.jsp at line 5552:
53: <% // }else{
54: //循环遍历出list集合中的对象
55: for(int i=0;i<list.size();i++){
56: BookEntity bookEntity=(BookEntity)list.get(i);
57: %>
58: <table>
public int addBook(BookEntity book){
//声明一个变量接受返回的受影响在行数
int num=0;
//实力化BaseDao类
BaseDao baseDao=new BaseDao();
//创建Connection对象
Connection con=null;
//创建PreparedStatement对象
PreparedStatement pStatement=null;
//需要执行在SQL语句
String sql="insert into book values(?,?,?,?)";
try{
//得到Connection链接
con=baseDao.getConnection();
//通过Connection对象得到pStatement对象
pStatement=con.prepareStatement(sql);
//为参数赋值
pStatement.setString(1,book.getBook_name());
pStatement.setString(2,book.getBook_type());
pStatement.setString(3,book.getBook_publish());
pStatement.setString(4,book.getBook_date());
//返回受影响在行数
num=pStatement.executeUpdate();
}catch(Exception e){
//如果出现异常将其打印出来
e.printStackTrace();
}finally{
}
return num;
}
我只看出了连接没关。另外就是4个字段为null或空串时数据库是否接收。