Example usage for java.sql SQLException toString

List of usage examples for java.sql SQLException toString

Introduction

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

Prototype

public String toString() 

Source Link

Document

Returns a short description of this throwable.

Usage

From source file:gov.nih.nci.cadsr.sentinel.database.DBAlertOracle.java

/**
 * Retrieve a string name representation for the "object" id provided.
 *
 * @param table_//from  w w w. j  a v  a 2s .c o  m
 *        The known database table name or null if the method should use a
 *        default based on the col_ value.
 * @param col_
 *        The column name which corresponds to the id_ provided.
 * @param id_
 *        The id of the specific database record desired.
 * @return The "name" from the record, this may correspond to the long_name,
 *         prefferred_name, etc database columns depending on the table
 *         being used.
 */
public String selectName(String table_, String col_, String id_) {
    // Can't work without a column name.
    if (col_ == null)
        return id_;
    if (id_ == null || id_.length() == 0)
        return null;

    // Determine the real table and column names to use.
    int npos = 0;
    String table = table_;
    String name = "long_name";
    String col = col_;
    String extra = "";
    if (table == null || table.length() == 0) {
        int ndx = DBAlertUtil.binarySearch(_DBMAP2, col);
        if (ndx == -1)
            return id_;
        table = _DBMAP2[ndx]._val;
        name = _DBMAP2[ndx]._subs;
        col = _DBMAP2[ndx]._col;
        extra = _DBMAP2[ndx]._xtra;
        if (col.equals("ua_name")) {
            // Is the name cached?
            npos = findName(id_);
            if (npos >= 0)
                return _nameText[npos];
        }
    }

    // Build a select and retrieve the "name".
    String select = "select " + name + " from " + table + " where " + col + " = ?" + extra;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = _conn.prepareStatement(select);
        pstmt.setString(1, id_);
        rs = pstmt.executeQuery();
        name = "";
        while (rs.next())
            name = name + "\n" + rs.getString(1);
        if (name.length() == 0)
            name = null;
        else
            name = name.substring(1);

        if (col.equals("ua_name") && npos < 0) {
            cacheName(-npos, id_, name);
        }
    } catch (SQLException ex) {
        _errorCode = DBAlertUtil.getSQLErrorCode(ex);
        _errorMsg = _errorCode + ": " + select + "\n\n" + ex.toString();
        _logger.error(_errorMsg);
        name = "(*error*)";
    } finally {
        closeCursors(pstmt, rs);
    }
    return (name == null) ? id_ : name;
}

From source file:gov.nih.nci.cadsr.sentinel.database.DBAlertOracle.java

/**
 * Retrieve a more user friendly version of the user id.
 *
 * @param id_/*from   w w w .  j  a va  2s. c  o  m*/
 *        The id as would be entered at logon.
 * @return null if the user id was not found in the sbr.user_accounts table,
 *         otherwise the 'name' value of the matching row.
 */
public String selectUserName(String id_) {
    // Define the SQL select.
    String select = "select uav.name " + "from sbr.user_accounts_view uav, sbrext.user_contexts_view ucv "
            + "where uav.ua_name = ? and ucv.ua_name = uav.ua_name and ucv.privilege = 'W'";
    PreparedStatement pstmt = null;
    String result = null;
    ResultSet rs = null;

    try {
        // Get ready...
        pstmt = _conn.prepareStatement(select);
        pstmt.setString(1, id_);

        // Go!
        rs = pstmt.executeQuery();
        if (rs.next()) {
            // Get the name, remember 1 indexing.
            result = rs.getString(1);
        }
    } catch (SQLException ex) {
        // We've got trouble.
        _errorCode = DBAlertUtil.getSQLErrorCode(ex);
        _errorMsg = _errorCode + ": " + select + "\n\n" + ex.toString();
        _logger.error(_errorMsg);
    } finally {
        closeCursors(pstmt, rs);
    }
    return result;
}

From source file:gov.nih.nci.cadsr.sentinel.database.DBAlertOracle.java

/**
 * Select all the caDSR Concepts/*from   ww  w  . j av a  2 s.c  om*/
 *
 * @return the Concepts
 */
