MinhDUNGTRAN TRANVANTHEO

Greenhorn
+ Follow
since Jul 12, 2005
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by MinhDUNGTRAN TRANVANTHEO

Good Afternoon Friends,

I have an easy test question relating to bean management persistent that i could not figure out. Please tell me what is wrong with the code.

I constantly got javax.ejb.EJBException error: Object state not saved
when i tested getname() method for findByPrimaryKey() and findAll() methods.




Here is the code
package org.school.idxc;
import javax.sql.*;
import javax.naming.*;
import javax.ejb.*;
import javax.sql.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Vector;
/**
* Bean implementation class for Enterprise Bean: status
*/
public class statusBean implements javax.ejb.EntityBean {
private javax.ejb.EntityContext myEntityCtx;
private int id;
private String name;
private DataSource ds;
private String dbname = "jdbc/idxc";
private Connection con;
/**
* ejbActivate
*/



public void ejbActivate() {

}

/**
* ejbLoad
*/
public void ejbLoad() {
System.out.println("Entering EJBLoad");
try
{
Integer primaryKey = (Integer) myEntityCtx.getPrimaryKey();
String sqlstmt = "select id, name from from status where id =?";
con = ds.getConnection();
PreparedStatement stmt = con.prepareStatement(sqlstmt);
stmt.setInt (1,primaryKey.intValue());
ResultSet rs = stmt.executeQuery();
if (rs.next())
{
this.id = rs.getInt(1);
this.name = rs.getString (2).trim();
stmt.close();
} // if
else
{
stmt.close();
throw new NoSuchEntityException ("Invalid id " + id);
}// else
} // try
catch (SQLException e)
{
System.out.println("EJBLOad : " + e.getMessage());
} // catch
finally
{
try
{
if (con != null)
con.close();
}// try
catch (SQLException e)
{
System.out.println("EJBLOad finally" + e.getMessage());
} // catch
}// finally
}
/**
* ejbPassivate
*/
public void ejbPassivate() {
}
/**
* ejbRemove
*/
public void ejbRemove() throws javax.ejb.RemoveException {
System.out.println ("Entering ejb Removed");
try
{
String sqlstmt = "delete from status where id=" + id;
con = ds.getConnection();
Statement stmt = con.createStatement();
stmt.executeUpdate(sqlstmt);
stmt.close();
}// try
catch (SQLException e)
{
System.out.println("Ejb Remove" + e.getMessage());
} // catch
finally
{
try
{
if (con!=null)
con.close();
}// try
catch (SQLException e)
{
System.out.println ("EJBRemoved " + e.getMessage());
} // catch
} // finally
}
/**
* ejbStore
*/
public void ejbStore() {
System.out.println("Entering the ejbStore");
try
{
String sqlstmt = "update status set id=" + id + ",name='" + name + "' where id=" + id;
con = ds.getConnection();
Statement stmt = con.createStatement();
if (stmt.executeUpdate(sqlstmt) != 1)
throw new EJBException ("Object state not saved");
stmt.close();
} // try
catch (SQLException e)
{
System.out.println ("EJBStore : " + e.getMessage());
}// catch
finally
{
try
{
if (con != null)
con.close();
} // try
catch(SQLException e)
{
System.out.println ("EJBStore finally " + e.getMessage());
} // catch
} // finally
}
/**
* getEntityContext
*/
public javax.ejb.EntityContext getEntityContext() {
return myEntityCtx;
}
/**
* setEntityContext
*/
public void setEntityContext(javax.ejb.EntityContext ctx) {
myEntityCtx = ctx;
try
{
InitialContext initial = new InitialContext();
ds = (DataSource)initial.lookup(dbname);
} // try
catch (NamingException e)
{
throw new EJBException ("set Entity context : Invalid database");
}// catch
}
/**
* unsetEntityContext
*/
public void unsetEntityContext() {
myEntityCtx = null;
}
/**
* ejbCreate
*/
public Integer ejbCreate(Integer key, String name) throws javax.ejb.CreateException {
this.id = key.intValue();
this.name = name;
System.out.println ("Entering ejbCreated!!!");
try
{
String sqlstmt = "insert into status(id,name) values (" + id + ",'" + (name == null ? "" : name) + "')";
con = ds.getConnection();
Statement stmt = con.createStatement();
stmt.executeUpdate(sqlstmt);
stmt.close();
}// try
catch (SQLException e)
{
System.out.println("EJBCreate : SQLEXception ");
}// catch
finally
{
try
{
if (con!=null)
con.close();
}// try
catch (SQLException e)
{
System.out.println ("EJB Created Finally : SQLException");
e.getMessage();
} // catch
}// finally
this.id = key.intValue();
this.name = name;
return key ;
}
/**
* ejbPostCreate
*/
public void ejbPostCreate(Integer id, String name) throws javax.ejb.CreateException {
}
/**
* ejbFindByPrimaryKey
*/
public Integer ejbFindByPrimaryKey(
Integer key) throws javax.ejb.FinderException {
try
{
String sqlstmt = "select id from status where id=" + key.intValue();
con = ds.getConnection();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sqlstmt);
if (!rs.next())
{
throw new ObjectNotFoundException();
} // if
rs.close();
stmt.close();
} // try
catch (SQLException e)
{
System.out.println ("EJBFindBYPrimaryKey " + e.getMessage());
} // catch
finally
{
try
{
if (con!=null)
con.close();
}// try
catch (SQLException e)
{
System.out.println ("EJB Find by primary key" + e.getMessage());
}// catch
}// finally
return key;
}
/**
* @return Returns the name.
*/
public String getName() {
return this.name;
}


/**
* @return Returns id
*/
public int getId() {
return this.id;
}
/**
* @param name The name to set.
*/
public void setName(String xname) {

this.name = xname;
}

/**
* ejbFindByLastnameContaining
*/

public Enumeration ejbFindAllNamne () throws javax.ejb.FinderException
{
try
{
String sqlstmt = "select id from status order by id";
con = ds.getConnection();
Statement s = con.createStatement();
ResultSet rs = s.executeQuery(sqlstmt);
Vector keys = new Vector();
while (rs.next())
{
keys.add(new Integer(rs.getInt(1)));
}// while
rs.close();
s.close();
con.close();
return keys.elements();
} // try
catch (SQLException e)
{
throw new FinderException (e.toString());
} // catch
}
}