• Post Reply Bookmark Topic Watch Topic
  • New Topic

bmp ejb code

 
saahil sinha
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
I need some help over this code.Could u pls check this up and tell me if the code is alright or has some problems.
Remote interface
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
public interface Test extends EJBObject
{
public boolean insertDataDir(String emp_no,String emp_name) throws RemoteException;
}
home interface
import java.rmi.RemoteException;
import javax.ejb.*;
public interface TestHome extends EJBHome
{
public Test create(String emp_no,String emp_name) throws RemoteException,CreateException;

public Test findByPrimaryKey(String emp_no) throws RemoteException,FinderException;


}
ejb program
import java.sql.*;
import javax.sql.*;
import java.util.*;
import javax.ejb.*;
import javax.naming.*;
public class TestEJB implements EntityBean
{
public Connection con=null;
public EntityContext ec;
public String emp_name;
public String emp_no;
public String ejbCreate(String emp_no,String emp_name) throws CreateException
{
try
{
insertDataDir(emp_no,emp_name);
}
catch(Exception ex)
{
throw new EJBException("ejbCreate_dir"+ex.getMessage());
}
this.emp_no=emp_no;
this.emp_name=emp_name;
return emp_no;
}


public void ejbPostCreate(String emp_no,String emp_name)
{

}


public void setEntityContext(EntityContext ec)
{
this.ec=ec;
try
{
makeConnection();
}
catch(Exception ex)
{
throw new EJBException("Unable to connect to database"+ex.getMessage());
}
}

public void unsetEntityContext()
{
try
{
con.close();
}
catch(SQLException ex)
{
throw new EJBException("UnsetEntityContext:"+ex.getMessage());
}

}

public void ejbRemove()
{

}

public void ejbActivate()
{

}

public void ejbPassivate()
{
}
public void ejbLoad()
{
}

public void ejbStore()
{
}

public void makeConnection() throws ClassNotFoundException,SQLException
{
try{
InitialContext ic=new InitialContext();
DataSource ds=(DataSource)ic.lookup("MyTest");
con=ds.getConnection();
}
catch(Exception ex)
{
System.out.println(ex);
}
}

public boolean insertDataDir(String emp_no,String emp_name) throws SQLException
{
String insertSt="insert into test(emp_no,emp_name)values('"+emp_no+"','"+emp_name+"')";
System.out.println(insertSt);

Statement stmt=con.createStatement();
boolean result=stmt.execute(insertSt);
return result;
}



public String ejbFindByPrimaryKey(String emp_no)throws FinderException
{
boolean result;
try
{
result=selectByPrimaryKey(emp_no);
}
catch(Exception ex)
{
throw new EJBException("Find By Client Name failed:"+ex.getMessage());
}
if (result)
{
return emp_no;
}
else
{
throw new ObjectNotFoundException("Row for "+emp_no+" not found");
}
}


public boolean selectByPrimaryKey(String emp_no)throws SQLException
{
String selectSt="select emp_no from test where emp_no='"+emp_no+"'";
Statement stmt=con.createStatement();
boolean result=stmt.execute(selectSt);
return result;
}
}
Client program
import java.util.*;
import java.io.*;
import java.rmi.RemoteException;
import javax.ejb.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
public class TestClient
{
String emp_no,emp_name;
static TestHome home;
public static void main(String args[])
{
int option=0;
TestClient cl=new TestClient();
try
{
Context initial=new InitialContext();
Object objref=initial.lookup("MyTest");
home=(TestHome)PortableRemoteObject.narrow(objref,TestHome.class);
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
while(option!=8)
{
System.out.println("select 1-3");
String s=stdin.readLine();
option=Integer.parseInt(s);
switch(option)
{
case 1:
cl.insert();
break;
case 2:
cl.rem();
default:

System.out.println("not found");
}
}
}
catch(Exception ex){}
}


public void insert()
{
try{

BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter no");
emp_no=stdin.readLine();
System.out.print("Enter name");
emp_name=stdin.readLine();


Test tst=home.create(emp_no,emp_name);
System.out.println("Inserted data for"+emp_no);
}
catch(Exception ex)
{
System.out.println(ex);
}

}
Also i would like to know abt the jndi(datasource in my code
DataSource ds=(DataSource)ic.lookup("MyTest") name is specified while connecting to the database.How do i provide a jndi name meaning on wht basis is the jndi name given.
Also how do u give the name in lookup()(in my code Object objref=initial.lookup("MyTest") tht while searching for the ejb in client program to invoke ejb methods
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi guys,
I need some help over this code.Could u pls check this up and tell me if the code is alright or has some problems.

It is very hard to pick errors browsing through such a long code. How about if you first try to run this program and come back and tell us what went wrong and when - compilation or runtime?

Hi guys,
Also i would like to know abt the jndi(datasource in my code
DataSource ds=(DataSource)ic.lookup("MyTest") name is specified while connecting to the database.How do i provide a jndi name meaning on wht basis is the jndi name given.

JNDI name is what you specify when you setup the datasource. Every app server provides some way of setting up datasources. During the process you can also specify other parameters to connect to the source such as host url, user name,password etc.


Also how do u give the name in lookup()(in my code Object objref=initial.lookup("MyTest") tht while searching for the ejb in client program to invoke ejb methods

Hint - Deployment descriptor!
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure whether it is possible to access database connection from setEntityContext().
setEntityContext can access the following:
EntityContext methods: getEJBHome
JNDI access to java:comp/env
There is no mention of resource manager in setEntityContext.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!