Example usage for java.sql SQLException SQLException

List of usage examples for java.sql SQLException SQLException

Introduction

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

Prototype

public SQLException(String reason, Throwable cause) 

Source Link

Document

Constructs a SQLException object with a given reason and cause.

Usage

From source file:com.googlecode.fascinator.sequences.SequenceService.java

/**
 * Shuts down the plugin//from   ww  w  .  ja v a 2s  .  c om
 * 
 * @throws AccessControlException
 *             if there was an error during shutdown
 */
public void shutdown() throws SQLException {
    // Derby can only be shutdown from one thread,
    // we'll catch errors from the rest.
    String threadedShutdownMessage = DERBY_DRIVER + " is not registered with the JDBC driver manager";
    try {
        // Tell the database to close
        DriverManager.getConnection(DERBY_PROTOCOL + ";shutdown=true");
        // Shutdown just this database (but not the engine)
        // DriverManager.getConnection(DERBY_PROTOCOL + SECURITY_DATABASE +
        // ";shutdown=true");
    } catch (SQLException ex) {
        // These test values are used if the engine is NOT shutdown
        // if (ex.getErrorCode() == 45000 &&
        // ex.getSQLState().equals("08006")) {

        // Valid response
        if (ex.getErrorCode() == 50000 && ex.getSQLState().equals("XJ015")) {
            // Error response
        } else {
            // Make sure we ignore simple thread issues
            if (!ex.getMessage().equals(threadedShutdownMessage)) {
                log.error("Error during database shutdown:", ex);
                throw new SQLException("Error during database shutdown:", ex);
            }
        }
    } finally {
        try {
            // Close our connection
            if (connection != null) {
                connection.close();
                connection = null;
            }
        } catch (SQLException ex) {
            log.error("Error closing connection:", ex);
        }
    }
}

From source file:com.chiralbehaviors.CoRE.kernel.Bootstrap.java

public void insert(WellKnownLocation wkl) throws SQLException {
    PreparedStatement s = connection.prepareStatement(String
            .format("INSERT into %s (id, name, description, updated_by) VALUES (?, ?, ?, ?)", wkl.tableName()));
    try {//from  ww w .j a v  a2s . co m
        s.setString(1, wkl.id());
        s.setString(2, wkl.wkoName());
        s.setString(3, wkl.description());
        s.setString(4, WellKnownAgency.CORE.id());
        s.execute();
    } catch (SQLException e) {
        throw new SQLException(String.format("Unable to insert  %s", wkl), e);
    }
}

From source file:au.edu.jcu.fascinator.plugin.harvester.directory.DerbyCache.java

private Connection connection() throws SQLException {
    if (connection == null || !connection.isValid(1)) {
        // At least try to close if not null... even though its not valid
        if (connection != null) {
            log.error("!!! Database connection has failed, recreating.");
            try {
                connection.close();/*from  w  ww  . ja va2s.  c o m*/
            } catch (SQLException ex) {
                log.error("Error closing invalid connection, ignoring: {}", ex.getMessage());
            }
        }

        // Open a new connection
        Properties props = new Properties();
        // Load the JDBC driver
        try {
            Class.forName(DERBY_DRIVER).newInstance();
        } catch (Exception ex) {
            log.error("Driver load failed: ", ex);
            throw new SQLException("Driver load failed: ", ex);
        }

        // Establish a database connection
        connection = DriverManager.getConnection(DERBY_PROTOCOL + DATABASE_NAME + ";create=true", props);
    }
    return connection;
}

From source file:org.ohmage.query.impl.AnnotationQueries.java

@Override
public List<Annotation> readSurveyResponseAnnotations(final UUID surveyId) throws DataAccessException {
    try {//from  ww w.j a v  a2 s  . c om
        return getJdbcTemplate().query(SQL_READ_SURVEY_RESPONSE_ANNOTATION,
                new Object[] { surveyId.toString() }, new RowMapper<Annotation>() {
                    public Annotation mapRow(ResultSet rs, int rowNum) throws SQLException {
                        try {
                            return new Annotation(rs.getString(1), rs.getString(2), rs.getLong(3),
                                    rs.getString(4));
                        } catch (DomainException e) {
                            throw new SQLException("Error creating an annotation object.", e);
                        }
                    }
                });
    } catch (org.springframework.dao.DataAccessException e) {
        throw new DataAccessException("An error occurred when running the following SQL: '"
                + SQL_READ_SURVEY_RESPONSE_ANNOTATION + " with the parameter " + surveyId, e);
    }
}

