0

i'm trying to get data from Mysql db to a .jsp page :

  <%

  List list = connection.getBookList();
  int id = 0;
  String box = null;

  Iterator<String> it = list.iterator();

  while (it.hasNext()) {
      id = Integer.parseInt(it.next());
      out.print("<tr>");
      for (int i = 0; i < 4; i++) {
          out.print("<td>");
          out.print(it.next());
          out.print("</td>");
  }
  out.print("<td>");
  box = "<input name=r" + id + " type='checkbox'>";
  out.print(box);
  out.print("</td>");
  out.print("</tr>");
 }
%>

here is the method in my class of db retrival:

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;
   }

if though is there any help to print my data in a jsp page within the way the upper method is written? i think the (view) makes the difference isn't it?

9
  • 2
    What is the problem now
    – PSR
    May 29, 2013 at 12:46
  • @Anas Jamal Abu Sall - I am clear about your problem. May 29, 2013 at 12:49
  • 2
    your list is a list of BookBean, you can't use an Iterator<String>... look for logs.
    – mauretto
    May 29, 2013 at 12:51
  • What do you want your view to render? May 29, 2013 at 12:54
  • 1
    Providing a description of what is wrong would be nice. Like is there any errors? What output do you get?
    – Qben
    May 29, 2013 at 12:58

1 Answer 1

2

Your iteration should be with BookBean and not with String

<%

  List list = connection.getBookList();
  int id = 0;
  String box = null;

  Iterator<BookBean> it = list.iterator();

  while (it.hasNext()) {

      BookBean view = it.next();
      out.print("<tr>");
      for (int i = 0; i < 4; i++) {
          out.print("<td>");

          //if you have a getter method in bean
          out.print(view.getId());

          out.print("</td>");
  }
  out.print("<td>");
  box = "<input name=r" + view.getId() + " type='checkbox'>";
  out.print(box);
  //out.print(view.getName());
  out.print("</td>");
  out.print("</tr>");
 }
%>
2
  • this code gives me null pointer excp. java.lang.NullPointerException bookshare.DBconnect.getBookList(DBconnect.java:184) org.apache.jsp.test_jsp._jspService(test_jsp.java:92) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:728) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432) javax.servlet.http.HttpServlet.service(HttpServlet.java:728) org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393) May 30, 2013 at 9:44
  • The exception is happening in DBconnect.java:184. Can you tell me what is the content in line number 184. Make sure the statement is not null and initialized properly in the getBookList method.
    – vjy
    May 30, 2013 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.