Click here to load reader
Upload
kishan-chand
View
214
Download
0
Embed Size (px)
DESCRIPTION
Entity Bean Programming Example
Citation preview
Creating netbeans project:
1.File->New project->Java Web->Web Application->
2.Application Name: SavingAccount
Project Location: choose the location for saving the project
3. Click next and select the server as Glassfish server
4.Click the services tab in the Netbeans
Click the JavaDB and start the server
Then right click on the JavaDB and click create database
Enter database name as BookBank
Username: oracle user namePassword: oracle password and click ok.
5.Right click on the SavingsAccount WebApplication project select entity class option Enter entity class name as BookBank and package name as bank , make sure that create persistence unit check box is selected
Select the data source name and create a new data source with the name BookBank and database as BookBank//BookBank.javapackage bank;
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();
}
public BookBank(String title, String author, double price) {
super();
this.title = title;
this.author = author;
this.price = price;
}
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = 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 classJava class name: BookCatalogInterfacePackage name: bank
//BookCatalogInterface.javapackage bank;
import javax.ejb.Remote;
import java.util.Collection;
@Remote
public interface BookCatalogInterface {
public void addBook(String title, String author, double price);
public Collection 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;
protected BookBank book;
protected Collection bookList;
public void addBook(String title, String author, double price) {
// Initialize the form
if (book == null)
book = new BookBank(title, author, price);
em.persist(book);
}
public Collection getAllBooks() {
bookList=em.createQuery("from BookBank b").getResultList();
return bookList;
}
public void persist(Object object) {
em.persist(object);
}
}
8. Right Click on the SavingAccount webApplication project create a new jsp
JSP file name: WebClient
WebClient.jsp
Record added
Book ID:
Title:
Author:
Price:
9. open the index.jsp and copy the following code
Library
Library
Enter the Title:
Enter Author name:
Enter Price:
10. compile all the java files and run the index.jsp file
Enter the input
Enter the details in the form and click submit
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) );