hi all...
i have made bmp bean. my bean is working fine. all the operation are working fine ( insert , delete , update) .but i some problem with searching records..here my code is
Home Interface
----------------------
import javax.ejb.*;
import java.rmi.RemoteException;
import java.util.Collection;
import java.sql.*;
public interface EmpEntityHome extends EJBHome
{
public EmpEntity create(Integer lEmpId,
String lFirstName,String lLastName) throws CreateException,RemoteException;
public EmpEntity findByPrimaryKey(Integer lEmpId) throws FinderException,RemoteException;
public Collection findByFirstName(String firstname) throws FinderException,RemoteException;
public Collection findByLastName(String lastname) throws FinderException,RemoteException;
}
comopenent interfrace
-------------------------------------
import javax.ejb.*;
import java.rmi.RemoteException;
public interface EmpEntity extends EJBObject
{
public String getEmpFirstName() throws RemoteException;
public void setEmpFirstName(String FirstName) throws RemoteException;
public String getEmpLastName() throws RemoteException;
public void setEmpLastName(String LastName) throws RemoteException;
}
Bean
---------------
import javax.ejb.*;
import java.util.*;
import java.sql.*;
import javax.sql.*;
import java.lang.*;
import javax.naming.*;
import javax.rmi.*;
public class EmpEntityBean implements EntityBean
{
public EntityContext contex;
public Integer pEmpId;
public String pEmpFirstName;
public String pEmpLastName;
public int pEmpRowState;
public Integer ejbCreate(Integer lEmpId,String lEmpFirstName,String lEmpLastName) throws CreateException
{
System.out.println("ejbCreate() called.");
if( (lEmpId.intValue() < 1) || ( lEmpFirstName == null) || (lEmpLastName == null) )
throw new CreateException("Invalid Parameters");
this.pEmpId=lEmpId;
this.pEmpFirstName=lEmpFirstName;
this.pEmpLastName=lEmpLastName;
this.pEmpRowState=0;
PreparedStatement pstmt = null;
Connection conn = null;
try
{
conn = this.getConnection();
System.out.println("In try block");
System.out.println( pEmpId.intValue()+ " " + pEmpFirstName+ " " + pEmpLastName+ " " +pEmpRowState);
pstmt = conn.prepareStatement("INSERT INTO EMPDATA VALUES (?,?,?,?)");
pstmt.setInt(1,pEmpId.intValue());
pstmt.setString(2,pEmpFirstName);
pstmt.setString(3,pEmpLastName);
pstmt.setInt(4,pEmpRowState);
if(pstmt.executeUpdate()!=1)
{
throw new CreateException("Failed to add employee into database");
}
System.out.println("Successfully Inserted");
return lEmpId;
}
catch (Exception e)
{
throw new CreateException(e.toString());
}
finally
{
try { if (pstmt != null) pstmt.close(); }
catch (Exception e) {}
try { if (conn != null) conn.close(); }
catch (Exception e) {}
}
}
public void ejbPostCreate(Integer lEmpId,String lEmpFirstName,String lEmpLastName)
{
System.out.println("ejbPostCreate() is called");
}
public String getEmpFirstName()
{
System.out.println("getEmpFirstName() called.");
return pEmpFirstName;
}
public void setEmpFirstName(String lFirstName)
{
System.out.println("setEmpFirstName() called.");
pEmpFirstName=lFirstName;
}
public String getEmpLastName()
{
System.out.println("getEmpLastName() called.");
return pEmpLastName;
}
public void setEmpLastName(String lLastName)
{
System.out.println("setEmpLastName() called.");
pEmpLastName=lLastName;
}
public void ejbActivate()
{
System.out.println("ejbActivate() called.");
}
public void ejbPassivate()
{
System.out.println("ejbPassivate() called.");
}
public void ejbRemove()
{
System.out.println("ejbRemove() called.");
PreparedStatement pstmt = null;
Connection conn = null;
try
{
conn = this.getConnection();
pstmt = conn.prepareStatement("DELETE FROM EMPDATA WHERE EMPID = ?");
pstmt.setInt(1, pEmpId.intValue());
if (pstmt.executeUpdate()!= 1)
{
throw new EJBException("Ejb Remove");
}
}
catch (Exception ex)
{
throw new EJBException(ex.toString());
}
finally
{
try { if (pstmt != null) pstmt.close(); }
catch (Exception e) {}
try { if (conn != null) conn.close(); }
catch (Exception e) {}
}
System.out.println("Successfully Deleted");
}
public void setEntityContext(EntityContext ctx)
{
System.out.println("setEntityContext called");
contex= ctx;
}
public void unsetEntityContext()
{
System.out.println("unsetEntityContext called");
contex = null;
}
public void ejbLoad()
{
System.out.println("ejbLoad() called.");
Integer pk=(Integer)contex.getPrimaryKey();
PreparedStatement pstmt = null;
Connection conn=null;
ResultSet result=null;
try
{
conn = this.getConnection();
pstmt = conn.prepareStatement("SELECT EMPID,EMPLASTNAME,EMPFIRSTNAME FROM EMPDATA WHERE EMPID = ?");
pstmt.setInt(1, pk.intValue());
result = pstmt.executeQuery();
if(result.next())
{
pEmpId=pk;
pEmpFirstName = result.getString("EMPFIRSTNAME");
pEmpLastName = result.getString("EMPLASTNAME");
System.out.println("Successfully Loaded");
}
else
{
throw new NoSuchEntityException();
}
}
catch (Exception ex)
{
throw new EJBException("EmployeeId " + pEmpId + " failed to load from database", ex);
}
finally
{
try { if (pstmt != null) pstmt.close(); }
catch (Exception e) {}
try { if (conn != null) conn.close(); }
catch (Exception e) {}
}
}
public void ejbStore()
{
System.out.println("ejbStore() called.");
PreparedStatement pstmt = null;
Connection conn = null;
int RowState=1;
try
{
conn = this.getConnection();
pstmt = conn.prepareStatement("UPDATE EMPDATA SET EMPFIRSTNAME = ?, EMPLASTNAME = ?,EMPROWSTATE= ? WHERE EMPID = ?");
pstmt.setString(1,pEmpFirstName);
pstmt.setString(2,pEmpLastName);
pstmt.setInt(3,RowState);
pstmt.setInt(4,pEmpId.intValue());
if(pstmt.executeUpdate()!=1)
{
throw new NoSuchEntityException("ejb Store");
}
}
catch (Exception ex) {
throw new EJBException("EMPLOYEE " + pEmpId + " failed to save to database", ex);
}
finally
{
try { if (pstmt != null) pstmt.close(); }
catch (Exception e) {}
try { if (conn != null) conn.close(); }
catch (Exception e) {}
}
System.out.println("Successfully Updated");
}
public Integer ejbFindByPrimaryKey(Integer pk) throws FinderException
{
PreparedStatement pstmt = null;
Connection conn = null;
ResultSet result=null;
try
{
conn=this.getConnection();
System.out.println("ejbFindByPrimaryKey(" + pk + ") called");
pstmt = conn.prepareStatement("SELECT EMPID FROM EMPDATA WHERE EMPID = ?");
pstmt.setInt(1,pk.intValue());
result = pstmt.executeQuery();
if(!result.next())
{
throw new ObjectNotFoundException("Cannot find employee with ID=" + pk);
}
}
catch (Exception e)
{
throw new FinderException(e.toString());
}
finally
{
try { if (pstmt != null) pstmt.close(); }
catch (Exception e) {}
try { if (conn != null) conn.close(); }
catch (Exception e) {}
}
return pk;
}
public Collection ejbFindByFirstName(String lFirstName) throws FinderException
{
PreparedStatement pstmt = null;
Connection conn = null;
ResultSet result = null;
try
{
conn = this.getConnection();
System.out.println("ejbFindByFirstName(" + lFirstName + ") called");
pstmt = conn.prepareStatement("SELECT * FROM EMPDATA WHERE EMPFIRSTNAME = ?");
pstmt.setString(1, lFirstName);
result = pstmt.executeQuery();
Vector keys=new Vector();
while (result.next())
{
keys.addElement(result.getObject("EMPID"));
}
return keys;
}
catch (Exception e)
{
throw new FinderException(e.toString());
}
finally
{
try { if (pstmt != null) pstmt.close(); }
catch (Exception e) {}
try { if (conn != null) conn.close(); }
catch (Exception e) {}
}
}
public Collection ejbFindByLastName(String lLastName) throws FinderException
{
PreparedStatement pstmt =null ;
Connection conn = null ;
ResultSet result=null;
try
{
conn=this.getConnection();
System.out.println("ejbFindByLastName(" + lLastName + ") called");
pstmt = conn.prepareStatement("SELECT EMPID ROM EMPDATA WHERE EMPLASTNAME = ?");
pstmt.setString(1, lLastName);
result = pstmt.executeQuery();
Vector keys = new Vector();
while (result.next())
{
keys.addElement(result.getObject("EMPID"));
}
return keys;
}
catch (Exception e)
{
throw new FinderException(e.toString());
}
finally
{
try { if (pstmt != null) pstmt.close(); }
catch (Exception e) {}
try { if (conn != null) conn.close(); }
catch (Exception e) {}
}
}
private Connection getConnection() throws SQLException
{
try
{
String DriverName ="oracle.jdbc.driver.OracleDriver";
String DBUrl ="jdbc

racle:thin:@192.168.3.27:1521:st";
String uid = "scott";
String pswd="tiger";
/*
* Loading the thin driver
*/
Class.forName(DriverName);
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
/* Getting a connection */
Connection pConnection=(Connection)DriverManager.getConnection(DBUrl,uid,pswd);
return pConnection;
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
}
problem
-------------
problem is that. i don'nt know how search by primary key, lastname ,firstname from he client side . i want empid ,firstname,lastname at the clientside at every search .plz tell me the code how can i get above inforamtion through search at the client side.
regards
amit grover