public Vector<ConceptItem> selectConcepts() {
    // Get the context names and id's.
    String select = "SELECT con_idseq, conte_idseq, con_id, version, evs_source, preferred_name, long_name, definition_source, preferred_definition, origin, asl_name "
            + "FROM sbrext.concepts_view_ext WHERE asl_name NOT LIKE 'RETIRED%' "
            + "and origin NOT LIKE 'NCI Thesaurus' " //'NCI Thesaurus' concepts are handled by 'NCI Thesaurus Concept cleanup'
            //+ "and ROWNUM <= 20 "
            + "ORDER BY upper(long_name) ASC";

    Statement stmt = null;
    ResultSet rs = null;
    Vector<ConceptItem> list = null;
    try {
        // Prepare the statement.
        stmt = _conn.createStatement();
        rs = stmt.executeQuery(select);

        // Get the list.
        list = new Vector<ConceptItem>();
        while (rs.next()) {
            ConceptItem rec = new ConceptItem();
            rec._idseq = rs.getString(1); //con_idseq same as ac_idseq
            rec._conteidseq = rs.getString(2); //conte_idseq is context id
            rec._publicID = rs.getString(3); //con_id is public id
            rec._version = rs.getString(4);
            rec._evsSource = rs.getString(5);
            rec._preferredName = rs.getString(6);
            rec._longName = rs.getString(7);
            rec._definitionSource = rs.getString(8);
            rec._preferredDefinition = rs.getString(9);
            rec._origin = rs.getString(10);
            rec._workflow_status = rs.getString(11);
            list.add(rec);
        }
    } catch (SQLException ex) {
        // Bad...
        _errorCode = DBAlertUtil.getSQLErrorCode(ex);
        _errorMsg = _errorCode + ": " + select + "\n\n" + ex.toString();
        _logger.error(_errorMsg);
    } finally {
        closeCursors(stmt, rs);
    }
    return list;
}

From source file:gov.nih.nci.cadsr.sentinel.database.DBAlertOracle.java

/**
 * Delete the Alert Definitions specified by the caller. The values must be
 * existing al_idseq values within the Alert table.
 *
 * @param list_// w w  w.  ja v a  2 s . c o  m
 *        The al_idseq values which identify the definitions to delete.
 *        Other dependant tables in the database will automatically be
 *        cleaned up via cascades and triggers.
 * @return 0 if successful, otherwise the Oracle error code.
 */
public int deleteAlerts(String list_[]) {
    // Be sure we have something to do.
    if (list_ == null || list_.length == 0)
        return 0;

    // Build the delete SQL statement.
    String delete = "delete " + "from sbrext.sn_alert_view_ext " + "where al_idseq in (?";

    for (int ndx = 1; ndx < list_.length; ++ndx) {
        delete = delete + ",?";
    }
    delete = delete + ")";

    // Delete all the specified definitions. We rely on cascades or triggers
    // to clean up
    // all related tables.
    PreparedStatement pstmt = null;
    int rc = 0;
    try {
        // Set all the SQL arguments.
        pstmt = _conn.prepareStatement(delete);
        for (int ndx = 0; ndx < list_.length; ++ndx) {
            pstmt.setString(ndx + 1, list_[ndx]);
        }

        // Send it to the database. And remember to flag a commit for later.
        pstmt.executeUpdate();
        _needCommit = true;
    } catch (SQLException ex) {
        // It's bad...
        _errorCode = DBAlertUtil.getSQLErrorCode(ex);
        _errorMsg = _errorCode + ": " + delete + "\n\n" + ex.toString();
        _logger.error(_errorMsg);
        rc = _errorCode;
    } finally {
        closeCursors(pstmt, null);
    }
    return rc;
}

From source file:gov.nih.nci.cadsr.sentinel.database.DBAlertOracle.java

/**
 * Required upon a successful return from open. When all database access is
 * completed for this user request. To optimize the database access, all
 * methods which perform actions that require a commmit only set a flag. It
 * is in the close() method the flag is interrogated and the commit actually
 * occurs.//from   w  w w  . j a  v a2s .c o  m
 *
 * @return 0 if successful, otherwise the Oracle error code.
 * @see gov.nih.nci.cadsr.sentinel.database.DBAlert#open(HttpServletRequest, String, String) open() with HTTP request
 * @see gov.nih.nci.cadsr.sentinel.database.DBAlert#open(ServletContext, String, String) open() with Servlet Context
 * @see gov.nih.nci.cadsr.sentinel.database.DBAlert#open(String, String, String) open()
 */
