FileDocCategorySizeDatePackage
FinallyForClosure.javaAPI DocExample2220Sun Dec 14 22:47:42 GMT 2003oreilly.hcj.exceptions

FinallyForClosure

public class FinallyForClosure extends Object
Demonstrates using finally for closure.
author
Robert Simmons jr. (kraythe)
version
$Revision: 1.3 $

Fields Summary
Constructors Summary
Methods Summary
public intgetValue(java.sql.Connection conn, java.lang.String sql)
Gets a value from the database.

param
conn The database connection to use.
param
sql The SQL to use.
return
The result value.
throws
SQLException If there is a SQL error.

		int result = 0;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			if (rs.next()) {
				result = rs.getInt(1);
			}
		} catch (final SQLException ex) {
			throw ex;
		} finally {
			if (rs != null) {
				rs.close();
			}
			if (stmt != null) {
				stmt.close();
			}
		}
		return result;
	
public intgetValue2(java.sql.Connection conn, java.lang.String sql)
Gets a value from the database.

param
conn The database connection to use.
param
sql The SQL to use.
return
The result value.
throws
SQLException If there is a SQL error.

		int result = 0;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			if (rs.next()) {
				result = rs.getInt(1);
			}
		} finally {
			if (rs != null) {
				rs.close();
			}
			if (stmt != null) {
				stmt.close();
			}
		}
		return result;