001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.shiro.util; 020 021import java.sql.Connection; 022import java.sql.ResultSet; 023import java.sql.SQLException; 024import java.sql.Statement; 025 026import org.slf4j.Logger; 027import org.slf4j.LoggerFactory; 028 029/** 030 * A set of static helper methods for managing JDBC API objects. 031 * <p/> 032 * <em>Note:</em> Some parts of this class were copied from the Spring Framework and then modified. 033 * They were copied here to prevent Spring dependencies in the Shiro core API. The original license conditions 034 * (Apache 2.0) have been maintained. 035 * 036 * @since 0.2 037 */ 038public final class JdbcUtils { 039 040 /** 041 * Private internal log instance. 042 */ 043 private static final Logger LOGGER = LoggerFactory.getLogger(JdbcUtils.class); 044 045 /** 046 * Private constructor to prevent instantiation. 047 */ 048 private JdbcUtils() { 049 } 050 051 /** 052 * Close the given JDBC Connection and ignore any thrown exception. 053 * This is useful for typical finally blocks in manual JDBC code. 054 * 055 * @param connection the JDBC Connection to close (may be <tt>null</tt>) 056 */ 057 public static void closeConnection(Connection connection) { 058 if (connection != null) { 059 try { 060 connection.close(); 061 } catch (SQLException ex) { 062 if (LOGGER.isDebugEnabled()) { 063 LOGGER.debug("Could not close JDBC Connection", ex); 064 } 065 } catch (Throwable ex) { 066 if (LOGGER.isDebugEnabled()) { 067 LOGGER.debug("Unexpected exception on closing JDBC Connection", ex); 068 } 069 } 070 } 071 } 072 073 /** 074 * Close the given JDBC Statement and ignore any thrown exception. 075 * This is useful for typical finally blocks in manual JDBC code. 076 * 077 * @param statement the JDBC Statement to close (may be <tt>null</tt>) 078 */ 079 public static void closeStatement(Statement statement) { 080 if (statement != null) { 081 try { 082 statement.close(); 083 } catch (SQLException ex) { 084 if (LOGGER.isDebugEnabled()) { 085 LOGGER.debug("Could not close JDBC Statement", ex); 086 } 087 } catch (Throwable ex) { 088 if (LOGGER.isDebugEnabled()) { 089 LOGGER.debug("Unexpected exception on closing JDBC Statement", ex); 090 } 091 } 092 } 093 } 094 095 /** 096 * Close the given JDBC ResultSet and ignore any thrown exception. 097 * This is useful for typical finally blocks in manual JDBC code. 098 * 099 * @param rs the JDBC ResultSet to close (may be <tt>null</tt>) 100 */ 101 public static void closeResultSet(ResultSet rs) { 102 if (rs != null) { 103 try { 104 rs.close(); 105 } catch (SQLException ex) { 106 if (LOGGER.isDebugEnabled()) { 107 LOGGER.debug("Could not close JDBC ResultSet", ex); 108 } 109 } catch (Throwable ex) { 110 if (LOGGER.isDebugEnabled()) { 111 LOGGER.debug("Unexpected exception on closing JDBC ResultSet", ex); 112 } 113 } 114 } 115 } 116 117}