From source file:com.wso2telco.proxy.util.DBUtils.java

/**
 * Get operators' MSISDN header properties.
 *
 * @return operators' MSISDN header properties map.
 * @throws SQLException    on errors//from  ww  w.ja  va2  s .  c om
 * @throws NamingException on errors
 */
public static Map<String, List<MSISDNHeader>> getOperatorsMSISDNHeaderProperties()
        throws SQLException, NamingException {
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    Map<String, List<MSISDNHeader>> operatorsMSISDNHeadersList = new HashMap<String, List<MSISDNHeader>>();
    String queryToGetOperatorProperty = "SELECT DISTINCT operatorId, LOWER(operatorName) AS operatorName FROM "
            + "operators_msisdn_headers_properties prop LEFT JOIN operators op ON op.ID=prop.operatorId";
    try {
        connection = getConnectDBConnection();
        preparedStatement = connection.prepareStatement(queryToGetOperatorProperty);
        resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            int operatorId = resultSet.getInt(AuthProxyConstants.OPERATOR_ID);
            String operatorName = resultSet.getString(AuthProxyConstants.OPERATOR_NAME);
            //Get msisdn properties of the operator.
            List<MSISDNHeader> msisdnHeaderList = getMSISDNPropertiesByOperatorId(operatorId, operatorName);
            operatorsMSISDNHeadersList.put(operatorName, msisdnHeaderList);
        }
    } catch (SQLException e) {
        throw new SQLException("Error occurred while retrieving operator MSISDN properties of operators : ", e);
    } catch (NamingException e) {
        throw new ConfigurationException("DataSource could not be found in mobile-connect.xml");
    } finally {
        closeAllConnections(preparedStatement, connection, resultSet);
    }
    return operatorsMSISDNHeadersList;
}

From source file:com.oltpbenchmark.benchmarks.rest.procedures.RESTStockLevel.java

