Closes the connection and print the stack-trace if there is any exception. - Java java.lang

Java examples for java.lang:Exception

Description

Closes the connection and print the stack-trace if there is any exception.

Demo Code


//package com.java2s;

import java.sql.Connection;

import java.sql.SQLException;

public class Main {
    /**/* w ww  .ja va  2 s .c o m*/
     * Closes the connection and print the stack-trace if there is any exception.
     * @param connection
     */
    public static void closeConnection(Connection connection) {
        closeConnection(connection, false);
    }

    /**
     * Closes the connection and print the stack-trace if there is any exception 
     * and the boolean variable <code>silent</code> is <code><b>true</b></code>.
     * 
     * @param connection
     * @param silent
     */
    public static void closeConnection(Connection connection, boolean silent) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                if (!silent) {
                    //TODO: add logger statement
                    e.printStackTrace();
                }
            }
        }
    }
}

Related Tutorials