JSP MySQL结果

时间:2013-05-29 13:36:26

标签: java mysql database jsp

我编写了这个函数来获取从MySQL数据库到JSP的项目列表:

public List getBookList() throws SQLException
   {
      List  BookList = new ArrayList();


      ResultSet results = statement.executeQuery("SELECT * FROM book" );



      while ( results.next() ) {
         BookBean  view  = new BookBean();

        view.setID(results.getString( 1 ));
        view.setName(results.getString( 2 ));
        view.setDescription(results.getString( 3));
        view.setCatID(results.getString( 4));
        view.setUID(results.getString(5 ));
        view.setDateAdded(results.getString( 6 ));
        view.setPicThumb(results.getString( 7 ));
        view.setPicLarge(results.getString( 8 ));




         BookList.add(view);
      }

      return BookList;
   }

如何调用此方法在.jsp页面上呈现结果集,是否有更好的方法来重写此方法?

2 个答案:

答案 0 :(得分:1)

是的,方法很好。您只需将此ArrayList对象带到jsp trhough request.setAttribute或从jsp scriplet <% somewhere on the top of the page... %>调用此方法,然后使用jstl呈现ArrayList。

以下是jsp trhough jstl

中呈现的ArrayList<HashMap<String,Object>>的示例

click here for example

用Google搜索,你会找到很多例子。

答案 1 :(得分:1)

使用MVC设计模式,您可以将值设置为model,然后在controller中将模型对象设置为请求。然后使用

类似这样的事情

request.setAttrbute("key to access the model",your model Object);
ServletContext context= getServletContext();
RequestDispatcher rd= context.getRequestDispatcher("path_to_your_jsp_page");
rd.forward(request, response);

然后使用ELJSTL读取JSP中的值 有很多方法,但上面就是其中之一!