1

I'm trying to display data from a database table named BOOKS in an h:dataTable that grabs its data from a method named getBooks().

public ArrayList<Book> getBooks() throws SQLException {
    if (ds == null) {
        throw new SQLException("ds is null; Can't get data source");
    }

    Connection conn = ds.getConnection();

    if (conn == null) {
        throw new SQLException("conn is null; Can't get db connection");
    }
    try {
        PreparedStatement ps = conn.prepareStatement(
                "select TITLE, AUTHOR, PRICE from BOOKS"
        );

        // retrieve customer data from database
        ResultSet result = ps.executeQuery();

        while (result.next()) {
            Book b = new Book();
            b.setTitle(result.getString("TITLE"));
            b.setAuthor(result.getString("AUTHOR"));
            b.setPrice(result.getDouble("PRICE"));
            books.add(b);
        }

    } finally {
        conn.close();
    }
    return books;
}

The SQL statement works fine and it retrieves the correct data, however when this JSF page runs:

<h:body>
    <center>
        <h1>Welcome to the Java Bookstore!</h1>
        <h:form>
            <h3>Inventory List</h3>
            <h:dataTable value="#{bookDatabase.books}" var="book" border="5" class="bookList">

                <h:column>
                    <f:facet name="header">Title</f:facet>
                    <h:outputText value="#{book.title}" />
                </h:column>
                <h:column>
                    <f:facet name="header">Author</f:facet>
                    <h:outputText value="#{book.author}" />
                </h:column>
                <h:column>
                    <f:facet name="header">Price</f:facet>
                    <h:outputText value="#{book.price}">
                        <f:convertNumber currencySymbol="$" groupingUsed="true"
                                         maxFractionDigits="2" type="currency"/>
                    </h:outputText>
                </h:column>

                <h:column>
                    <h:commandLink value="Add to Cart" 
                                   action="#{shoppingCart.add(book)}" >
                        <f:ajax render="@all"/>
                    </h:commandLink>
                </h:column>

            </h:dataTable>

        </h:form>
        <br/>
        <br/>
        <h3>Shopping Cart</h3>
        <h:form>
            <h:dataTable value="#{shoppingCart.box}" var="cart" border="5" class="shoppingCart">
                <h:column>
                    <f:facet name="header">Title</f:facet>
                    <h:outputText value="#{cart.title}" />
                </h:column>
                <h:column>
                    <f:facet name="header">Author</f:facet>
                    <h:outputText value="#{cart.author}" />
                </h:column>
                <h:column>
                    <f:facet name="header">Price</f:facet>
                    <h:outputText value="#{cart.price}">
                        <f:convertNumber currencySymbol="$" groupingUsed="true"
                                         maxFractionDigits="2" type="currency"/>
                    </h:outputText>
                </h:column>

                <h:column>
                    <f:facet name="header">Quantity</f:facet>
                    <h:outputText value="#{cart.quantity}" />
                </h:column>

                <h:column>
                    <h:commandLink value="Remove By One" action="#{shoppingCart.remove(cart)}" >
                        <f:ajax render="@form" />
                    </h:commandLink>
                </h:column>

            </h:dataTable>
            <br/>
            <h3>
                Total Price:
                <h:outputText value="#{shoppingCart.getTotal()}">
                    <f:convertNumber currencySymbol="$" groupingUsed="true"
                                     maxFractionDigits="2" type="currency"/>
                </h:outputText>
            </h3>


            <h:commandButton value="Checkout"
                             action="#{shoppingCart.checkout}"/>
        </h:form>
    </center>
</h:body>

It displays this table for the Inventory List that should only contain 4 elements, but seems to have iterated more than needed i.e. displaying the 4 books again in the table after reaching the last book.

Why is it giving me this bug? Is there something i'm not doing right?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Whyte3212
  • 11
  • 2
  • 1
    Please move the business logic in its own place (I am avoiding this fact for no reason as of now). Here ``, you are calling a getter method [which invoke multiple times](http://stackoverflow.com/a/2090062/1391249) by nature in which you have placed the business logic. Migrate the code from that method to another place such as a method decorated with `@PostConstruct`. (Do not use deprecated HTML tags/attributes like `
    ` which is highly discouraged).
    – Tiny Feb 22 '15 at 05:23

0 Answers0