title: retrieving data from mysql database table in Java
body: <p>I'm having a problem retrieving data from the database.
  I have a function</p>
  
  <pre><code>import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.PreparedStatement;
  import java.sql.ResultSet;  
  import java.sql.SQLException;
  import java.sql.Statement;
  
  public class DataAccess {
  private Connection conn;
  private User user = new User();
  
  public DataAccess(){
      connect();
  }
  
  public boolean connect() {
      boolean success = true;
  
      String driverName ="com.mysql.jdbc.Driver";
      String conURL = "jdbc:mysql://localhost:3306/final_project";
      String user = "root";
      String pass = "1009";
  
      try {
          Class.forName(driverName).newInstance();
      } catch (InstantiationException e) {
          e.printStackTrace();
      } catch (IllegalAccessException e) {
          e.printStackTrace();
      } catch (ClassNotFoundException e) {
          System.err.println(e.getMessage() + "------Cannot Load Driver");
          success = false;
      }
  
      try {
          conn = DriverManager.getConnection(conURL, user, pass);
      } catch (SQLException e) {
          System.err.println(e.getMessage() + "--SQL States: " + e.getSQLState() + "---- ErrorCode: " + e.getErrorCode());
          success = false;
      }
      return success;
  }
  
  public void insertBooks (Books books)
  {
      try {           
          PreparedStatement stmt = conn.prepareStatement("insert into Books (Title, Author, ISBN, Total) VALUES (?, ?, ?, ?);");
          stmt.setString(1, books.getTitle());
          stmt.setString(2, books.getAuthor());
          stmt.setInt(3, books.getISBN());
          stmt.setDouble(4, books.getPrice());
  
          stmt.executeUpdate();
      } catch (SQLException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  }
  
  public void selectBooks () {
      try {
          Statement stm = conn.createStatement();
          ResultSet rs = stm.executeQuery("SELECT * FROM books");
          while (rs.next()) {
              Books books = new Books();
  
              books.setTitle(rs.getString(1));
              books.setAuthor(rs.getString(2));
              books.setISBN(rs.getInt(3));
              books.setPrice(rs.getDouble(4));
  
              user.add(books);
          }
      } catch (SQLException e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>which selects from the table books and gets and sets every field as title, author, isbn and price, and then it's added to the bookList object in the user class.
  When I use the function</p>
  
  <pre><code>public ArrayList&lt;Books&gt; getBookList() {
      return bookList;
  }
  </code></pre>
  
  <p>nothing is returned.</p>
  
  <p>The User class:</p>
  
  <pre><code>import javax.faces.bean.ManagedBean;
  import javax.faces.bean.SessionScoped;
  import javax.faces.context.FacesContext;
  import java.util.ArrayList;
  
  @ManagedBean(name = "user")
  @SessionScoped
  public class User {
  private String firstName;
  private String lastName;
  private ArrayList&lt;Books&gt; bookList = new ArrayList&lt;Books&gt;();
  private ArrayList&lt;Books&gt; shopBookList = new ArrayList&lt;Books&gt;();
  private double total;
  
  public String getFirstName() {
      return firstName;
  }
  
  public void setFirstName(String firstName) {
      this.firstName = firstName;
  }
  
  public String getLastName() {
      return lastName;
  }
  
  public void setLastName(String lastName) {
      this.lastName = lastName;
  }
  
  public ArrayList&lt;Books&gt; getBookList() {
      return bookList;
  }
  
  public void setBookList(ArrayList&lt;Books&gt; bookList) {
      this.bookList = bookList;
  }
  
  public double getTotal() {
      for (Books bk : bookList) {
          total += bk.getPrice();
      }
      return total;
  }
  
  public void setTotal(double total) {
      this.total = total;
  }
  
  public ArrayList&lt;Books&gt; getShopBookList() {
      return shopBookList;
  }
  
  public void setShopBookList(ArrayList&lt;Books&gt; shopBookList) {
      this.shopBookList = shopBookList;
  }
  
  public String add(Books books) {
      bookList.add(books);
      DataAccess da = new DataAccess();
      da.insertBooks(books);
      return "Added";
  }
  
  public String searchBooks() {
      DataAccess da = new DataAccess();
      da.selectBooks();
      return "books";
  }
  
  public String shop(Books books) {
      shopBookList.add(books);
      return null;
  }
  
  public String start() {
      bookList = new ArrayList&lt;Books&gt;();
      firstName = "";
      lastName = "";
      total = 0;
      FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
      return "index";
  }
  </code></pre>
  
  <p>}</p>
  
  <p>The Books class:</p>
  
  <pre><code>import javax.faces.bean.ManagedBean;
  import javax.faces.bean.SessionScoped;
  
  @ManagedBean(name = "books")
  @SessionScoped
  
  public class Books {
  private String title;
  private String author;
  private int ISBN;
  private double price;
  
  public String getTitle() {
      return title;
  }
  
  public void setTitle(String title) {
      this.title = title;
  }
  
  public String getAuthor() {
      return author;
  }
  
  public void setAuthor(String author) {
      this.author = author;
  }
  
  public int getISBN() {
      return ISBN;
  }
  
  public void setISBN(int iSBN) {
      ISBN = iSBN;
  }
  
  public double getPrice() {
      return price;
  }
  
  public void setPrice(double total) {
      this.price = total;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>The page that is supposed to grab data from user.booklist:</p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  
  &lt;html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core"&gt;
  
  &lt;h:body&gt;
  &lt;h:form&gt;
      &lt;h1&gt;Books Page&lt;/h1&gt;
      &lt;hr /&gt;
      &lt;label&gt;The following books are available:&lt;/label&gt;&lt;br /&gt;
      &lt;h:dataTable value="#{user.bookList}" var="bk" border="1"&gt;
          &lt;h:column&gt;
              &lt;f:facet name="header"&gt;ISBN&lt;/f:facet&gt;
              &lt;h:outputText value="#{bk.ISBN}" /&gt;
          &lt;/h:column&gt;
          &lt;h:column&gt;
              &lt;f:facet name="header"&gt;Title&lt;/f:facet&gt;
              &lt;h:outputText value="#{bk.title}" /&gt;
          &lt;/h:column&gt;
          &lt;h:column&gt;
              &lt;f:facet name="header"&gt;Author&lt;/f:facet&gt;
              &lt;h:outputText value="#{bk.author}" /&gt;
          &lt;/h:column&gt;
          &lt;h:column&gt;
              &lt;f:facet name="header"&gt;Price&lt;/f:facet&gt;
              &lt;h:outputText value="#{bk.price}" /&gt;
          &lt;/h:column&gt;
          &lt;h:column&gt;
              &lt;f:facet name="header"&gt;Add&lt;/f:facet&gt;
              &lt;h:commandLink value="Add" action="#{user.shop(bk)}" /&gt;
          &lt;/h:column&gt;
      &lt;/h:dataTable&gt;
      &lt;h:commandButton value="Home" action="index" /&gt;
      &lt;h:commandButton value="Checkout" action="checkout" /&gt;
  &lt;/h:form&gt;
  &lt;/h:body&gt;
  &lt;/html&gt;
  </code></pre>