Example usage for java.sql SQLException getCause

List of usage examples for java.sql SQLException getCause

Introduction

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

Prototype

public synchronized Throwable getCause() 

Source Link

Document

Returns the cause of this throwable or null if the cause is nonexistent or unknown.

Usage

From source file:com.sql.Audit.java

/**
 * Adds an entry to the audit table//ww w  .  ja  v  a  2s  . c om
 * @param action performed action to be stored
 */
public static void addAuditEntry(String action) {
    Connection conn = null;
    PreparedStatement ps = null;
    try {

        conn = DBConnection.connectToDB();

        String sql = "INSERT INTO Audit VALUES" + "(?,?,?)";

        ps = conn.prepareStatement(sql);
        ps.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
        ps.setInt(2, 0);
        ps.setString(3, action == null ? "MISSING ACTION" : StringUtils.left(action, 255));

        ps.executeUpdate();
    } catch (SQLException ex) {
        if (ex.getCause() instanceof SQLServerException) {
            addAuditEntry(action);
        }
    } finally {
        DbUtils.closeQuietly(conn);
        DbUtils.closeQuietly(ps);
    }
}

From source file:com.sql.CaseParty.java

public static List<CasePartyModel> getCasePartyList(String caseYear, String caseType, String caseMonth,
        String caseNumber) {//from  w ww  . ja va 2  s.c om
    List<CasePartyModel> list = new ArrayList();
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        conn = DBConnection.connectToDB();

        String sql = "SELECT" + " caseRelation," + " LastName," + " FirstName," + " MiddleInitial,"
                + " jobTitle," + " companyName," + " Address1," + " Address2," + " City," + " stateCode,"
                + " zipCode," + " phone1 FROM " + " caseParty" + " WHERE" + " caseyear = ?"
                + " AND casetype = ?" + " AND casemonth = ?" + " AND caseNumber = ?";
        ps = conn.prepareStatement(sql);
        ps.setString(1, caseYear);
        ps.setString(2, caseType);
        ps.setString(3, caseMonth);
        ps.setString(4, caseNumber);
        rs = ps.executeQuery();

        while (rs.next()) {
            CasePartyModel item = new CasePartyModel();
            item.setCaseRelation(rs.getString("caseRelation") == null ? "" : rs.getString("caseRelation"));
            item.setLastName(rs.getString("LastName") == null ? "" : rs.getString("LastName"));
            item.setFirstName(rs.getString("FirstName") == null ? "" : rs.getString("FirstName"));
            item.setMiddleInitial(rs.getString("MiddleInitial") == null ? "" : rs.getString("MiddleInitial"));
            item.setJobTitle(rs.getString("jobTitle") == null ? "" : rs.getString("jobTitle"));
            item.setCompanyName(rs.getString("companyName") == null ? "" : rs.getString("companyName"));
            item.setAddress1(rs.getString("Address1") == null ? "" : rs.getString("Address1"));
            item.setAddress2(rs.getString("Address2") == null ? "" : rs.getString("Address2"));
            item.setCity(rs.getString("City") == null ? "" : rs.getString("City"));
            item.setStateCode(rs.getString("stateCode") == null ? "" : rs.getString("stateCode"));
            item.setZipcode(rs.getString("zipCode") == null ? "" : rs.getString("zipCode"));
            item.setPhone1(rs.getString("phone1") == null ? "" : rs.getString("phone1"));
            list.add(item);
        }
    } catch (SQLException ex) {
        if (ex.getCause() instanceof SQLServerException) {
            getCasePartyList(caseYear, caseType, caseMonth, caseNumber);
        }
    } finally {
        DbUtils.closeQuietly(conn);
        DbUtils.closeQuietly(ps);
        DbUtils.closeQuietly(rs);
    }
    return list;
}

From source file:com.kapti.data.persistence.oracle.OracleConnection.java

public static Connection getConnection() throws StockPlayException {
    try {//from   www.  j  a  v a2s . c o  m
        if (ds == null) {

            ds = new BasicDataSource();
            ds.setDriverClassName("oracle.jdbc.driver.OracleDriver");

            ds.setUrl("jdbc:oracle:thin:@//oersted.iii.hogent.be:1521/xe");

            ds.setUsername("stockplay");
            ds.setPassword("chocolademousse");
            ds.setTestOnBorrow(true);
            ds.setTestOnReturn(true);
            ds.setTestWhileIdle(true);
            ds.setRemoveAbandoned(true);
            ds.setMaxWait(15 * 1000); // 15 seconden timeout
            ds.setValidationQuery("select 1 from dual");
        }

        return ds.getConnection();
    } catch (SQLException ex) {
        throw new SubsystemException(SubsystemException.Type.DATABASE_FAILURE,
                "Error while creating connection-object", ex.getCause());
    }
}

From source file:com.taobao.datax.plugins.common.DBSource.java

/**
 * A synchronized static method which gets database connection by the
 * identification.//  w ww.  j a  va 2s. c om
 * 
 * @param id
 *            unique identification binding with DataSource.
 * 
 * @return a database connection.
 * 
 * @throws {@link IllegalStateException} Cannot connect to DataBase
 * 
 *               {@link IllegalArgumentException} Connect id is not registered
 * 
 */
