Title: Professional EJB
ISBN: 1861005083
US Price: $ 59.99
Canadian Price:
C$ 89.95
UK Price: £ 47.99
© Wrox Press Limited, US and UK.

Reviews : Java Books :
Professional EJB : The EJB 2.0 Entity Model

The findInDate() method retrieves instances between two timestamps. Like any home method implementation, it cannot use any data specific to a particular instance:

public Collection ejbFindInDate(Date from,
	Date to) throws FinderException {
Connection con = null;
try {
InitialContext initial = new InitialContext();
DataSource ds =
  (DataSource) initial.lookup
  ("java:comp/env/jdbc/DefaultDS");
con = ds.getConnection();
PreparedStatement ps =
   con.prepareStatement
   ("SELECT id FROM measurements " +
		"WHERE eventTimestamp >= ? " +
		"AND eventTimestamp <= ?");
  ps.setTimestamp
  (1, new java.sql.Timestamp(from.getTime()));
  ps.setTimestamp
  (2, new java.sql.Timestamp(to.getTime()));
  ResultSet rs = ps.executeQuery();
  LinkedList results = new LinkedList();
  while (rs.next()) {
	results.add(new Long(rs.getLong(1)));
  }
	  return results;
} catch (java.sql.SQLException sqle) {
	sqle.printStackTrace();

	throw new EJBException(sqle);
} catch (javax.naming.NamingException ne) {
	ne.printStackTrace();
	throw new EJBException(ne);
} finally {
	if (con != null) {
		try {
			con.close();
		} catch (Exception ex) {}
	}
}
}

Note: Color coded lines should be on one line, they were split for formatting purposes.

The ejbCreate() method is the only method in this class that modifies the database:

public Long ejbCreate(long measurementId, Date timestamp,
		String eventName,
		double magnitude) throws CreateException {
	if ((timestamp == null) || (eventName == null)) {	
		throw new CreateException
		("Null values not allowed.");
}
this.id = measurementId;
this.eventTimestamp = timestamp;
this.eventName = eventName;
this.eventMagnitude = magnitude;
Connection con = null;
try {
	InitialContext initial = new InitialContext();
	DataSource ds =
		(DataSource) initial.lookup
		("java:comp/env/jdbc/DefaultDS");
	con = ds.getConnection();
	PreparedStatement ps =
	  con.prepareStatement
	  ("INSERT INTO measurements (id, " +
	   "eventTimestamp, eventName, eventMagnitude) " +
	   "VALUES (?,?,?,?)");
	ps.setLong(1, id);
	ps.setTimestamp
	(2, new java.sql.Timestamp(eventTimestamp.getTime()));
	ps.setString(3, eventName);
	ps.setDouble(4, eventMagnitude);
	ps.executeUpdate();
	return new Long(measurementId);
} catch (Exception e) {
	e.printStackTrace();
	throw new CreateException();
} finally {
	if (con != null) {
		try {
			con.close();
		} catch (Exception ex) {}
	}
}
}

public void ejbPostCreate(long measurementId, Date timestamp,
		String eventName,
		double magnitude) throws CreateException {}

Note: Color coded lines should be on one line, they were split for formatting purposes.

This is a fairly standard ejbLoad() method. However, we do not implement ejbStore() because there are no circumstances in which we modify the bean instance:

public void ejbLoad() {
	Connection con = null;
	Long tmpId = (Long) ctx.getPrimaryKey();
	id = tmpId.longValue();
	try {
		InitialContext initial = new InitialContext();
		DataSource ds =
		  (DataSource) initial.lookup
		  ("java:comp/env/jdbc/DefaultDS");
		con = ds.getConnection();
		PreparedStatement ps =
		  con.prepareStatement
		  ("SELECT eventTimestamp, eventName, " +
			"eventMagnitude " +
			"FROM measurements where id = ?");
		ps.setLong(1, id);
		ResultSet rs = ps.executeQuery();
		if (!rs.next()) {
			throw new EJBException
			("Object not found");
		}
		eventTimestamp = 
		new Date(rs.getTimestamp(1).getTime());
		if (rs.wasNull()) {
			throw new EJBException
			("Null in database not allowed.");
		}
		eventName = rs.getString(2);
		if (rs.wasNull()) {
			throw new EJBException
			("Null in database not allowed.");
		}
		eventMagnitude = rs.getDouble(3);
		if (rs.wasNull()) {
			throw new EJBException
			("Null in database not allowed.");
		}
	} catch (java.sql.SQLException sqle) {
		sqle.printStackTrace();
		throw new EJBException(sqle);
	} catch (javax.naming.NamingException ne) {
		ne.printStackTrace();
		throw new EJBException(ne);
	} finally {
	if (con != null) {
		try {
			con.close();
		} catch (Exception ex) {}
	}
}
}

public void ejbStore() {}
public void ejbRemove() {}
public void ejbActivate() {}
public void ejbPassivate() {}
public void setEntityContext(EntityContext ctx) {
	this.ctx = ctx;
}
public void unsetEntityContext() {
	this.ctx = null;
}
}

Note: Color coded lines should be on one line, they were split for formatting purposes.

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.