public int close() {
    // We only need to do something if a connection is obtained.
    int rc = 0;
    if (_conn != null) {
        // Don't forget to commit if needed.
        try {
            if (_needCommit)
                _conn.commit();
        } catch (SQLException ex) {
            // There seems to be a problem.
            _errorCode = DBAlertUtil.getSQLErrorCode(ex);
            _errorMsg = _errorCode + ": " + ex.toString();
            _logger.error(_errorMsg);
        }

        // Close the connection and release all pointers.
        try {
            _conn.close();
        } catch (SQLException ex) {
            // There seems to be a problem.
            _errorCode = DBAlertUtil.getSQLErrorCode(ex);
            _errorMsg = _errorCode + ": " + ex.toString();
            _logger.error(_errorMsg);
            rc = _errorCode;
        } finally {
            _conn = null;
            _sc = null;
        }
    }
    return rc;
}

From source file:gov.nih.nci.cadsr.sentinel.database.DBAlertOracle.java

/**
 * Retrieves the abbreviated list of all alerts. The AlertRec objects
 * returned are not fully populated with all the details of each alert. Only
 * basic information such as the database id, name, creator and a few other
 * basic properties are guaranteed./*from   ww w .  ja va2s . c o  m*/
 *
 * @param user_
 *        The user id with which to qualify the results. This must be as it
 *        appears in the "created_by" column of the Alert tables. If null is
 *        used, a list of all Alerts is retrieved.
 * @return The array of Alerts retrieved.
 */
public AlertRec[] selectAlerts(String user_) {
    // Define the SQL Select
    String select = "select a.al_idseq, a.name, a.last_auto_run, a.auto_freq_unit, a.al_status, a.auto_freq_value, a.created_by, u.name "
            + "from sbrext.sn_alert_view_ext a, sbr.user_accounts_view u " + "where ";

    // If a user id was given, qualify the list with it.
    if (user_ != null)
        select = select + "a.created_by = ? and ";
    select = select + "u.ua_name = a.created_by";

    PreparedStatement pstmt = null;
    ResultSet rs = null;
    Vector<AlertRec> results = new Vector<AlertRec>();
    AlertRec[] database = null;

    try {
        // Prepare the statement.
        pstmt = _conn.prepareStatement(select);
        if (user_ != null)
            pstmt.setString(1, user_);

        // Get the list.
        rs = pstmt.executeQuery();
        while (rs.next()) {
            // For the list of alerts we only need basic information.
            AlertRec rec = new AlertRec();
            rec.setAlertRecNum(rs.getString(1));
            rec.setName(rs.getString(2));
            rec.setAdate(rs.getTimestamp(3));
            rec.setFreq(rs.getString(4));
            rec.setActive(rs.getString(5));
            rec.setDay(rs.getInt(6));
            rec.setCreator(rs.getString(7));
            rec.setCreatorName(rs.getString(8));

            // After much consideration, I thought it best to dynamically
            // generate the textual summary of the alert. This
            // could be done at the time the alert is saved and stored in
            // the database, however, the descriptions could become
            // very large and we have to worry about some things changing
            // and not being updated. For the first implementation
            // it seems best to generate it. In the future this may change.
            selectQuery(rec);
            select = rec.getSummary(false);
            rec.clearQuery();
            rec.setSummary(select);
            results.add(rec);
        }
    } catch (SQLException ex) {
        _errorCode = DBAlertUtil.getSQLErrorCode(ex);
        _errorMsg = _errorCode + ": " + select + "\n\n" + ex.toString();
        _logger.error(_errorMsg);
    } finally {
        closeCursors(pstmt, rs);
    }

    // Now that we have the full results we can create a single simple array
    // to contain
    // them. This greatly simplifies access throughout the code.
    int count = results.size();
    if (count > 0) {
        database = new AlertRec[count];
        for (int ndx = 0; ndx < count; ++ndx)
            database[ndx] = (AlertRec) results.get(ndx);
    }

    // Return the results.
    return database;
}

From source file:gov.nih.nci.cadsr.sentinel.database.DBAlertOracle.java

/**
 * Create a connection from the pool. This is not part of the constructor to
 * allow the method to have return codes that can be interrogated by the
 * caller. If Exception are desired, appropriate wrapper methods can be
 * created to provide both features and give the caller the flexibility to
 * use either without additional coding.
 * <p>//from www .  ja va 2s  . c  o  m
 * Be sure to call DBAlert.close() to complete the request before returning
 * to the client or loosing the object focus in the caller to "new
 * DBAlert()".
 *
 * @param sc_
 *        The servlet context which holds the data source pool reference
 *        created by the DBAlert.setupPool() method.
 * @param user_
 *        The database user logon id.
 * @param pswd_
 *        The password to match the user.
 * @return 0 if successful, otherwise the Oracle error code.
 * @see gov.nih.nci.cadsr.sentinel.database.DBAlert#close close()
 */
public int open(ServletContext sc_, String user_, String pswd_) {
    // If we already have a connection, don't bother.
    if (_conn != null)
        return 0;

    try {
        // Get a connection from the pool, if anything unexpected happens
        // the catch is
        // run.
        _sc = sc_;
        _user = user_;
        AlertPlugIn var = (AlertPlugIn) _sc.getAttribute(DBAlert._DATASOURCE);
        if (var == null) {
            OracleConnectionPoolDataSource ocpds = (OracleConnectionPoolDataSource) _sc.getAttribute(_DBPOOL);
            _conn = ocpds.getConnection(user_, pswd_);
            if (_poolWarning) {
                _poolWarning = false;
                _logger.warn("============ Could not find JBoss datasource using internal connection pool.");
            }
        } else if (pswd_ == null)
            _conn = var.getDataSource().getConnection();
        else
            _conn = var.getAuthenticate().getConnection(user_, pswd_);

        // We handle the commit once in the close.
        _conn.setAutoCommit(false);
        _needCommit = false;

        return 0;
    } catch (SQLException ex) {
        // There seems to be a problem.
        _errorCode = DBAlertUtil.getSQLErrorCode(ex);
        _errorMsg = _errorCode + ": " + ex.toString();
        _logger.error(_errorMsg);
        _sc = null;
        if (_conn != null) {
            try {
                _conn.close();
            } catch (Exception ex2) {
            }
        }
        _conn = null;
        return _errorCode;
    }
}

From source file:gov.nih.nci.cadsr.sentinel.database.DBAlertOracle.java

/**
 * Return the recipients names in ascending order by first name as a single
 * string. If the recipient is a broadcast context group the group is expanded.
 * Those who have elected not to receive broadcasts from a context group are
 * not included. All freeform email addresses are listed after the names
 * retrieved from the account table.// w  w w  .j  a va2s  .  c o m
 *
 * @param recipients_ The Alert recipient list.
 * @return A single comma separate list of names and email addresses with
 *      the broadcast context groups expanded.
 */
public String selectRecipientNames(String recipients_[]) {
    // Check input.
    if (recipients_ == null || recipients_.length == 0)
        return "(none)";

    // Break the recipients list apart.
    String contexts = "";
    String users = "";
    String emails = "";
    for (int ndx = 0; ndx < recipients_.length; ++ndx) {
        if (recipients_[ndx].charAt(0) == '/')
            contexts = contexts + ", '" + recipients_[ndx].substring(1) + "'";
        else if (recipients_[ndx].indexOf('@') < 0)
            users = users + ", '" + recipients_[ndx] + "'";
        else
            emails = emails + ", " + recipients_[ndx];
    }

    // Build the select for user names
    String select = "";
    if (users.length() > 0)
        select += "select ua.name as lname from sbr.user_accounts_view ua where ua.ua_name in ("
                + users.substring(2) + ") and ua.electronic_mail_address is not null ";

    // Build the select for a Context group
    if (contexts.length() > 0) {
        if (select.length() > 0)
            select += "union ";

        select += "select ua.name as lname from sbr.user_accounts_view ua, sbrext.user_contexts_view uc, sbr.contexts_view c where c.conte_idseq in ("
                + contexts.substring(2)
                + ") and uc.name = c.name and uc.privilege = 'W' and ua.ua_name = uc.ua_name and ua.alert_ind = 'Yes' and ua.electronic_mail_address is not null ";
    }

    String names = "";
    if (select.length() > 0) {
        // Sort the results.
        select = "select lname from (" + select + ") order by upper(lname) asc";

        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            // Retrieve the user names from the database.
            pstmt = _conn.prepareStatement(select);
            rs = pstmt.executeQuery();

            // Make this a comma separated list.
            while (rs.next()) {
                names += ", " + rs.getString(1);
            }
        } catch (SQLException ex) {
            _errorCode = DBAlertUtil.getSQLErrorCode(ex);
            _errorMsg = _errorCode + ": " + select + "\n\n" + ex.toString();
            _logger.error(_errorMsg);
            names = "";
        } finally {
            closeCursors(pstmt, rs);
        }
    }

    // Append the freeform email addresses.
    if (emails.length() > 0)
        names += emails;
    return (names.length() > 0) ? names.substring(2) : "(none)";
}

