From: <fu...@us...> - 2010-01-07 20:06:01
|
Revision: 1013 http://cishell.svn.sourceforge.net/cishell/?rev=1013&view=rev Author: fugu13 Date: 2010-01-07 20:05:55 +0000 (Thu, 07 Jan 2010) Log Message: ----------- Add some database utilities. Typical usage will be like so: execute() { //get out database Connection connection = DatabaseUtilities.connect(database); try { //do stuff } catch (SQLException e) { } catch(FooException e) { } finally { DatabaseUtilities.closeConnectionQuietly(connection); } } Added Paths: ----------- trunk/core/org.cishell.utilities/src/org/cishell/utilities/DatabaseUtilities.java Added: trunk/core/org.cishell.utilities/src/org/cishell/utilities/DatabaseUtilities.java =================================================================== --- trunk/core/org.cishell.utilities/src/org/cishell/utilities/DatabaseUtilities.java (rev 0) +++ trunk/core/org.cishell.utilities/src/org/cishell/utilities/DatabaseUtilities.java 2010-01-07 20:05:55 UTC (rev 1013) @@ -0,0 +1,28 @@ +package org.cishell.utilities; + +import java.sql.Connection; +import java.sql.SQLException; + +import org.cishell.framework.algorithm.AlgorithmExecutionException; +import org.cishell.service.database.Database; + +public class DatabaseUtilities { + public static void closeConnectionQuietly(Connection connection) { + try { + connection.close(); + } catch (SQLException e) { + //quietly! Use this only in finally blocks, pretty much, and right before throwing exceptions that will leave the scope of the Connection object. + } + } + + public static Connection connect(Database database) + throws AlgorithmExecutionException { + Connection connection; + try { + connection = database.getConnection(); + } catch (SQLException e) { + throw new AlgorithmExecutionException("Unable to communicate with the database.", e); + } + return connection; + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |