Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>They often just represents real world data. Here's a simple example of a Javabean:</p> <pre><code>public class User implements java.io.Serializable { // Properties. private Long id; private String name; private Date birthdate; // Getters. public Long getId() { return id; } public String getName() { return name; } public Date getBirthdate() { return birthdate; } // Setters. public void setId(Long id) { this.id = id; } public void setName(String name) { this.name = name; } public void setBirthdate(Date birthdate) { this.birthdate = birthdate; } // Important java.lang.Object overrides. public boolean equals(Object other) { return (other instanceof User) &amp;&amp; (id != null) ? id.equals(((User) other).id) : (other == this); } public int hashCode() { return (id != null) ? (getClass().hashCode() + id.hashCode()) : super.hashCode(); } public String toString() { return String.format("User[id=%d,name=%s,birthdate=%d]", id, name, birthdate); } } </code></pre> <p>Implementing <code>Serializable</code> is not per se mandatory, but very useful if you'd like to be able to persist or transfer Javabeans outside Java's memory, e.g. in harddisk or over network. </p> <p>In for example a DAO class you can use it to create a list of users wherein you <strong>store</strong> the data of the <code>user</code> table in the database:</p> <pre><code>List&lt;User&gt; users = new ArrayList&lt;User&gt;(); while (resultSet.next()) { User user = new User(); user.setId(resultSet.getLong("id")); user.setName(resultSet.getString("name")); user.setBirthdate(resultSet.getDate("birthdate")); users.add(user); } return users; </code></pre> <p>In for example a Servlet class you can use it to <strong>transfer</strong> data from the database to the UI:</p> <pre><code>protected void doGet(HttpServletRequest request, HttpServletResponse response) { List&lt;User&gt; users = userDAO.list(); request.setAttribute("users", users); request.getRequestDispatcher("users.jsp").forward(request, response); } </code></pre> <p>In for example a JSP page you can <strong>access</strong> it by <a href="http://docs.oracle.com/javaee/1.4/tutorial/doc/JSPIntro7.html" rel="noreferrer">EL</a>, which follows the Javabean conventions, to display the data:</p> <pre class="lang-html prettyprint-override"><code>&lt;table&gt; &lt;tr&gt; &lt;th&gt;ID&lt;/th&gt; &lt;th&gt;Name&lt;/th&gt; &lt;th&gt;Birthdate&lt;/th&gt; &lt;/tr&gt; &lt;c:forEach items="${users}" var="user"&gt; &lt;tr&gt; &lt;td&gt;${user.id}&lt;/td&gt; &lt;td&gt;&lt;c:out value="${user.name}" /&gt;&lt;/td&gt; &lt;td&gt;&lt;fmt:formatDate value="${user.birthdate}" pattern="yyyy-MM-dd" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/c:forEach&gt; &lt;/table&gt; </code></pre> <p>Does it make sense? You see, it's kind of a <strong>convention</strong> which you can use everywhere to <em>store</em>, <em>transfer</em> and <em>access</em> data.</p> <h3>See also:</h3> <ul> <li><a href="http://www.oracle.com/technetwork/java/javase/documentation/spec-136004.html" rel="noreferrer">JavaBeans specification</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload