Friday, October 17, 2008

EJB Sample codes

Give Sample code of Stateless SessionBean?
HelloHome.java
-----------------------
package com.bhumana.ejb;
import javax.ejb.*;
import java.rmi.*;
public interface HelloHome extends EJBHome
{ public HelloRemote create()throws RemoteException,CreateException; }


HelloRemote.java
----------------------
package com.bhumana.ejb;
import javax.ejb.*;
import java.rmi.*;
public interface HelloRemote extends EJBObject
{ public String hello(String name)throws RemoteException; }

HelloBean.java
-----------------------
package com.bhumana.ejb;
import javax.ejb.*;
import java.rmi.*;
public class HelloBean implements SessionBean
{
public void setSessionContext(SessionContext sc){}
public void ejbCreate(){}
public void ejbActivate(){}
public void ejbPassivate(){}
public void ejbRemove(){}

public String hello(String name)
{ return "hi welcome " +name; }
}

ejb-jar.xml
---------------------




HelloEJB
com.bhumana.ejb.HelloHome
com.bhumana.ejb.HelloRemote
com.bhumana.ejb.HelloBean
Stateless
Container





HelloEJB
hello

Required




weblogic-ejb-jar.xml
-------------------------------



HelloEJB
sudhajndi



HelloClient.java
-----------------------
package com.bhumana.ejb;
import java.util.*;
import javax.naming.*;
public class HelloClient
{ public static void main(String as[]) {
Hashtable ht=new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL,"t3://localhost:7001");
try {
Context ctx=new InitialContext(ht);
Object o=ctx.lookup("sudhajndi");
HelloHome h=(HelloHome)o;
HelloRemote r=h.create();
String str=r.hello("sudhakar");
System.out.println(str);
}catch(Exception e) { System.out.println(e); }
}}

Give me sample code of StatefullBean?

HelloHome.java
-----------------------
package com.bhumana.ejb;
import javax.ejb.*;
import java.rmi.*;
public interface HelloHome extends EJBHome
{ public HelloRemote create(String name)throws RemoteException,CreateException;
public HelloRemote create(String email,String phone)throws RemoteException,CreateException; }

HelloRemote.java
----------------------
package com.bhumana.ejb;
import javax.ejb.*;
import java.rmi.*;
import java.util.*;
public interface HelloRemote extends EJBObject
{ public ArrayList get(String State)throws RemoteException; }

HelloBean.java
--------------------
package com.bhumana.ejb;
import javax.ejb.*;
import java.rmi.*;
import java.util.*;
public class HelloBean implements SessionBean
{ String name,phone,email,state;

public void setSessionContext(SessionContext sc){}
public void ejbCreate(String name) { this.name=name; }
public void ejbCreate(String phone,String email) { this.phone=phone; this.email=email; }
public void ejbActivate(){} public void ejbPassivate(){} public void ejbRemove(){}
public ArrayList get(String state)
{ this.state=state;
ArrayList al=new ArrayList();
al.add(name); al.add(phone); al.add(email); al.add(state); return al; }
}

HelloClient.java
---------------------
package com.bhumana.ejb;
import java.util.*;
import javax.naming.*;
public class HelloClient
{ public static void main(String as[]) {
Hashtable ht=new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL,"t3://localhost:7001");
ArrayList al=new ArrayList();
try {
Context ctx=new InitialContext(ht);
Object o=ctx.lookup("sudhajndi");
HelloHome h=(HelloHome)o;
HelloRemote r=h.create("sudhakar");
HelloRemote r1=h.create("sd@bsr.com","98");
al=r1.get("ka");
System.out.println(al);
}catch(Exception e)
{
System.out.println(e);
}
}
}

Give me sample code of Bmp Example?

AccountHome.java
--------------------------
package com.bhumana.entity;
import javax.ejb.*;
import java.rmi.*;
import java.util.*;
public interface AccountHome extends EJBHome
{
public AccountRemote create(String ano, String aname,double bal)throws
RemoteException,CreateException;
public AccountRemote findByPrimaryKey(String ano)throws FinderException,RemoteException;
public Collection findAllAccounts()throws RemoteException,FinderException;
}

AccountRemote.java
---------------------------
package com.bhumana.entity;
import javax.ejb.*;
import java.rmi.*;
import java.util.*;
public interface AccountRemote extends EJBObject
{
public double deposit(double amt)throws RemoteException;
public double withdraw(double amt)throws RemoteException;
public ArrayList getAll()throws RemoteException;
}

