Example usage for java.sql SQLException setNextException

List of usage examples for java.sql SQLException setNextException

Introduction

In this page you can find the example usage for java.sql SQLException setNextException.

Prototype

public void setNextException(SQLException ex) 

Source Link

Document

Adds an SQLException object to the end of the chain.

Usage

From source file:Main.java

public static void main(String[] args) {
    Connection conn = null;//from w  w w.  j  a v a2 s . com
    Statement stmt = null;
    ResultSet rs = null;
    try {
        String driver = "oracle.jdbc.driver.OracleDriver";
        Class.forName(driver).newInstance();
        System.out.println("Connecting to database...");
        String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:ORCL";
        conn = DriverManager.getConnection(jdbcUrl, "yourName", "mypwd");
        stmt = conn.createStatement();
        try {
            rs = stmt.executeQuery("Select * from no_table_exisits");
        } catch (SQLException seRs) {
            String exMsg = "Message from MySQL Database";
            String exSqlState = "Exception";
            SQLException mySqlEx = new SQLException(exMsg, exSqlState);
            seRs.setNextException(mySqlEx);
            throw seRs;
        }
    } catch (SQLException se) {
        int count = 1;
        while (se != null) {
            System.out.println("SQLException " + count);
            System.out.println("Code: " + se.getErrorCode());
            System.out.println("SqlState: " + se.getSQLState());
            System.out.println("Error Message: " + se.getMessage());
            se = se.getNextException();
            count++;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * <p>/*from www. j  a  v  a 2 s.  c om*/
 * Execute a DDL statement
 * </p>
 */
public static void executeDDL(String ddl) throws SQLException {
    Connection conn = getLocalConnection();

    // now register the function
    print(ddl);
    try {
        PreparedStatement createStatement = conn.prepareStatement(ddl);

        createStatement.execute();
        createStatement.close();
    } catch (SQLException t) {
        SQLException s = new SQLException("Could not execute DDL:\n" + ddl);

        s.setNextException(t);

        throw s;
    }
}

From source file:com.nortal.petit.converter.util.ResultSetHelper.java

public static Object get(Type type, ResultSet rs, String column) throws SQLException {
    try {//from   w  ww.  java 2 s .c  o  m
        ColumnRetrievalStrategy<?> strategy = type == null ? new NopColumnRetrievalStrategy()
                : type.getColumnRetrievalStrategy();

        return strategy.getColumnValue(rs, column);

    } catch (SQLException e) {
        SQLException exception = new SQLException("Failed to get data from column " + column);
        e.setNextException(exception);
        throw e;
    }
}

From source file:gridool.util.jdbc.JDBCUtils.java

private static void rethrow(SQLException cause, String[] sqls) throws SQLException {
    StringBuilder msg = new StringBuilder(cause.getMessage());
    msg.append(" Query: ");
    if (sqls == null) {
        msg.append("[]");
    } else {/* w  ww.  j ava 2s  .  c  om*/
        msg.append(Arrays.asList(sqls));
    }
    SQLException e = new SQLException(msg.toString());
    e.setNextException(cause);
    throw e;
}

From source file:gridool.util.jdbc.JDBCUtils.java

/**
 * Throws a new exception with a more informative error message.
 * //from w w w  .  ja  v  a  2s.c  om
 * @param cause The original exception that will be chained to the new 
 * exception when it's rethrown. 
 * @param sql The query that was executing when the exception happened.     
 * @param params The query replacement paramaters; <code>null</code> is a 
 * valid value to pass in.
 */
private static void rethrow(SQLException cause, String sql, Object[] params) throws SQLException {
    StringBuilder msg = new StringBuilder(cause.getMessage());
    msg.append(" Query: ");
    msg.append(sql);
    msg.append(" Parameters: ");
    if (params == null) {
        msg.append("[]");
    } else {
        msg.append(Arrays.asList(params));
    }
    SQLException e = new SQLException(msg.toString());
    e.setNextException(cause);
    throw e;
}

From source file:jef.database.DbUtils.java

/**
 * SQL?/*from  w  ww. ja  v  a2s.com*/
 * 
 * @param errors
 * @return
 */
public static final SQLException wrapExceptions(Collection<SQLException> errors) {
    if (errors == null || errors.isEmpty())
        return null;
    Iterator<SQLException> iter = errors.iterator();
    SQLException root = iter.next();
    SQLException last = root;
    while (iter.hasNext()) {
        SQLException current = iter.next();
        last.setNextException(current);
        last = current;
    }
    return root;
}

From source file:net.fender.sql.LoadBalancingDataSource.java

@Override
protected ManagedConnection getManagedConnection() throws SQLException {
    ManagedConnection managedConnection = null;
    SQLException exceptionToThrow = new SQLException();
    int tries = 0;
    while (managedConnection == null && tries <= timesToRetry) {
        DataSource dataSource = getNextDataSource();
        try {/*from  w  w w  .ja  va 2  s  . c o m*/
            Connection connection = dataSource.getConnection();
            managedConnection = new ManagedConnection(connection, this);
            if (validateConnectionOnAquire) {
                validateConnection(managedConnection);
            }
        } catch (SQLException e) {
            log.warn("connection failure " + e.getMessage());
            exceptionToThrow.setNextException(e);
            tries++;
        }
    }
    if (managedConnection == null) {
        throw exceptionToThrow;
    }
    return managedConnection;
}

From source file:QueryRunner.java

/**
 * Throws a new exception with a more informative error message.
 * //from   w ww  .  j a v a 2s.  com
 * @param cause The original exception that will be chained to the new 
 * exception when it's rethrown. 
 * 
 * @param sql The query that was executing when the exception happened.
 * 
 * @param params The query replacement parameters; <code>null</code> is a 
 * valid value to pass in.
 * 
 * @throws SQLException if a database access error occurs
 */
protected void rethrow(SQLException cause, String sql, Object[] params) throws SQLException {

    String causeMessage = cause.getMessage();
    if (causeMessage == null) {
        causeMessage = "";
    }
    StringBuffer msg = new StringBuffer(causeMessage);

    msg.append(" Query: ");
    msg.append(sql);
    msg.append(" Parameters: ");

    if (params == null) {
        msg.append("[]");
    } else {
        msg.append(Arrays.asList(params));
    }

    SQLException e = new SQLException(msg.toString(), cause.getSQLState(), cause.getErrorCode());
    e.setNextException(cause);

    throw e;
}

From source file:org.batoo.jpa.core.impl.jdbc.dbutils.AbstractQueryRunner.java

/**
 * Throws a new exception with a more informative error message.
 * /*from w w w . j a v a 2 s .  c om*/
 * @param cause
 *            The original exception that will be chained to the new exception when it's rethrown.
 * 
 * @param sql
 *            The query that was executing when the exception happened.
 * 
 * @param params
 *            The query replacement parameters; <code>null</code> is a valid value to pass in.
 * 
 * @throws SQLException
 *             if a database access error occurs
 */
protected void rethrow(SQLException cause, String sql, Object... params) throws SQLException {

    String causeMessage = cause.getMessage();
    if (causeMessage == null) {
        causeMessage = "";
    }
    final StringBuffer msg = new StringBuffer(causeMessage);

    msg.append(" Query: ");
    msg.append(sql);
    msg.append(" Parameters: ");

    if (params == null) {
        msg.append("[]");
    } else {
        msg.append(Arrays.deepToString(params));
    }

    final SQLException e = new SQLException(msg.toString(), cause.getSQLState(), cause.getErrorCode());
    e.setNextException(cause);

    throw e;
}

From source file:org.batoo.jpa.jdbc.dbutils.QueryRunner.java

/**
 * Throws a new exception with a more informative error message.
 * //from  w ww.  j a v  a2s  .co  m
 * @param cause
 *            The original exception that will be chained to the new exception when it's rethrown.
 * 
 * @param sql
 *            The query that was executing when the exception happened.
 * 
 * @param params
 *            The query replacement parameters; <code>null</code> is a valid value to pass in.
 * @return SQLException if a database access error occurs
 */
private SQLException convertSqlException(SQLException cause, String sql, Object... params) {
    String causeMessage = cause.getMessage();
    if (causeMessage == null) {
        causeMessage = "";
    }

    final StringBuffer msg = new StringBuffer(causeMessage);

    msg.append(" Query: ");
    msg.append(sql);
    msg.append(" Parameters: ");

    if (params == null) {
        msg.append("[]");
    } else {
        msg.append(Arrays.deepToString(params));
    }

    final SQLException e = new SQLException(msg.toString(), cause.getSQLState(), cause.getErrorCode());
    e.setNextException(cause);

    return e;
}