代码如下:package com.radyworld.blog;import java.util.List;
import java.io.IOException;
import java.sql.SQLException;import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;public class GetBlogServlet extends HttpServlet { private static final long serialVersionUID = -902274209927022883L; @SuppressWarnings({ "rawtypes", "unchecked" })
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id = request.getParameter("id"); // 数据源对象可以理解为连接池的管理者,通过他可以获取数据库的链接
DataSource ds = null; try {
// 通过在context.xml文件,设定的获取数据源的对象的名字,获取数据源对象
Context context = new InitialContext();
ds = (DataSource) context.lookup("java:/comp/env/jdbc/mysqlds");
} catch (Exception e) {
System.out.println("获取数据源时出错");
}
try {
// 添加博文的SQL语句,now()生成当前系统的时间
String sql = "select id,title,content,createdtime from blog where id=10"
+ id;
// DButils中核心类,生成对象时传递数据源对象
QueryRunner qr = new QueryRunner(ds);
List list = (List) qr.query(sql, new BeanListHandler(Blog.class));
Blog blog = (Blog) list.get(0); request.setAttribute("blog", blog);
request.getRequestDispatcher("/displayBlog.jsp").forward(request,
response);
} catch (SQLException e) {
e.printStackTrace();
}
}}
----------------------------------------------------------------------------------------------
错误报告:严重: Servlet.service() for servlet GetBlogServlet threw exception
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at com.radyworld.blog.GetBlogServlet.doGet(GetBlogServlet.java:44)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)

解决方案 »

  1.   

    sql字符串中的那个10是不是多的?
      

  2.   

    你的List是不是空的哦  你调试看看  
      

  3.   

     输出list.size()看是不是为0 ,最好 if(list != null && list.size() > 0) ....
      

  4.   

    Blog blog = (Blog) list.get(0);
    查询出来的list应该为null,在使用之前按照楼上的先判断一下在取list的值...
      

  5.   

    调试一下那个list吧,get里边数据量不能太大的,list是空的或者太大都会出问题。
      

  6.   

    嗯,换了个方式,去掉了。String sql = "select id,title,content,createdtime from blog where id="+ id;
      

  7.   

    空的!输入为0,但数据库里面是有内容,sql查询语句应该没有什么问题吧
    依旧无法解决~ 崩溃ing
      

  8.   

    越界是因为sql查询返回的list.size() == 0;
    至于你说的查不到数据的情况,一个可行的方法就是,debug时将sql查询字符串的值抓出来在database客户端运行下,确认是否可查到记录,以此判断问题出在哪里
      

  9.   

    严重: Servlet.service() for servlet GetBlogServlet threw exception
    java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.RangeCheck(ArrayList.java:547)
    at java.util.ArrayList.get(ArrayList.java:322)
    at com.radyworld.blog.GetBlogServlet.doGet(GetBlogServlet.java:44)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)lz要学会观察log啊good luck
      

  10.   

    楼主你好,你把提交方式换Post试试吧
      

  11.   

    还有几种情况下可能会失败

    1 页面中Input控件的name不是"id",导致id取到的总是null
    2 若id的字段类型是字符类型,则应该用单引号括起来
      

  12.   

    嗯,解决了! 
    谢谢大家~  找到问题了。id传入的值null了!  调整下id的值就ok了 真囧,终于查出来了,小问题大苦恼~ 坚持,坚持,坚持,fighting!!!!