から最終的には定型を削除します -Javaの私はの線に沿って繰り返されるコードの多くを持っている多くの方法でDAOクラスを持って繰り返し試して、キャッチ、DAO
public void method1(...) {
Connection conn = null;
try {
//custom code here
} catch (SQLException e) {
LOG.error("Error accessing the database.", e);
throw new DatabaseException();
} catch (QueryNotFoundException e) {
LOG.error("Error accessing the database.", e);
throw new DatabaseException();
} finally {
if (conn != null)
connectionPool.returnConnection(conn);
}
public void method2(...) {
Connection conn = null;
try {
//different custom code here
} catch (SQLException e) {
LOG.error("Error accessing the database.", e);
throw new DatabaseException();
} catch (QueryNotFoundException e) {
LOG.error("Error accessing the database.", e);
throw new DatabaseException();
} finally {
if (conn != null)
connectionPool.returnConnection(conn);
}
私はこのクラスを再構築したいと思います繰り返しを避けるために、try、catchを一箇所に入れてください。どうすればこれを達成できますか?
Yup、それはJdbcTemplateがオプションではない場合に提案する次のパターンです+1) –