advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement



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.

 Microsoft Visual Studio 2010 Showcase
 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 39
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Windows 7: From Beta to Final Code in One Year
Google Shows Off Chrome OS, Releases Source
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?
Fedora 12 Takes Aim at Linux Networking
Top Supercomputer Nearly Doubles in Speed
Fedora 12 Linux Tackles Virtualization
Apple Gives iPhone Developers App Status Tracker
Novell Sets OpenSUSE 11.2 Free

Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Exploring HTML 5's Audio/Video Multimedia Support
Overriding Virtual Functions? Use C++0x Attributes to Avoid Bugs.
Understanding the Cloud Computing Security Vulnerabilities
Cisco and IBM Target a Greener World
Upgrade to Visual Studio 2010 with the Ultimate Offer

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs