Developing an EJB for Library operations - PowerPoint PPT Presentation

1 / 35
About This Presentation
Title:

Developing an EJB for Library operations

Description:

start-- All programs-- Microsoft Access-- File-- New(Black db) ... Microsoft Access-Driver(*.mdb)-- give DSN name & select the access DB(and if you ... – PowerPoint PPT presentation

Number of Views:65
Avg rating:3.0/5.0
Slides: 36
Provided by: mrk56
Category:

less

Transcript and Presenter's Notes

Title: Developing an EJB for Library operations


1
Developing an EJB for Library operations
MRK Think BIG
By M.Rajendra Kumar www.mrajendrakumar.info
2
MRK Think BIG
Developing an EJB for Library operations
Content of the Power Point Presentation
  • Create the database with MS-Access
  • Create the user DSN Name to the database
  • How to develop an EJB for Library operation?
  • EJB architecture
  • Simple EJB program
  • Requirement of Library operation
  • Implementation Library operation with EJB
  • How to run EJB

3
MRK Think BIG
Developing an EJB for Library operations
  • Required database for Library
  • Create a database in access or oracle
  • start--gtAll programs--gtMicrosoft
    Access--gtFile--gtNew(Black db)
  • --gtgive a db name--gtCreate table in Design
    view--gtcreate the req field name with data type
    --gt save it as giving a name to your table(and
    then if you want give some data to it) --gt close
    the access
  • Create a DNS for the database which you created
  • Start--gtsettings--gtControl Panel--gtAdministrtor
    Tool--gtData Sources(ODBC)--gtAdd(under user
    DSN)--gt
  • Microsoft Access-Driver(.mdb)--gtgive DSN name
    select the access DB(and if you want username
    password) --gtOk--gtOk

4
MRK Think BIG
Developing an EJB for Library operations
3A. EJB architecture
  • Require files
  • Remote Interface
  • Home Interface
  • Bean Class
  • Client Class

5
MRK Think BIG
Developing an EJB for Library operations
3B. Simple EJB program
  • Remote Interface coding

import java.rmi. import javax.ejb. public
interface HelloRemote extends EJBObject //
Required methods declaration which are
communication between client and server public
String sayHello(String str) throws
RemoteException
Indicating that this is a Remote interface
6
MRK Think BIG
Developing an EJB for Library operations
3B. Simple EJB program
  • Home Interface coding

import java.rmi. import javax.ejb. public
interface HelloHome extends EJBHome // ONLY
create() method public HelloRemote create()
throws RemoteException, CreateException
Indicating that this is a Home interface
7
MRK Think BIG
Developing an EJB for Library operations
3B. Simple EJB program
  • Bean Class coding

SessionBean is an interface which has following
methods 1.ejbCreate() 2.ejbRemove() 3.ejbAct
ivate() 4.ejbPassivate() 5.setSessionContext
(SessionContext ctx) You will face abstract
error while compiling if any of method is
missed.
import java.rmi. import javax.ejb. public
class HelloBean implements SessionBean
SessionContext ctx public void ejbCreate()
public String sayHello(String str) throws
RemoteException System.out.println("\nMsg from
Client "str) return "\nMsg from Bean
Hello EJB Client!" //Continue
Indicating that this is a Bean Class
8
MRK Think BIG
Developing an EJB for Library operations
3B. Simple EJB program
  • Bean Class coding

public void ejbPassivate() public void
ejbActivate() public void setSessionContext(S
essionContext ctx) this.ctx ctx public
void ejbRemove()
9
MRK Think BIG
Developing an EJB for Library operations
3B. Simple EJB program
  • Client Class coding

import java.rmi. import javax.naming. //For
Context and InitialContext class import
java.util. // For Properties class public
class HelloClient public static void
main(String arg) try Properties p
new Properties() p.put(Context.INITIAL_CONTEXT_
FACTORY,"weblogic.jndi.WLInitialContextFactory")
p.put(Context.PROVIDER_URL,"t3//localhost7001"
) Context ctx new InitialContext(p) //Cont
inue
10
MRK Think BIG
Developing an EJB for Library operations
3B. Simple EJB program
  • Client Class coding

Object objRef ctx.lookup("HelloBean") Hello
Home homeObj (HelloHome)javax.rmi.PortableRemote
Object.narrow(objRef,HelloHome.class) HelloRemo
te remoteObj homeObj.create() System.out.pri
ntln(remoteObj.sayHello("Hello EJB
server!")) remoteObj.remove()
catch(java.rmi.RemoteException e)
System.out.println("RemoteException "e)
catch(javax.ejb.CreateException e)
System.out.println("CreateException "e)
catch(javax.naming.NamingException e)
System.out.println("NamingException "e)
catch(Exception e) System.out.println("javaX
RemoteException "e)
11
MRK Think BIG
Developing an EJB for Library operations
3C. Requirement of Library operation
  • Required methods for Library
  • Req operation
  • Sort (Display)
  • Search
  • Insert
  • Delete
  • Issue
  • Return

Req () for client server comm., String
queryOp(String item) String updateOp(int accno,
String name, String author, int price, int
isAvailable) String updateOp(int accno, char ird)
No.s args with data type One - string One -
string Multiple Book struc., One - int One -
int One - int
12
MRK Think BIG
Developing an EJB for Library operations
3D. Implementation Library operation with EJB
  • Remote Interface coding

import java.rmi. import javax.ejb.
public interface HelloRemote extends EJBObject
public interface LibraryRemote extends EJBObject
// Required methods declaration which are
communication between client and server
public String sayHello(String str) throws
RemoteException
public String queryOperation(String str) throws
RemoteException public String
updateOperation(int accno1,char ird) throws
RemoteException public String
updateOperation(int accno1, String title1, String
author1, int price1, int isavailable1) throws
RemoteException
13
MRK Think BIG
Developing an EJB for Library operations
3D. Implementation Library operation with EJB
  • Home Interface coding

import java.rmi. import javax.ejb. // ONLY
create() method
public interface HelloHome extends EJBHome
public interface LibraryHome extends EJBHome
public HelloRemote create() throws
RemoteException, CreateException
public LibraryRemote create() throws
RemoteException, CreateException
14
MRK Think BIG
Developing an EJB for Library operations
3D. Implementation Library operation with EJB
  • Bean Class coding

import java.rmi. import javax.ejb. SessionC
ontext ctx
import java.sql. // For the sql
commands public class LibraryBean implements
SessionBean
public class HelloBean implements SessionBean
Connection c Statement stm ResultSet
rs String tableName,qry_str,returnRecordSet,titl
e, author,isavailable int accno, price, i
public void ejbCreate() public String
sayHello(String str) throws RemoteException Syst
em.out.println("\nMsg from Client
"str) return "\nMsg from Bean Hello EJB
Client!" //Continue
15
MRK Think BIG
Developing an EJB for Library operations
3D. Implementation Library operation with EJB
  • Bean Class coding

public void ejbCreate()
public void ejbCreate() tableName
"books" //Table name for the operation
performance try Class.forName("sun.jdbc.odb
c.JdbcOdbcDriver") cDriverManager.getConnection
("jdbcodbcMRKLibrary) stmc.createStatement()
catch(java.lang.ClassNotFoundException
e) System.out.println("ClassNotFoundException
"e) catch(SQLException e) System.out.pri
ntln("Error in ejbCreate method "e)
public String sayHello(String str) throws
RemoteException System.out.println("\nMsg from
Client "str) return "\nMsg from Bean Hello
EJB Client!" //Continue
JDBC ODBC Connection to the database
16
MRK Think BIG
Developing an EJB for Library operations
3D. Implementation Library operation with EJB
  • Bean Class coding

public String queryOperation(String str) throws
RemoteException public String
updateOperation(int accno1,char ird) throws
RemoteException public String
updateOperation(int accno1, String title1, String
author1, int price1, int isavailable1) throws
RemoteException
public String queryOperation(String str) throws
RemoteException public String
updateOperation(int accno1,char ird) throws
RemoteException public String
updateOperation(int accno1, String title1, String
author1, int price1, int isavailable1) throws
RemoteException
public String queryOperation(String str) throws
RemoteException str"SELECT FROM
"tableName" "str String crntRecordSet"\nS.NO
ACCESS NUM TITLE \t\t\tAUTHOR \t\tPRICE
\tAVAILABLITY" System.out.println("\nReceived
query in queryOperation methods
"str) i0 try rsstm.executeQuery(str
) while(rs.next()) i
accnors.getInt("accno") titlers.getString(
"title") authorrs.getString("author")
pricers.getInt("price") //Continue
  • Req operation
  • Sort (Display)
  • Search
  • Insert
  • Delete
  • Issue
  • Return

17
MRK Think BIG
Developing an EJB for Library operations
3D. Implementation Library operation with EJB
  • Bean Class coding

isavailable rs.getBoolean("isavailable") ?
"Yes" "No" System.out.print("\n "i")
"accno" \t"title" \t\t"author"
\t\t"price" \t"isavailable"
") crntRecordSet"\n "i") "accno"
\t"title" \t\t"author" \t\t"price"
\t"isavailable" "
if (i0) crntRecordSet"\t\t\t ltlt Book is not
available gtgt" catch (Exception e)
System.out.println("Error in selectOperation
method "e) return "REC"crntRecordSe
t
  • Req operation
  • Sort (Display)
  • Search
  • Insert
  • Delete
  • Issue
  • Return

18
MRK Think BIG
Developing an EJB for Library operations
3D. Implementation Library operation with EJB
  • Bean Class coding

public String updateOperation(int accno1,char
ird) throws RemoteException
public String updateOperation(int accno1, char
ird) throws RemoteException String
str"" if(isAvailableAccNo(accno1))
if(ird'I') //Issue str updateTheRecord("UPD
ATE "tableName" set isavailable0 WHERE
isavailable1 AND accno"accno1)
if(ird'R') //Return str
updateTheRecord("UPDATE "tableName" set
isavailable1 WHERE isavailable0 AND
accno"accno1) if(ird'D')
//Delete str updateTheRecord("DELETE FROM
"tableName" WHERE accno"accno1) else str
"ERR\t\t\t ltlt Sorry, the AccessNumber is NOT
available. gtgt" return str //Continue
  • Req operation
  • Sort (Display)
  • Search
  • Insert
  • Delete
  • Issue
  • Return

19
MRK Think BIG
Developing an EJB for Library operations
3D. Implementation Library operation with EJB
  • Bean Class coding

public String updateOperation(int accno1, String
title1, String author1, int price1, int
isavailable1) throws RemoteException
public String updateOperation(int accno1, String
title1, String author1, int price1, int
isavailable1) throws RemoteException String
str"" if(!isAvailableAccNo(accno1)) str
updateTheRecord("INSERT INTO "tableName"
values("accno1",'"title1"','"author1"',"pr
ice1",'"isavailable1"')") else str
"ERR\t\t\t ltlt Sorry, the AccessNumber is
already exist. gtgt" return str //Continue
  • Req operation
  • Sort (Display)
  • Search
  • Insert
  • Delete
  • Issue
  • Return

20
MRK Think BIG
Developing an EJB for Library operations
3D. Implementation Library operation with EJB
  • Bean Class coding

public boolean isAvailableAccNo(int accno1 )
boolean isAvailfalse try rs
stm.executeQuery("SELECT FROM "tableName)
while(rs.next()) if( accno1
rs.getInt("accno")) isAvail true break
catch (Exception e)
System.out.println("Error in isAvailableAccNo
method "e) return isAvail //Continue
Checking that is any book in this access no?
21
MRK Think BIG
Developing an EJB for Library operations
3D. Implementation Library operation with EJB
  • Bean Class coding

public String updateTheRecord(String
update) String comment "\t\t\t ltlt
"update.substring(0,6) int totalRecordUpdated
-1 System.out.println("\nReceived query in
updateTheRecord method "update) comment"
operation successfully made gtgt" try
totalRecordUpdated stm.executeUpdate(update)
catch (Exception e) System.out.println("Erro
r in updateTheRecord method "e) comment
"\t\t\t ltlt "update.substring(0,6)
comment" operation IS NOT successfully made
gtgt" System.out.println("Total number of
record(s) is updated "totalRecordUpdated) com
ment"\n\t\t\t\t ltlt for only "totalRecordUpdated
" book(s) gtgt" return "COM"comment
Executing the updateOpeation method
22
MRK Think BIG
Developing an EJB for Library operations
3D. Implementation Library operation with EJB
  • Bean Class coding

public void ejbPassivate() public void
ejbActivate() public void setSessionContext(S
essionContext ctx) this.ctx ctx
public void ejbRemove() try stm.close(
) c.close() catch(SQLException
e) System.out.println("Error in ejbRemove
method "e)
public void ejbRemove()
Release (De-allocate) the resources i.e here JDBC
ODBC Connection
23
MRK Think BIG
Developing an EJB for Library operations
3D. Implementation Library operation with EJB
  • Client Class coding

import java.rmi. import javax.naming. //For
Context and InitialContext class import
java.util. // For Properties class
public class LibraryClient
public class HelloClient
public static void main(String arg)
try Properties p new Properties() p.put(C
ontext.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLIn
itialContextFactory") p.put(Context.PROVIDER_UR
L,"t3//localhost7001") Context ctx new
InitialContext(p) //Continue
public static int getNumber()
DataInputStream dis int in0 try
dis new DataInputStream(System.in) in
Integer.parseInt(dis.readLine()) if (inlt0)
throw new Exception() catch(Exception e)
System.out.println("\n \t\t\t ltlt Must be in
valid number format only gtgt") in
getNumber() return in
//Continue
Method for get input as numeric format from user
24
MRK Think BIG
Developing an EJB for Library operations
3D. Implementation Library operation with EJB
  • Client Class coding

public static String getString()
DataInputStream dis String str""
try dis new DataInputStream(System.in) str
dis.readLine() catch(Exception e)
System.out.println("Exception "e)
return str
Method for get input as String format from user
public static void main(String arg)
try Properties p new Properties() p.put(C
ontext.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLIn
itialContextFactory") p.put(Context.PROVIDER_UR
L,"t3//localhost7001") //Continue
public static void main(String arg)
String recvResultSet"" String
author,title int accno-1, price,isAvailable,ch,
ch2 //Continue
25
MRK Think BIG
Developing an EJB for Library operations
3D. Implementation Library operation with EJB
  • Client Class coding

try Properties p new Properties()
p.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory") p.put(C
ontext.PROVIDER_URL,"t3//localhost7001") Conte
xt ctx new InitialContext(p) Object objRef
ctx.lookup("HelloBean") HelloHome homeObj
(HelloHome) javax.rmi.PortableRemoteObject.narrow
(objRef,HelloHome.class) HelloRemote remoteObj
homeObj.create() //Continue
try Properties p new Properties()
p.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory") p.put(C
ontext.PROVIDER_URL,"t3//localhost7001") Conte
xt ctx new InitialContext(p) Object objRef
ctx.lookup(LibraryBean") LibraryHome homeObj
(LibraryHome) javax.rmi.PortableRemoteObject.narro
w (objRef,LibraryHome.class) LibraryRemote
remoteObj homeObj.create() //Continue
Assigning user define properties to InitialContext
Creating remote object for server comm.,
26
MRK Think BIG
Developing an EJB for Library operations
3D. Implementation Library operation with EJB
  • Client Class coding

Overview of the switch case to call the necessary
methods
Do switch (userkey) case 1 Sorting
order(display) switch (userkey) case 1
Access no case 2 Title case 2
Search case 3 Issue case 4 Return case 5
Insert case 6 Delete case 7 Exit
while(!Exit)
27
MRK Think BIG
Developing an EJB for Library operations
3D. Implementation Library operation with EJB
  • Client Class coding

System.out.println(remoteObj.sayHello("Hello
EJB server!"))
do System.out.print("\n\n\nSelect the
operation\n")
System.out.print("\n1.Sort(Display)\n2.Search\n3.I
ssue\n 4.Return\n5.Insert\n6.Delete\n7.Exit ")
ch getNumber() switch(ch) case 1
System.out.print("(SORT) Select the sorting
ORDER) System.out.print("\n\t1.Access
number\n\t2.Title\n\t3.Main menu-Back (Default)
") ch2 getNumber() switch(ch2) case 1
System.out.print("\t(ORDER BY ACCESS
NUMBER) "remoteObj.queryOperation(" ORDER BY
accno")) break case 2 System.out.print("\t(O
RDER BY TITLE) "remoteObj.queryOperation("
ORDER BY title")) break break
28
MRK Think BIG
Developing an EJB for Library operations
3D. Implementation Library operation with EJB
  • Client Class coding

case 2 System.out.print("(SEARCH) ")
System.out.print("\nEnter the book title ")
title getString() System.out.print("\n"remot
eObj.queryOperation(" WHERE title LIKE
'"title"' ORDER BY title")) break case 3
System.out.print("(ISSUE) ")
System.out.print("\t(ORDER BY AVAILABLITY)
" remoteObj.queryOperation(" WHERE isavailable1
ORDER BY title")) System.out.print("\nEnter
the book access number ") accno
getNumber() if (accno!0) System.out.print("\
n"remoteObj.updateOperation(accno,'I')) break
29
MRK Think BIG
Developing an EJB for Library operations
3D. Implementation Library operation with EJB
  • Client Class coding

case 4 System.out.print("(RETURN) ")
System.out.print("\nEnter the book access number
") accno getNumber() if
(accno!0) System.out.print("\n"remoteObj.updat
eOperation(accno,'R')) break case 5
System.out.print("(INSERT) ")
System.out.print("\nEnter the book Access Number
") do if (accno0)
System.out.println("\n \t\t\t ltlt Must be in
valid number format only gtgt") accno
getNumber() while (accno0)
30
MRK Think BIG
Developing an EJB for Library operations
3D. Implementation Library operation with EJB
  • Client Class coding

System.out.print("Enter the book Title
") title getString() System.out.print("Enter
the book Author ") author getString() Syste
m.out.print("Enter the book Price ") price
getNumber() System.out.print("Is this book
issuable?\n(Enter 0 to NO) ") isAvailable
getNumber()0 ? 0 1 System.out.print("\n"
remoteObj.updateOperation(accno,title,author,price
,isAvailable)) break case 6
System.out.print("(DELETE) ")
System.out.print("\nEnter the book access
number\n(Enter 0 to CANCEL this operation) ")
accno getNumber() if (accno!0) System.out.
print("\n"remoteObj.updateOperation(accno,'D'))
break
31
MRK Think BIG
Developing an EJB for Library operations
3D. Implementation Library operation with EJB
  • Client Class coding

case 7 break while(ch!7) rem
oteObj.remove() catch(java.rmi.RemoteExcep
tion e) System.out.println("RemoteException
"e) catch(javax.ejb.CreateException e)
System.out.println("CreateException "e)
catch(javax.naming.NamingException e)
System.out.println("NamingException "e)
catch(Exception e) System.out.println("javaX
RemoteException "e)
32
MRK Think BIG
Developing an EJB for Library operations
3E. How to run EJB
  • Deploy and run the EJB program
  • Once your MS-Access i.e database, DSN connection
    and the EJB code are successful completed. Then
    you can start compile and run the EJB program as
    follows
  • Go to the command prompt and type as follows for
    set the weblogic environment
  • D\bea\weblogic81\server\bin
  • Setwlsenv

Client-CMD
  • Goto to your EJB program directory in the prompt
    for compiling using as follows
  • Z
  • Cd Z\mw\ejb\lib
  • Javac .java

Client-CMD
  • Start the weblogic server which located in
    D\bea\weblogic81\server\bin\startWLS.cmd

Server-CMD
EJB BUILDER-CMD EJB BUILDER-APP
  • Start the weblogic builder which located in
    D\bea\weblogic81\server\bin\startWLBuilder.cmd

33
MRK Think BIG
Developing an EJB for Library operations
3E. How to run EJB
Deploy and run the EJB program
EJB BUILDER-APP
  • Now connect the EJB builder to the server,
    once server completely started
  • Open the EJB program folder to create xml
    files and jar file which located in Z\mw\ejb\lib

EJB BUILDER-APP
  • Check and set/type appropriate file name to
    deployment.
  • EJB client jar file set as ? Z\mw\ejb\lib\test.
    jar
  • Select the LibraryBean which appear left- side
    view for setting
  • General tab
  • EJB Name set as ? LibraryBean
  • Transacting type set as ? Container
  • JNDI name set as(Which you given the client
    progs lookup stm) -gt LibraryBean
  • Get in Classes tab
  • Bean set as ? LibraryBean
  • Home set as ? LibraryHome
  • Remote set as ? LibraryRemote

EJB BUILDER-APP
  • Save it and deploy it Click tools and then
    Deploy Module in the EJB builder

EJB BUILDER-APP
34
MRK Think BIG
Developing an EJB for Library operations
3E. How to run EJB
Deploy and run the EJB program
  • Now you can run the client program after
    successful EJB deployment
  • Get back to the command prompt and run
    LibraryClient program
  • Java LibraryClient

Client-CMD
  • Now you can start use the EJB library program to
    view and do the library operation

Client-CMD
  • You can also view some change in the server side
    by doing some operation the client side

Server-CMD
Client-CMD
35
MRK Think BIG
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question Question
Question Question Question Question
Developing an EJB for Library operations
  • ? Question(s) ?

www.mrajendrakumar.info
Write a Comment
User Comments (0)
About PowerShow.com