AccountBean.java
-----------------------
package com.bhumana.entity;
import javax.ejb.*;
import java.sql.*;
import javax.sql.*;
import java.util.*;
import javax.naming.*;
public class AccountBean implements EntityBean
{
String ano,aname;
double bal;
Connection con=null;
ArrayList al=new ArrayList();
public void setEntityContext(EntityContext ec){}
public void unsetEntityContext(){}

public String ejbCreate(String ano,String aname,double bal)
{
this.ano=ano; this.aname=aname; this.bal=bal;
try{ getDBConnection();
PreparedStatement ps=con.prepareStatement("insert into account values(?,?,?)");
ps.setString(1,ano); ps.setString(2,aname); ps.setDouble(3,bal);
int x=ps.executeUpdate();
if(x==1) System.out.println("record is inserterd");
else
System.out.println("record is not inserterd");
}catch(Exception e)
{ System.out.println(e); }
return ano; }

public void ejbPostCreate(String ano,String aname,double bal){}
public void ejbActivate()
{
try{
getDBConnection();
}catch(Exception e){}

}
public void ejbPassivate()
{
try{
if(con!=null)
con.close();
}catch(Exception e){}
}

public void ejbLoad()
{
try{

getDBConnection();
PreparedStatement ps=con.prepareStatement("select * from account where ano=?");
ps.setString(1,ano);
ResultSet rs=ps.executeQuery();
while(rs.next())
{
ano=rs.getString(1);
aname=rs.getString(2);
bal=rs.getDouble(3);
}

}catch(Exception e)
{
System.out.println(e);
}
}
public void ejbStore()
{
try{
getDBConnection();
PreparedStatement ps=con.prepareStatement("update account set bal=? where ano=?");
ps.setDouble(1,bal);
ps.setString(2,ano);
int x=ps.executeUpdate();
}catch(Exception e)
{
System.out.println(e);
}
}
public void ejbRemove(){}
public String ejbFindByPrimaryKey(String ano)
{
try{
getDBConnection();
PreparedStatement ps=con.prepareStatement("select * from account where ano=?");
ps.setString(1,ano);
ResultSet rs=ps.executeQuery();
while(rs.next())
{
ano=rs.getString(1);
aname=rs.getString(2);
bal=rs.getDouble(3);
}
}catch(Exception e)
{ System.out.println(e); }

return ano;
}
public Collection ejbFindAllAccounts()
{
try{

getDBConnection();
PreparedStatement ps=con.prepareStatement("select * from account");
ResultSet rs=ps.executeQuery();
while(rs.next())
{
ano=rs.getString(1);
al.add(ano);
}
}catch(Exception e)
{
System.out.println(e);
}
return al;
}
public double deposit(double amt)
{
bal=bal+amt;
return bal;
}
public double withdraw(double amt)
{
if(amt<=bal)
bal=bal-amt;
return bal;
}
public ArrayList getAll()
{
al.add(ano);
al.add(aname);
al.add(new Double(bal));
return al;
}
private void getDBConnection()
{
try{
Context ctx=new InitialContext();
Object o=ctx.lookup("sivajndi");
DataSource ds=(DataSource)o;
con=ds.getConnection();
}catch(Exception e)
{
System.out.println(e);
}
}
}

AccountClient.java
-------------------------
package com.bhumana.entity;
import java.util.*;
import javax.naming.*;
public class AccountClient
{
public static void main(String as[])
{
Hashtable ht=new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL,"t3://localhost:7001");
try{
Context ctx=new InitialContext(ht);
Object o=ctx.lookup("accountjndi");
AccountHome h=(AccountHome)o;
AccountRemote r=h.create("102","bar",10000.00);
AccountRemote r1=h.findByPrimaryKey("101");
ArrayList al=new ArrayList();
al=r1.getAll();
System.out.println(al);
double d=r1.deposit(500.00);
System.out.println(d);
double w=r1.withdraw(200.00);
System.out.println(w);
al=r1.getAll();
System.out.println(al);
Collection col=h.findAllAccounts();
Iterator i=col.iterator();
while(i.hasNext())
{
AccountRemote ar=(AccountRemote)i.next();
al=ar.getAll();
System.out.println(al);
}

}catch(Exception e)
{
System.out.println(e);
}
}
}

Give me sample code of CMP?
AccountHome.java
-------------------------

package com.bhumana.entity;
import javax.ejb.*;
import java.rmi.*;
import java.util.*;
public interface AccountHome extends EJBHome
{
public AccountRemote create(String ano, String aname,double bal)throws
RemoteException,CreateException;
public AccountRemote findByPrimaryKey(String ano)throws FinderException,RemoteException;
public Collection findAllAccounts()throws RemoteException,FinderException;
}