From source file:gov.nih.nci.cadsr.sentinel.database.DBAlertOracle.java

/**
 * Perform the database access for a simple query which results in a 3
 * column value per returned row./*from  w  ww  .  j av a2s .  c  om*/
 *
 * @param select_
 *        The SQL select to run.
 * @return 0 if successful, otherwise the database error code.
 */
private Results2 getBasicData2(String select_) {
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    Vector<ResultsData2> results = new Vector<ResultsData2>();
    Results2 data = new Results2();

    try {
        // Prepare the statement.
        pstmt = _conn.prepareStatement(select_);

        // Get the list.
        rs = pstmt.executeQuery();
        ResultsData2 rec;
        while (rs.next()) {
            // Remember about the 1 (one) based indexing.
            rec = new ResultsData2();
            rec._id1 = rs.getString(1);
            rec._id2 = rs.getString(2);
            rec._label = rs.getString(3);
            results.add(rec);
        }

        // Move the list from a Vector to an array and add "(All)" to
        // the beginning.
        int count = results.size() + 1;
        data._data = new ResultsData2[count];
        data._data[0] = new ResultsData2();
        data._data[0]._label = Constants._STRALL;
        data._data[0]._id1 = Constants._STRALL;
        data._data[0]._id2 = Constants._STRALL;
        int cnt = 0;
        for (int ndx = 1; ndx < count; ++ndx) {
            rec = (ResultsData2) results.get(cnt++);
            data._data[ndx] = new ResultsData2();
            data._data[ndx]._label = rec._label.replaceAll("[\\s]", " ");
            data._data[ndx]._id1 = rec._id1;
            data._data[ndx]._id2 = rec._id2;
        }

        data._rc = 0;
    } catch (SQLException ex) {
        // Bad...
        _errorCode = DBAlertUtil.getSQLErrorCode(ex);
        _errorMsg = _errorCode + ": " + select_ + "\n\n" + ex.toString();
        _logger.error(_errorMsg);
        data._rc = _errorCode;
    } finally {
        closeCursors(pstmt, rs);
    }
    return data;
}

From source file:gov.nih.nci.cadsr.sentinel.database.DBAlertOracle.java

/**
 * Pull the report information for a specific alert definition.
 *
 * @param rec_// ww  w. j  a va  2 s .c  om
 *        The alert for the desired report. The alertRecNum must be set
 *        prior to this method.
 * @return 0 if successful, otherwise the database error code.
 */
private int selectReport(AlertRec rec_) {
    // Each Alert has one Report definition.
    String select = "select rep_idseq, include_property_ind, style, send, acknowledge_ind, comments, assoc_lvl_num "
            + "from sbrext.sn_report_view_ext " + "where al_idseq = ?";
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    int rc = 0;
    try {
        // Get ready...
        pstmt = _conn.prepareStatement(select);
        pstmt.setString(1, rec_.getAlertRecNum());

        // Strictly speaking if a record is not found it is a violation of a
        // business rule, however,
        // the code is written to default all values to avoid these types of
        // quirks.
        rs = pstmt.executeQuery();
        if (rs.next()) {
            rec_.setReportRecNum(rs.getString(1));
            rec_.setIncPropSect(rs.getString(2));
            rec_.setReportAck(rs.getString(3));
            rec_.setReportEmpty(rs.getString(4));
            rec_.setReportAck(rs.getString(5));
            rec_.setIntro(rs.getString(6), true);
            rec_.setIAssocLvl(rs.getInt(7));
        }
        rs.close();
        rs = null;
        pstmt.close();
        pstmt = null;

        rc = selectRecipients(rec_);
    } catch (SQLException ex) {
        // We've got trouble.
        _errorCode = DBAlertUtil.getSQLErrorCode(ex);
        _errorMsg = _errorCode + ": " + select + "\n\n" + ex.toString();
        _logger.error(_errorMsg);
        rc = _errorCode;
    } finally {
        closeCursors(pstmt, rs);
    }
    return rc;
}