Entity Bean Programm
Entity Bean Programm
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class BookBank implements Serializable {
/**
* @return the serialVersionUID
*/
public static long getSerialVersionUID() {
return serialVersionUID;
}
private String title;
private String author;
private double price;
public BookBank() {
super();
}
return id;
}
@Override
public int hashCode() {
int hash = 0;
hash += (getId() != null ? getId().hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof BookBank)) {
return false;
}
BookBank other = (BookBank) object;
if ((this.getId() == null && other.getId() != null) || (this.getId() != null && !
this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "bank.BookBank[ id=" + getId() + " ]";
}
/**
* @return the title
*/
public String getTitle() {
return title;
}
/**
* @param title the title to set
*/
public void setTitle(String title) {
this.title = title;
}
/**
* @return the author
*/
public String getAuthor() {
return author;
}
/**
* @param author the author to set
*/
public void setAuthor(String author) {
this.author = author;
}
/**
* @return the price
*/
public double getPrice() {
return price;
}
/**
* @param price the price to set
*/
public void setPrice(double price) {
this.price = price;
}
}
6. Right click on the SavingsAccount WebApplication project and select java class
Java class name: BookCatalogInterface
Package name: bank
//BookCatalogInterface.java
package bank;
import javax.ejb.Remote;
import java.util.Collection;
@Remote
public interface BookCatalogInterface {
public void addBook(String title, String author, double price);
public Collection <BookBank> getAllBooks();
}
7. Right click on the SavingAccount webapplication project and select stateless
session bean
Session bean name: BookCatalogBean
Package: bank
//BookCatalogBean.java
package bank;
import java.util.Iterator;
import java.util.Collection;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.io.Serializable;
import javax.ejb.Remote;
@Remote(BookCatalogInterface.class)
@Stateless
public class BookCatalogBean implements Serializable, BookCatalogInterface {
@PersistenceContext(unitName="samplePU")
EntityManager em;
}
8. Right Click on the SavingAccount webApplication project create a new jsp
JSP file name: WebClient
WebClient.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="bank.*, javax.naming.*, java.util.*"%>
<%!
private BookCatalogInterface bci = null;
String s1,s2,s3;
Collection list;
try {
s1 = request.getParameter("t1");
s2 = request.getParameter("aut");
s3 = request.getParameter("price");
<%
}
list=bci.getAllBooks();
for (Iterator iter = list.iterator(); iter.hasNext();){
BookBank element = (BookBank)iter.next();
%>
<br>
<p>Book ID: <b><%= element.getId() %></b></p>
<p>Title: <b><%= element.getTitle() %></b></p>
<p>Author: <b><%= element.getAuthor() %></b></p>
<p>Price: <b><%= element.getPrice() %></b></p>
<%
}
<body bgcolor="pink">
<h1>Library</h1>
<hr>
<br>
<p>
<input type="submit" value="Submit">
<input type="reset" value="Reset"></p>
</form>
</body>
</html>
10. compile all the java files and run the index.jsp file
Enter the input
Table creation:
CREATE TABLE BOOKBANK (
ID int(11) NOT NULL auto_increment,
TITLE varchar(50) NOT NULL,
AUTHOR varchar(50) NOT NULL,
PRICE decimal(12,2) NOT NULL,
PRIMARY KEY (ID)
);