AccountRemote.java
----------------------------
package com.bhumana.entity;
import javax.ejb.*;
import java.rmi.*;
import java.util.*;
public interface AccountRemote extends EJBObject
{
public double deposit(double amt)throws RemoteException;
public double withdraw(double amt)throws RemoteException;
public ArrayList getAll()throws RemoteException;
}

AccountBean.java
------------------------

package com.bhumana.entity;
import javax.ejb.*;
import java.sql.*;
import javax.sql.*;
import java.util.*;
import javax.naming.*;

public abstract class AccountBean implements EntityBean
{
ArrayList al=new ArrayList();
public void setEntityContext(EntityContext ec){}
public void unsetEntityContext(){}
public String ejbCreate(String ano,String aname,double bal)
{
setAno(ano);
setAname(aname);
setBal(bal);

return getAno();
}

public void ejbPostCreate(String ano,String aname,double bal){}
public void ejbActivate(){}
public void ejbPassivate(){}
public void ejbLoad(){}
public void ejbStore(){}
public void ejbRemove(){}
public double deposit(double amt)
{
setBal(getBal()+amt);
return getBal();
}
public double withdraw(double amt)
{
if(amt<=getBal())
setBal(getBal()-amt);
return getBal();
}
public ArrayList getAll()
{
al.add(getAno());
al.add(getAname());
al.add(new Double(getBal()));
return al;
}
public abstract void setAno(String ano);
public abstract void setAname(String aname);
public abstract void setBal(double bal);
public abstract String getAno();
public abstract String getAname();
public abstract double getBal();
}
AccountClient.java
--------------------------
package com.bhumana.entity;
import java.util.*;
import javax.naming.*;
public class AccountClient
{
public static void main(String as[])
{
Hashtable ht=new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL,"t3://localhost:7001");
try{
Context ctx=new InitialContext(ht);
Object o=ctx.lookup("jndijndi");
AccountHome h=(AccountHome)o;
AccountRemote r=h.create("103","siva",20000.00);
AccountRemote r1=h.findByPrimaryKey("103");

ArrayList al=new ArrayList();
al=r1.getAll();
System.out.println(al);
double d=r1.deposit(500.00);
System.out.println(d);
double w=r1.withdraw(200.00);
System.out.println(w);
al=r1.getAll();
System.out.println(al);
Collection col=h.findAllAccounts();
Iterator i=col.iterator();
while(i.hasNext())
{

AccountRemote ar=(AccountRemote)i.next();
al=ar.getAll();
System.out.println(al);
}

}catch(Exception e)
{
System.out.println(e);
}}}

Give sample code of Service Locator?
package com.service;
import javax.ejb.*;
import javax.naming.*;
import java.util.*;
import javax.sql.*;

public class ServiceLocator
{
static ServiceLocator sl;
Context ctx ;
Map cache;
private ServiceLocator()
{

try{
Properties p=new Properties();
p.setProperty(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
p.setProperty(Context.PROVIDER_URL,"t3://localhost:7001");
ctx=new InitialContext(p);
cache=Collections.synchronizedMap(new HashMap());

}catch(Exception e){System.out.println(e);}
}

public static ServiceLocator getInstance()
{
if(sl==null)
sl=new ServiceLocator();

return sl;
}

public EJBLocalHome getLocalHome(String jndiName)
{
EJBLocalHome home=null;
try{
if(cache.containsKey(jndiName))
home=(EJBLocalHome)cache.get(jndiName);
else{
home=(EJBLocalHome)ctx.lookup(jndiName);
cache.put(jndiName,home);
}
}catch(Exception e){System.out.println(e);}
return home;
}

public EJBHome getHome(String jndiName)
{
EJBHome home=null;
try{
if(cache.containsKey(jndiName))
home=(EJBHome)cache.get(jndiName);
else{
home=(EJBHome)ctx.lookup(jndiName);
cache.put(jndiName,home);
}
}catch(Exception e){System.out.println(e);}
return home;
}


public DataSource getDataSource(String jndiName)
{
DataSource ds=null;
try{
if(cache.containsKey(jndiName))
ds=(DataSource)cache.get(jndiName);
else{
ds=(DataSource)ctx.lookup(jndiName);
cache.put(jndiName,ds);
}
}catch(Exception e){System.out.println(e);}
return ds;
}}

No comments: