aspose file tools*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Searching problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Searching problem" Watch "Searching problem" New topic
Author

Searching problem

AmitCdac Grover
Greenhorn

Joined: Mar 30, 2005
Posts: 26
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
Sanju Thomas
Ranch Hand

Joined: Dec 29, 2004
Posts: 243
Use your home interface to access the finder methods, Those finder methods will either return a Component interface reference, or Collection component interface reference. In your case findByPrimaryKey will return the Component interface reference and the rest two will return the Collection component interface reference. Once you got the Component interface reference you can invoke the business method using it.
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
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..


As of EJB 2.0, entity beans must must be CMP. There's no doubt/choice/alternative/question about it. You cannot make it a BMP. This is something you need to be clear of.


SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
Chetan Sahasrabudhe
Ranch Hand

Joined: Aug 09, 2004
Posts: 75
As of EJB 2.0, entity beans must must be CMP. There's no doubt/choice/alternative/question about it. You cannot make it a BMP. This is something you need to be clear of.


I was under the impression that if one wants to use CMR then CMP is must. otherwise writing BMP is perfectly legal in ejb 2.0 as well.

even HF and Mastering EJB is talking about BMPs.


Thanks & Regards<br />Chetan
Ganeshji Marwaha
Greenhorn

Joined: Sep 16, 2004
Posts: 27
Originally posted by Chengwei Lee:


As of EJB 2.0, entity beans must must be CMP. There's no doubt/choice/alternative/question about it. You cannot make it a BMP. This is something you need to be clear of.


I am not particularly sure about this Lee...

I would say that as of EJB 2.0, entity beans *must* be a CMT. Even if the bean is either of CMP or BMP, it still has to use CMT.

Ofcourse, to use CMRs we still have no choice other than using CMPs.

But still, my point is that BMPs are still allowed.


-- Ganeshji Marwaha<br />SCJP<br />SCWCD<br />SCBCD<br />MCP, MCP+SB, MCSD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Searching problem
 
Similar Threads
java.lang.ClassCastException
Problem in bmp entity bean while running client?
JDBC VRE 2.0 BatchUpdate
ejbLoad is not getting called
Error: [ ejbFindByPrimaryKey ]