private void stockLevelTransaction(int w_id, int d_id, int threshold, RESTWorker w) throws SQLException {
    int o_id = 0;
    // XXX int i_id = 0;
    int stock_count = 0;

    // XXX District dist = new District();
    // XXX OrderLine orln = new OrderLine();
    // XXX Stock stck = new Stock();

    JSONArray jsonArray = RESTUtil.executeSelectQuery(builder, stockGetDistOrderIdSQLWithVariables,
            Integer.toString(w_id), Integer.toString(d_id));

    if (jsonArray.length() == 0)
        throw new RuntimeException("D_W_ID=" + w_id + " D_ID=" + d_id + " not found!");

    try {/* w  w w .  j a  v a 2  s  .c om*/
        o_id = jsonArray.getJSONObject(0).getInt("D_NEXT_O_ID");
        threshold = jsonArray.getJSONObject(0).getInt("THRESHOLD");
    } catch (JSONException e) {
        throw new SQLException("Error in parsing the results", e);
    }

    RESTUtil.waitMilliSeconds(w.getWorkloadConfiguration().getThinktime());

    jsonArray = RESTUtil.executeSelectQuery(builder, stockGetCountStockSQLWithVariables, Integer.toString(w_id),
            Integer.toString(d_id), Integer.toString(o_id), Integer.toString(o_id - 20), Integer.toString(w_id),
            Integer.toString(threshold));

    if (jsonArray.length() == 0)
        throw new RuntimeException("OL_W_ID=" + w_id + " OL_D_ID=" + d_id + " OL_O_ID=" + o_id + " not found!");

    try {
        stock_count = jsonArray.getJSONObject(0).getInt("STOCK_COUNT");
    } catch (JSONException e) {
        throw new SQLException("Error in parsing the results", e);
    }

    // calls same query 8 time
    // start = System.nanoTime();
    // while (start + 500000 > System.nanoTime()) {
    //
    // }
    // jsonArray = queryRESTEndpoint(stockGetCountStockSQLWithVariables2,
    // Integer.toString(w_id), Integer.toString(d_id),
    // Integer.toString(o_id), Integer.toString(o_id - 20),
    // Integer.toString(w_id),
    // Integer.toString(threshold));
    // start = System.nanoTime();
    // while (start + 500000 > System.nanoTime()) {
    //
    // }
    // jsonArray = queryRESTEndpoint(stockGetCountStockSQLWithVariables3,
    // Integer.toString(w_id), Integer.toString(d_id),
    // Integer.toString(o_id), Integer.toString(o_id - 20),
    // Integer.toString(w_id),
    // Integer.toString(threshold));
    // start = System.nanoTime();
    // while (start + 500000 > System.nanoTime()) {
    //
    // }
    // jsonArray = queryRESTEndpoint(stockGetCountStockSQLWithVariables4,
    // Integer.toString(w_id), Integer.toString(d_id),
    // Integer.toString(o_id), Integer.toString(o_id - 20),
    // Integer.toString(w_id),
    // Integer.toString(threshold));
    // start = System.nanoTime();
    // while (start + 500000 > System.nanoTime()) {
    //
    // }
    // jsonArray = queryRESTEndpoint(stockGetCountStockSQLWithVariables5,
    // Integer.toString(w_id), Integer.toString(d_id),
    // Integer.toString(o_id), Integer.toString(o_id - 20),
    // Integer.toString(w_id),
    // Integer.toString(threshold));
    // start = System.nanoTime();
    // while (start + 500000 > System.nanoTime()) {
    //
    // }
    // jsonArray = queryRESTEndpoint(stockGetCountStockSQLWithVariables6,
    // Integer.toString(w_id), Integer.toString(d_id),
    // Integer.toString(o_id), Integer.toString(o_id - 20),
    // Integer.toString(w_id),
    // Integer.toString(threshold));
    // start = System.nanoTime();
    // while (start + 500000 > System.nanoTime()) {
    //
    // }
    // jsonArray = queryRESTEndpoint(stockGetCountStockSQLWithVariables7,
    // Integer.toString(w_id), Integer.toString(d_id),
    // Integer.toString(o_id), Integer.toString(o_id - 20),
    // Integer.toString(w_id),
    // Integer.toString(threshold));
    // start = System.nanoTime();
    // while (start + 500000 > System.nanoTime()) {
    //
    // }
    // jsonArray = queryRESTEndpoint(stockGetCountStockSQLWithVariables8,
    // Integer.toString(w_id), Integer.toString(d_id),
    // Integer.toString(o_id), Integer.toString(o_id - 20),
    // Integer.toString(w_id),
    // Integer.toString(threshold));

    StringBuilder terminalMessage = new StringBuilder();
    terminalMessage.append("\n+-------------------------- STOCK-LEVEL --------------------------+");
    terminalMessage.append("\n Warehouse: ");
    terminalMessage.append(w_id);
    terminalMessage.append("\n District:  ");
    terminalMessage.append(d_id);
    terminalMessage.append("\n\n Stock Level Threshold: ");
    terminalMessage.append(threshold);
    terminalMessage.append("\n Low Stock Count:       ");
    terminalMessage.append(stock_count);
    terminalMessage.append("\n+-----------------------------------------------------------------+\n\n");
    if (LOG.isTraceEnabled())
        LOG.trace(terminalMessage.toString());
}

From source file:com.chiralbehaviors.CoRE.kernel.Bootstrap.java

public void insert(WellKnownObject wko) throws SQLException {
    PreparedStatement s = connection.prepareStatement(String
            .format("INSERT into %s (id, name, description, updated_by) VALUES (?, ?, ?, ?)", wko.tableName()));
    try {/* w w  w .  jav a 2s  .c o  m*/
        s.setString(1, wko.id());
        s.setString(2, wko.wkoName());
        s.setString(3, wko.description());
        s.setString(4, WellKnownAgency.CORE.id());
        s.execute();
    } catch (SQLException e) {
        throw new SQLException(String.format("Unable to insert %s", wko), e);
    }
}