Example usage for java.sql SQLException getNextException

List of usage examples for java.sql SQLException getNextException

Introduction

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

Prototype

public SQLException getNextException() 

Source Link

Document

Retrieves the exception chained to this SQLException object by setNextException(SQLException ex).

Usage

From source file:Main.java

public static void main(String[] args) {
    try {//from   ww w  .j a va 2 s. c  om
        throwWarning();
    } catch (SQLException e) {
        System.err.println("An SQL exception occurred: " + e);
        e.printStackTrace();
        while ((e = e.getNextException()) != null) {
            System.err.println("Contained reason: " + e);
        }
    }
}

From source file:Main.java

public static void main(String[] args) {
    Connection conn = null;/*from  www  . j  av  a2 s. co  m*/
    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

public static void main(String[] argv) throws Exception {

    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);/*from   w  w  w. java 2s .  c o m*/

    String serverName = "127.0.0.1";
    String portNumber = "1433";
    String mydatabase = serverName + ":" + portNumber;
    String url = "jdbc:JSQLConnect://" + mydatabase;
    String username = "username";
    String password = "password";

    Connection connection = DriverManager.getConnection(url, username, password);

    try {
        connection.createStatement().execute("select wrong");
    } catch (SQLException e) {
        while (e != null) {
            String message = e.getMessage();

            String sqlState = e.getSQLState();

            int errorCode = e.getErrorCode();

            driverName = connection.getMetaData().getDriverName();
            if (driverName.equals("Oracle JDBC Driver") && errorCode == 123) {
            }

            e = e.getNextException();
        }
    }

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    try {/*w  w w  . j  a  va  2  s.  co  m*/
        Connection conn = getHSQLConnection();

        conn.setAutoCommit(false);
        Statement st = conn.createStatement();
        st.executeUpdate("create table survey (id int,name varchar(30));");
        st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");
        st = conn.createStatement();
        ResultSet rs = st.executeQuery("SELECT * FROM survey");

        outputResultSet(rs);

        rs.close();
        st.close();
        conn.close();
    } catch (SQLException sqle) {
        do { // loop through each exception
             // do something with each exception
            System.err.println("Exception occurred:\nMessage: " + sqle.getMessage());
            System.err.println("SQL state: " + sqle.getSQLState());
            System.err.println("Vendor code: " + sqle.getErrorCode() + "\n----------------");
        } while ((sqle = sqle.getNextException()) != null);
    }
}

From source file:org.trafodion.rest.util.JdbcT4Util.java

public static void main(String args[]) {
    Options options = new Options();
    options.addOption("h", "help", false, "print this message");
    options.addOption("q", "queryText", true, "SQL query text to execute");
    HelpFormatter formatter = new HelpFormatter();

    CommandLine commandLine;/* w w w . j  a  v  a2s .  c  o  m*/
    String queryText = null;
    StringBuilder sb = new StringBuilder();

    try {
        commandLine = new GnuParser().parse(options, args);
        if (commandLine.hasOption("help")) {
            formatter.printHelp("JdbcT4Util", options);
            System.exit(0);
        }
        if (commandLine.hasOption("queryText")) {
            queryText = commandLine.getOptionValue("q");
        }
        if (queryText == null) {
            formatter.printHelp("JdbcT4Util", options);
            System.exit(-1);
        }
    } catch (Exception e) {
        formatter.printHelp("JdbcT4Util", options);
        System.exit(-1);
    }

    try {
        JdbcT4Util jdbcT4Util = new JdbcT4Util();
        TrafT4Connection connection = (TrafT4Connection) jdbcT4Util.getConnection();

        //Regular Statement
        Statement stmt = connection.createStatement();
        ResultSet rs = stmt.executeQuery(queryText);
        JSONArray js = new JSONArray();
        js = convertResultSetToJSON(rs);
        if (LOG.isErrorEnabled())
            LOG.error(js.toString());
        rs.close();
        stmt.close();

        //PreparedStatement with explain
        TrafT4PreparedStatement pStmt = (TrafT4PreparedStatement) connection.prepareStatement(queryText,
                "SQL_CURSOR_DEMO");
        rs = pStmt.executeQuery("SELECT * FROM TABLE(explain(null, 'SQL_CURSOR_DEMO'))");
        js = new JSONArray();
        js = convertResultSetToJSON(rs);
        if (LOG.isErrorEnabled())
            LOG.error(js.toString());
        rs.close();
        stmt.close();

        System.exit(0);
    } catch (SQLException e) {
        SQLException nextException = e;
        do {
            sb.append(nextException.getMessage());
            sb.append("\nSQLState   " + nextException.getSQLState());
            sb.append("\nError Code " + nextException.getErrorCode());
        } while ((nextException = nextException.getNextException()) != null);
        if (LOG.isErrorEnabled())
            LOG.error("SQLException [" + sb.toString() + "]");
        System.exit(1);
    } catch (Exception e) {
        e.printStackTrace();
        sb.append(e.getMessage());
        if (LOG.isDebugEnabled())
            LOG.debug("Exception [" + sb.toString() + "]");
        System.exit(1);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    try {//from w  w w. ja  va 2 s .  c o  m
        Connection conn = getConnection();
        Statement st = conn.createStatement();

        st.executeUpdate("create table survey (id int,myDate DATE );");
        String INSERT_RECORD = "insert into survey(id, myDate) values(?, ?)";

        PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD);
        pstmt.setString(1, "1");
        java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime());
        pstmt.setDate(2, sqlDate);

        pstmt.executeUpdate();

        ResultSet rs = st.executeQuery("SELECT * FROM survey");

        rs.close();
        st.close();
        conn.close();
    } catch (SQLException e) {
        while (e != null) {
            String errorMessage = e.getMessage();
            System.err.println("sql error message:" + errorMessage);

            // This vendor-independent string contains a code.
            String sqlState = e.getSQLState();
            System.err.println("sql state:" + sqlState);

            int errorCode = e.getErrorCode();
            System.err.println("error code:" + errorCode);
            // String driverName = conn.getMetaData().getDriverName();
            // System.err.println("driver name:"+driverName);
            // processDetailError(drivername, errorCode);
            e = e.getNextException();
        }

    }
}

