0% found this document useful (0 votes)
26 views

Entity Bean Programm

The document provides steps to create a NetBeans project that allows adding, retrieving, and displaying book records from a database table using JSP and Java EE technologies. The key steps are: 1. Create a Java Web application project in NetBeans and select a Glassfish application server. 2. Define an entity class and interface for managing book records. 3. Create a stateless session bean that uses JPA to persist and retrieve book records. 4. Develop a JSP page to display a form for adding books and a table of all books.

Uploaded by

Kishan Chand
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Entity Bean Programm

The document provides steps to create a NetBeans project that allows adding, retrieving, and displaying book records from a database table using JSP and Java EE technologies. The key steps are: 1. Create a Java Web application project in NetBeans and select a Glassfish application server. 2. Define an entity class and interface for managing book records. 3. Create a stateless session bean that uses JPA to persist and retrieve book records. 4. Develop a JSP page to display a form for adding books and a table of all books.

Uploaded by

Kishan Chand
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 12

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 name
Password: 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.java
package 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 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;

protected BookBank book;


protected Collection <BookBank> 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 <BookBank>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
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="bank.*, javax.naming.*, java.util.*"%>

<%!
private BookCatalogInterface bci = null;
String s1,s2,s3;
Collection list;

public void jspInit() {


try {

InitialContext ic = new InitialContext();


bci = (BookCatalogInterface) ic.lookup("java:module/BookCatalogBean");

System.out.println("Loaded Bank Bean");

} catch (Exception ex) {


System.out.println("Error:"+
ex.getMessage());
}
}
public void jspDestroy() {
bci = null;
}
%>
<%

try {
s1 = request.getParameter("t1");

s2 = request.getParameter("aut");
s3 = request.getParameter("price");

if ( s1 != null && s2 != null && s3 != null) {


Double price= new Double(s3);
bci.addBook(s1, s2, price.doubleValue());
System.out.println("Record added:");
%>
<p>
<b>Record added</b>
<p>

<%
}
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>

<%
}

}// end of try


catch (Exception e) {
e.printStackTrace ();
}
%>
9. open the index.jsp and copy the following code
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Library</title>
</head>

<body bgcolor="pink">
<h1>Library</h1>
<hr>

<form action="Webclient.jsp" method="POST">


<p>Enter the Title:
<input type="text" name="t1" size="25"></p>
<br>
<p>Enter Author name:
<input type="text" name="aut" size="25"></p>
<br>
<p>Enter Price:
<input type="text" name="price" size="25"></p>

<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

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)
);

You might also like