Stringid=request.getParameter("id");//获取数据源DataSourceds=null;try{Contextcontext=newInitialContext();ds=(DataSource)context.lookup("java:/comp/env/jdbc/mysqlds");}cat...
String id = request.getParameter("id");
// 获取数据源
DataSource ds = null;
try {
Context context = new InitialContext();
ds = (DataSource) context.lookup("java:/comp/env/jdbc/mysqlds");
} catch (Exception e) {
System.out.println("数据源获取失败");
}
// 连接数据库
try {
// 获取连接
QueryRunner qr = new QueryRunner(ds);
String sql = "select id,title,content,created_time from blog where id="
+ id;
List list = (List) qr.query(sql, new BeanListHandler(Blog.class));
Blog blog = (Blog) list.get(0);
System.out.println(blog.getId());
System.out.println(blog.getTitle());
System.out.println(blog.getConcent());
System.out.println(blog.getCreated_time());
/*request.setAttribute("Blog", blog);
request.getRequestDispatcher("/displayblog.jsp").forward(request,
response);*/
} catch (SQLException e) {
e.printStackTrace();
}
执行老是提示Blog blog = (Blog) list.get(0);越界
展开