Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

an easy test question relating to Bean Management Persistent

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
}
}
 
sunglasses are a type of coolness prosthetic. Check out the sunglasses on this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic