Java SQLException getAllSqlExceptionMessages(SQLException t)

Here you can find the source of getAllSqlExceptionMessages(SQLException t)

Description

Same as #getAllSqlExceptionMessages(Throwable,boolean) with the "include exception name" parameter set to true.

License

Open Source License

Parameter

Parameter Description
t the top sql exception (may be <code>null</code>)

Return

string containing the SQL Exception's message and its next exception's messages in order

Declaration

public static String getAllSqlExceptionMessages(SQLException t) 

Method Source Code

//package com.java2s;
/*/*w ww . j  ava 2 s  .  com*/
 * RHQ Management Platform
 * Copyright (C) 2005-2008 Red Hat, Inc.
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License, version 2, as
 * published by the Free Software Foundation, and/or the GNU Lesser
 * General Public License, version 2.1, also as published by the Free
 * Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License and the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License
 * and the GNU Lesser General Public License along with this program;
 * if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

import java.sql.SQLException;
import java.util.ArrayList;

public class Main {
    private static final String ARROW = " -> ";

    /**
     * Returns all the messages for the SQL Exception and all of its next exceptions in one long string.
     *
     * @param  t                    the top SQL Exception (may be <code>null</code>)
     * @param  includeExceptionName if <code>true</code>, the exception name will prefix all messages
     *
     * @return string containing the SQL Exception's message and its next exception's messages in order
     */
    public static String getAllSqlExceptionMessages(SQLException t,
            boolean includeExceptionName) {
        StringBuffer ret_message = new StringBuffer();

        if (t != null) {
            String[] msgs = getAllSqlExceptionMessagesArray(t,
                    includeExceptionName);
            ret_message.append(msgs[0]);

            for (int i = 1; i < msgs.length; i++) {
                ret_message.append(ARROW);
                ret_message.append(msgs[i]);
            }
        } else {
            ret_message.append(">> sql exception was null <<");
        }

        return ret_message.toString();
    }

    /**
     * Same as {@link #getAllSqlExceptionMessages(Throwable, boolean)} with the "include exception name" parameter set
     * to <code>true</code>.
     *
     * @param  t the top sql exception (may be <code>null</code>)
     *
     * @return string containing the SQL Exception's message and its next exception's messages in order
     */
    public static String getAllSqlExceptionMessages(SQLException t) {
        return getAllSqlExceptionMessages(t, true);
    }

    /**
     * Returns all the messages for the SQL Exception and all of its causes.
     *
     * @param  t                    the top SQL Exception (may be <code>null</code>)
     * @param  includeExceptionName if <code>true</code>, the exception name will prefix all messages
     *
     * @return strings containing the SQL Exception's message and its next exception's messages in order
     */
    public static String[] getAllSqlExceptionMessagesArray(SQLException t,
            boolean includeExceptionName) {
        ArrayList<String> list = new ArrayList<String>();

        if (t != null) {

            String tMessage = t.getMessage();
            if (includeExceptionName) {
                list.add(t.getClass().getName() + ":" + tMessage);
            } else {
                list.add((tMessage != null) ? tMessage : t.getClass()
                        .getName());

            }

            while ((t.getNextException() != null)
                    && (t != t.getNextException())) {
                String msg;

                t = t.getNextException();
                tMessage = t.getMessage();
                if (includeExceptionName) {
                    msg = t.getClass().getName() + ":" + tMessage;
                } else {
                    msg = (tMessage != null) ? tMessage : t.getClass()
                            .getName();
                }

                list.add(msg + "(error-code=" + t.getErrorCode()
                        + ",sql-state=" + t.getSQLState() + ")");
            }
        }

        return list.toArray(new String[list.size()]);
    }

    /**
     * Same as {@link #getAllSqlExceptionMessagesArray(SQLException, boolean)} with the "include exception name"
     * parameter set to <code>true</code>.
     *
     * @param  t the top sql exception (may be <code>null</code>)
     *
     * @return strings containing the SQL Exception's message and its next exception's messages in order
     */
    public static String[] getAllSqlExceptionMessagesArray(SQLException t) {
        return getAllSqlExceptionMessagesArray(t, true);
    }
}

Related

  1. extractErrorCode(SQLException sqlException)
  2. extractNestedSQLExceptions( SQLException exception)
  3. extractSqlStateClassCode(SQLException sqlException)
  4. getAllMessages(Throwable t, boolean includeExceptionName)
  5. getAllMessagesArray(Throwable t, boolean includeExceptionName)
  6. getAllSqlExceptionMessagesArray(SQLException t, boolean includeExceptionName)
  7. getClassWhichThrowsException(SQLException e)
  8. getExceptionCause(Throwable e)
  9. getExceptionMessage(Throwable t)