Listing 1A: The remote interface of the stateless session bean MiddleBean.java
package sameer.ejb;
import javax.ejb.*;
import java.rmi.RemoteException;
public interface Middle extends EJBObject {
public void doTransaction (String customerName,String password,int age) throws RemoteException;
}
Listing 1B: The home interface of the stateless session bean MiddleBean.java
package sameer.ejb;
import javax.ejb.*;
import java.rmi.RemoteException;
public interface MiddleHome extends EJBHome {
Middle create() throws CreateException, RemoteException;
}
Listing 1C: The stateless session bean MiddleBean.java
package sameer.ejb;
import javax.ejb.*;
import java.io.Serializable;
import java.rmi.RemoteException;
import javax.naming.*;
import java.util.*;
import java.sql.*;
public class MiddleBean implements SessionBean {
public void ejbActivate() {}
public void ejbRemove() {}
public void ejbPassivate(){}
public void setSessionContext(SessionContext ctx) {}
public void ejbCreate () throws CreateException {}
public void doTransaction(String customerName, String pass-
word,int age)
throws MyException{
try{
Context ctx = new InitialContext();
TranstestHome home = (TranstestHome)
ctx.lookup("ejb.TranstestHome");
Transtest bean = home.create();
bean.putUser("Sameer","word");
bean.putAge("Sameer",10);
}catch(Exception e){
throw new MyException("an exception occured" +e);
}
}
}
Listing 2A: The remote interface of the stateless session bean TranstestBean
package sameer.ejb;
import javax.ejb.*;
import java.rmi.RemoteException;
public interface Transtest extends EJBObject {
public void putUser (String customerName,String password)
throws RemoteException,MyException;
public void putAge (String customerName,int age) throws RemoteException,MyException;
}
Listing 2B: The home interface of the stateless session bean TranstestBean
package sameer.ejb;
import javax.ejb.*;
import java.rmi.RemoteException;
public interface TranstestHome extends EJBHome {
Transtest create() throws CreateException, RemoteException;
}
Listing 2C: The stateless session bean TranstestBean
package sameer.ejb;
import javax.ejb.*;
import java.io.Serializable;
import java.rmi.RemoteException;
import java.util.*;
import java.sql.*;
public class TranstestBean implements SessionBean {
public void ejbActivate() {}
public void ejbRemove() {}
public void ejbPassivate(){}
public void setSessionContext(SessionContext ctx) {}
public void ejbCreate () throws CreateException {}
public void putUser(String customerName, String password)
throws MyException{
try{
String str = "INSERT INTO USERTABLE (name, pwd) VALUES ('" + customerName + "','" +
password +"')";
System.out.println ("Executing stmt: " + str);
new weblogic.jdbc.jts.Driver();
Connection conn=DriverManager.getConnection("jdbc:weblogic:jts:demoPool") ;
Statement stmt=conn.createStatement();
int rs=stmt.executeUpdate(str);
System.out.println(">>>>Username/Pwd insert succeeded
for "+ customerName +"
and password "+password);
throw new MyException(); // delibrately throw an applica-
tion exception
}catch(SQLException se){
throw new MyException("There was an exceptin "+se);
}
}
public void putAge(String customerName,int age) throws
MyException {
try{
String str ="INSERT INTO AGETABLE (name, age) VALUES ('" +
customerName + "',"
+ age +")";
System.out.println ("Executing stmt: " + str);
// Class.forName("weblogic.jdbc.jts.Driver");
new weblogic.jdbc.jts.Driver();
Connection conn=DriverManager.getConnection("jdbc:weblog-
ic:jts:demoPool");
Statement stmt=conn.createStatement();
int rs=stmt.executeUpdate(str);
System.out.println(">>>>Username/Age insert succeeded for
"+ customerName +"
and age "+age);
}catch(SQLException se){
throw new MyException("There was an exceptin "+se);
}
}
}
Listing 3: The application exception class that is deliberately thrown
package sameer.ejb;
public class MyException extends Exception {
public MyException() {}
public MyException(String message) {
super(message);
}
}
Listing 4: The client for the sessionbean MiddleBean
package sameer.ejb;
import javax.ejb.*;
import javax.naming.*;
import java.rmi.RemoteException;
import java.util.*;
public class Client {
static String url = "t3://localhost:7001";
static String user= null;
static String password= null;
public static void main(String[] args) throws Exception {
Context ctx = getInitialContext();
MiddleHome home = (MiddleHome) ctx.lookup("ejb.MiddleHome");
Middle bean = home.create();
bean.doTransaction("Sameer","word",26);
}
public static Context getInitialContext() throws Exception {
Hashtable h = new Hashtable();
h.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
h.put(Context.PROVIDER_URL, url);
return new InitialContext(h);
}
}
No comments:
Post a Comment