Java SQL Type getSQLException(String message, String sqlState, Exception exception)

Here you can find the source of getSQLException(String message, String sqlState, Exception exception)

Description

Returns a SQLException and sets the initCause if running with a 1.4+ JVM.

License

Open Source License

Declaration

public static SQLException getSQLException(String message,
        String sqlState, Exception exception) 

Method Source Code

//package com.java2s;
import java.lang.reflect.Method;
import java.sql.*;

public class Main {
    /**/*from w  ww . j a v a2 s.  c o m*/
     * Returns a SQLException and sets the initCause if running with a 1.4+ JVM.
     */
    public static SQLException getSQLException(String message,
            String sqlState, Exception exception) {
        return getSQLException(message, sqlState, Integer.MIN_VALUE,
                exception);
    }

    /**
     * Returns a SQLException and sets the initCause if running with a 1.4+ JVM.
     */
    public static SQLException getSQLException(String message,
            String sqlState, int vendorCode, Exception exception) {
        SQLException sqlException;

        if (exception != null) {
            if (message == null) {
                message = exception.getMessage();
            } else {
                message += ": " + exception.getMessage();
            }
        }

        if (vendorCode == Integer.MIN_VALUE) {
            sqlException = new SQLException(message, sqlState);
        } else {
            sqlException = new SQLException(message, sqlState, vendorCode);
        }

        if (exception != null) {
            Class sqlExceptionClass = sqlException.getClass();
            Class[] parameterTypes = new Class[] { Throwable.class };
            Object[] arguments = new Object[] { exception };

            try {
                Method initCauseMethod = sqlExceptionClass.getMethod(
                        "initCause", parameterTypes);

                initCauseMethod.invoke(sqlException, arguments);
            } catch (NoSuchMethodException e) {
                // Ignore; this method does not exist in older JVM's.
            } catch (Exception e) {
                // Ignore all other exceptions, do not prevent the main exception
                // from being returned if reflection fails for any reason...
            }
        }

        return sqlException;
    }
}

Related

  1. getReturnClass(String typeName)
  2. getSemestre(Integer actionformation_id, Connection connection, String actionformation_libellecourt)
  3. getSerialPrimaryKeyTypeString(Connection conn)
  4. getSimpleTypeString(String dbType)
  5. getSQLAttributeType(int sqlType)
  6. getSQLParameterSubstring(String value, int type)
  7. getSqlType(int type)
  8. getSqlType(Object param)
  9. getSqlType(String cubridType)