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?