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语句没问题

解决方案 »

  1.   

    String sql="select * from book where book_type like '%?%' or book_publish like '%?%' or book_name like '%?%'"; 
    改为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+"%"); 试试看?
      

  2.   

    一楼的方案正确。
    '%?%'这种写法的含义是匹配含有引号的记录,所以pStatement.setString(1,type)时报索引位置错误异常。
      

  3.   

    没报上次那个错误了 ,但是出现了新的问题。还是报错:java.lang.NullPointerException
      

  4.   

    修改后报这个错误:
    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>
      

  5.   

    改为 int i=0。可能你查出来只有一条记录
      

  6.   

    刚刚的问题我修改了一下,和List bookQuery(String type,String publish,String name)方法所属类中还有另外一个方法 ,我想是那个方法出了点问题,帮忙看一下。
    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;
    }
      

  7.   


    我只看出了连接没关。另外就是4个字段为null或空串时数据库是否接收。