Java Utililty Methods SQLException

List of utility methods to do SQLException

Description

The list of methods to do SQLException are organized into topic(s).

Method

String[]getAllMessagesArray(Throwable t, boolean includeExceptionName)
Returns all the messages for the throwable and all of its causes.
ArrayList<String> list = new ArrayList<String>();
if (t != null) {
    String msg;
    String tMessage = t.getMessage();
    if (includeExceptionName) {
        msg = t.getClass().getName() + ":" + tMessage;
    } else {
        msg = (tMessage != null) ? tMessage : t.getClass().getName();
...
StringgetAllSqlExceptionMessages(SQLException t)
Same as #getAllSqlExceptionMessages(Throwable,boolean) with the "include exception name" parameter set to true.
return getAllSqlExceptionMessages(t, true);
String[]getAllSqlExceptionMessagesArray(SQLException t, boolean includeExceptionName)
Returns all the messages for the SQL Exception and all of its causes.
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()]);
StringgetClassWhichThrowsException(SQLException e)
get Class Which Throws Exception
for (StackTraceElement element : e.getStackTrace()) {
    if (element.getClassName().contains("pfreiberg")) {
        String[] tokens = element.getClassName().split("\\.");
        return tokens[tokens.length - 1];
return "";
ThrowablegetExceptionCause(Throwable e)
Gets the cause from an exception.
if (e instanceof java.sql.SQLException) {
    return e;
} else if (e instanceof java.sql.BatchUpdateException) {
    return e;
} else if (e.getCause() != null) {
    return getExceptionCause(e.getCause());
} else {
    return e;
...
StringgetExceptionMessage(Throwable t)
Returns the cause of a trigger exception (BatchupdateException).
if (t.getCause() instanceof BatchUpdateException
        && ((BatchUpdateException) t.getCause()).getNextException() != null) {
    final BatchUpdateException bue = (BatchUpdateException) t.getCause();
    return bue.getNextException().getMessage();
return t.getMessage();
StringgetFullMessage(SQLException exception)
Retrieves the full message from SQLException.
StringBuilder errorTrace = new StringBuilder(exception.getMessage());
SQLException next = exception;
while (next != null) {
    errorTrace.append("; ");
    errorTrace.append(next.getMessage());
    next = next.getNextException();
Throwable cause = exception.getCause();
...
SQLExceptiongetNextExceptionFromLastCause(Exception e)
get Next Exception From Last Cause
Throwable lastCause = getLastCause(e);
SQLException nextException = null;
if (lastCause instanceof SQLException) {
    nextException = ((SQLException) lastCause).getNextException();
return nextException;
StringBuffergetSingleSQLExceptionCause(SQLException e)
get Single SQL Exception Cause
StringBuffer message = new StringBuffer();
message.append("SQL State:").append(e.getSQLState()).append("\n");
message.append("Error Code:").append(e.getErrorCode()).append("\n");
StringWriter string_writer = new StringWriter();
PrintWriter writer = new PrintWriter(string_writer);
e.printStackTrace(writer);
message.append(string_writer.getBuffer());
return message;
...
intgetSQLErrorCode(SQLException ex_)
Sometimes the SQL error code isn't set and we have to pull it from the message.
int error = ex_.getErrorCode();
if (error != 0)
    return error;
String msg = ex_.toString();
int pos = msg.indexOf("ORA-");
if (pos < 0)
    return -9999;
pos += 4;
...