public static synchronized Connection getConnection(String id) {
    Connection c = null;
    BasicDataSource dataSource = (BasicDataSource) sourceInfoMap.get(id);
    try {
        c = dataSource.getConnection();
    } catch (SQLException e) {
        logger.error(ExceptionTracker.trace(e));
        throw new IllegalArgumentException(e.getCause());
    }
    if (null != c) {
        logger.info(String.format("Key [%s] connect to database pool successfully .", id));
    } else {
        logger.error(String.format("Key [%s]  connect to database pool failed .", id));
        throw new IllegalArgumentException(String.format("Connection key [%s] error .", id));
    }
    return c;
}

From source file:sos.util.SOSExceptionMessage.java

/**
 * /* w  ww  . ja  v a2s. c o m*/
 * 
 * @param exception
 * @return
 */
public static String getExceptionMessage(Exception exception) {
    String msg = "";

    try {

        if (exception instanceof SQLException) {
            //|| exception instanceof javax.mail.MessagingException) {

            SQLException sqlExcep = (SQLException) exception;
            while (sqlExcep != null) {

                if (sqlExcep.equals(sqlExcep.getNextException())) {
                    break;
                }

                msg = sqlExcep.toString();
                if (sqlExcep.getCause() != null) {
                    msg = msg + "\n" + sqlExcep.getCause();
                }
                sqlExcep = sqlExcep.getNextException();

            }

        } else {
            msg = exception.toString();
            if (exception.getCause() != null) {
                msg = exception.toString() + " " + exception.getCause();
            }

        }
    } catch (Exception e) {
        System.out.print(e);
    }
    return msg;
}

From source file:org.wsm.database.tools.util.ConnectionManager.java

public static boolean isConnectionPropertiesValid(DBConnectionProperties properties)
        throws DBConnectionFailedException {
    if (properties == null) {
        return false;
    }//from w  w w .j a v  a 2 s. co m
    Driver driver = loadDriver(properties.getDatabaseType());
    String connectionURL = getConnectionURL(properties);
    Connection con = null;
    try {
        con = DriverManager.getConnection(connectionURL, properties.getUserName(), properties.getPassword());
        return con != null;
    } catch (SQLException e) {
        throw new DBConnectionFailedException("Exception occured while getting connection " + e.getCause());
    } finally {
        try {
            if (con != null) {
                con.close();
            }
            DriverManager.deregisterDriver(driver);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.google.gerrit.server.schema.H2AccountPatchReviewStore.java

public static OrmException convertError(String op, SQLException err) {
    switch (getSQLStateInt(err)) {
    case 23001: // UNIQUE CONSTRAINT VIOLATION
    case 23505: // DUPLICATE_KEY_1
        return new OrmDuplicateKeyException("ACCOUNT_PATCH_REVIEWS", err);

    default:/*  w  ww .  ja v a 2s .c  om*/
        if (err.getCause() == null && err.getNextException() != null) {
            err.initCause(err.getNextException());
        }
        return new OrmException(op + " failure on ACCOUNT_PATCH_REVIEWS", err);
    }
}

From source file:com.oracle.tutorial.jdbc.JDBCTutorialUtilities.java

public static void alternatePrintSQLException(SQLException ex) {
    while (ex != null) {
        System.err.println("SQLState: " + ex.getSQLState());
        System.err.println("Error Code: " + ex.getErrorCode());
        System.err.println("Message: " + ex.getMessage());
        Throwable t = ex.getCause();
        while (t != null) {
            System.out.println("Cause: " + t);
            t = t.getCause();/*  www .  ja v a  2s .  c  o  m*/
        }
        ex = ex.getNextException();
    }
}

From source file:com.oracle.tutorial.jdbc.JDBCTutorialUtilities.java

public static void printSQLException(SQLException ex) {
    for (Throwable e : ex) {
        if (e instanceof SQLException) {
            if (ignoreSQLException(((SQLException) e).getSQLState()) == false) {
                e.printStackTrace(System.err);
                System.err.println("SQLState: " + ((SQLException) e).getSQLState());
                System.err.println("Error Code: " + ((SQLException) e).getErrorCode());
                System.err.println("Message: " + e.getMessage());
                Throwable t = ex.getCause();
                while (t != null) {
                    System.out.println("Cause: " + t);
                    t = t.getCause();/*w  ww  .j  a v  a 2  s. co m*/
                }
            }
        }
    }
}

From source file:org.apereo.portal.portlet.dao.jpa.SQLNextExceptionLoggerAspect.java

public void logBatchUpdateExceptions(Throwable t) {
    while (!(t instanceof SQLException)) {
        t = t.getCause();/*from  www .j  a  v a  2  s  .c o m*/
        if (t == null) {
            return;
        }
    }

    SQLException sqle = (SQLException) t;

    //If the SQLException is the root chain the results of getNextException as initCauses
    if (sqle.getCause() == null) {
        SQLException nextException;
        while ((nextException = sqle.getNextException()) != null) {
            sqle.initCause(nextException);
            sqle = nextException;
        }
    }
    //The SQLException already has a cause so log the results of all getNextException calls
    else {
        while ((sqle = sqle.getNextException()) != null) {
            this.logger.error("Logging getNextException for root SQLException: " + t, sqle);
        }
    }
}