From source file:SimpleProgramToAccessOracleDatabase.java

public static void main(String[] args) throws SQLException {
    Connection conn = null; // connection object
    Statement stmt = null; // statement object
    ResultSet rs = null; // result set object
    try {//from   ww w .ja v  a 2 s.c o m
        conn = getConnection(); // without Connection, can not do much
        // create a statement: This object will be used for executing
        // a static SQL statement and returning the results it produces.
        stmt = conn.createStatement();
        // start a transaction
        conn.setAutoCommit(false);

        // create a table called cats_tricks
        stmt.executeUpdate("CREATE TABLE cats_tricks " + "(name VARCHAR2(30), trick VARCHAR2(30))");
        // insert two new records to the cats_tricks table
        stmt.executeUpdate("INSERT INTO cats_tricks VALUES('mono', 'r')");
        stmt.executeUpdate("INSERT INTO cats_tricks VALUES('mono', 'j')");

        // commit the transaction
        conn.commit();

        // set auto commit to true (from now on every single
        // statement will be treated as a single transaction
        conn.setAutoCommit(true);

        // get all of the the records from the cats_tricks table
        rs = stmt.executeQuery("SELECT name, trick FROM cats_tricks");

        // iterate the result set and get one row at a time
        while (rs.next()) {
            String name = rs.getString(1); // 1st column in query
            String trick = rs.getString(2); // 2nd column in query
            System.out.println("name=" + name);
            System.out.println("trick=" + trick);
            System.out.println("==========");
        }
    } catch (ClassNotFoundException ce) {
        // if the driver class not found, then we will be here
        System.out.println(ce.getMessage());
    } catch (SQLException e) {
        // something went wrong, we are handling the exception here
        if (conn != null) {
            conn.rollback();
            conn.setAutoCommit(true);
        }

        System.out.println("--- SQLException caught ---");
        // iterate and get all of the errors as much as possible.
        while (e != null) {
            System.out.println("Message   : " + e.getMessage());
            System.out.println("SQLState  : " + e.getSQLState());
            System.out.println("ErrorCode : " + e.getErrorCode());
            System.out.println("---");
            e = e.getNextException();
        }
    } finally { // close db resources
        try {
            rs.close();
            stmt.close();
            conn.close();
        } catch (Exception e) {
        }

    }
}

From source file:org.apache.sqoop.util.LoggingUtils.java

/**
 * Log every exception in the chain if//from  w w w  .  ja  v a 2  s. co m
 * the exception is a chain of exceptions.
 */
public static void logAll(Log log, SQLException e) {
    log.error("Top level exception: ", e);
    e = e.getNextException();
    int indx = 1;
    while (e != null) {
        log.error("Chained exception " + indx + ": ", e);
        e = e.getNextException();
        indx++;
    }
}

From source file:com.dalabs.droop.util.LoggingUtils.java

public static void logAll(Log log, String message, SQLException e) {
    log.error(message == null ? "Top level exception: " : message, e);
    e = e.getNextException();
    int indx = 1;
    while (e != null) {
        log.error("Chained exception " + indx + ": ", e);
        e = e.getNextException();//from w ww  .  j av  a  2s  . c  o  m
        indx++;
    }
}

From source file:Main.java

/**
 * Print the stack trace for a SQLException to a 
 * specified PrintWriter. //from w  ww .  j  a  va 2 s.c om
 *
 * @param e SQLException to print stack trace of
 * @param pw PrintWriter to print to
 */
public static void printStackTrace(SQLException e, PrintWriter pw) {

    SQLException next = e;
    while (next != null) {
        next.printStackTrace(pw);
        next = next.getNextException();
        if (next != null) {
            pw.println("Next SQLException:");
        }
    }
}