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(Throwable cause) 

Source Link

Document

Constructs a SQLException object with a given cause.

Usage

From source file:com.splout.db.common.SQLite4JavaManager.java

@Override
public String query(String query, int maxResults) throws SQLException, JSONSerDeException {
    SQLiteStatement st = null;/*from   w  w  w .  ja v a  2 s.c  om*/
    try {
        SQLiteConnection conn = db.get();
        if (timeoutThread != null) {
            timeoutThread.startQuery(conn, query);
        }
        st = conn.prepare(query);
        if (timeoutThread != null) {
            timeoutThread.endQuery(conn);
        }
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        if (st.step()) {
            do {
                // true if there is data (SQLITE_ROW) was returned, false if statement has been completed (SQLITE_DONE)
                Map<String, Object> objectToRead = new HashMap<String, Object>();
                for (int i = 0; i < st.columnCount(); i++) {
                    objectToRead.put(st.getColumnName(i), st.columnValue(i));
                }
                list.add(objectToRead);
            } while (st.step() && list.size() < maxResults);
        }
        if (list.size() == maxResults) {
            throw new SQLException("Hard limit on number of results reached (" + maxResults
                    + "), please use a LIMIT for this query.");
        }
        return JSONSerDe.ser(list);
    } catch (SQLiteException e) {
        throw new SQLException(e);
    } finally {
        if (st != null) {
            st.dispose();
        }
    }
}

From source file:com.wso2telco.dep.operatorservice.dao.WorkflowDAO.java

public void updateWorkflowRef(String apiName, String apiVersion, int appId) throws SQLException, Exception {

    Connection con = null;/* w  w w.ja va2  s.  c o m*/
    PreparedStatement ps = null;
    try {
        con = DbUtils.getDbConnection(DataSourceNames.WSO2TELCO_DEP_DB);
        if (con == null) {
            throw new SQLException("Connection not found");
        }
        StringBuilder queryString = new StringBuilder("UPDATE ");
        queryString.append("workflow_reference");
        queryString.append(" SET status = ?");
        queryString.append(" WHERE api_version = ?");
        queryString.append(" and application_id = ?");
        queryString.append(" and api_name = ?");

        ps = con.prepareStatement(queryString.toString());

        ps.setInt(1, 0);
        ps.setString(2, apiVersion);
        ps.setInt(3, appId);
        ps.setString(4, apiName);
        log.debug("sql query in updateOperatorToken : " + ps);
        ps.executeUpdate();
    } catch (SQLException e) {
        log.error("database operation error in updateWorkflowRef : ", e);
        throw e;
    } catch (Exception e) {
        log.error("error in updateWorkflowRef : ", e);
        throw e;
    } finally {
        DbUtils.closeAllConnections(ps, con, null);
    }
}

From source file:de.innovationgate.webgate.api.jdbc.pool.DBCPReplicationConnection.java

public void commit() throws SQLException {
    if (_readonly) {
        throw new SQLException("Datamodification is not allowed on a readonly connection.");
    } else {/* w w w  .j  av a  2s  . co  m*/
        _currentConnection.commit();
    }
}

From source file:jp.co.acroquest.endosnipe.data.db.PostgresDataSourceCreator.java

/**
 * {@inheritDoc}//w w w  .ja  va2  s  . c o  m
 */
public DataSource createPoolingDataSource(String dbname, boolean connectOnlyExists) throws SQLException {
    try {
        String driverClass = DBManager.getDriverClass();
        Class.forName(driverClass);
        String uri = createDatabaseURI(DBManager.getHostName(), DBManager.getPort(), dbname);
        String userName = DBManager.getUserName();
        String password = DBManager.getPassword();
        ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(uri, userName, password);

        // ????????connectOnlyExists?false???
        // ????????
        if (!connectOnlyExists) {
            if (!(this.existsDatabase(dbname))) {
                // ???????
                DBInitializer.createDatabase(dbname);
            }
        }

        // ????? StackObjectPool ??
        // ????????
        ConnectionManager manager = ConnectionManager.getInstance();
        ObjectPool connectionPool = manager.getConnectionPool(uri);
        if (connectionPool == null) {
            connectionPool = manager.createNewConnectionPool(uri);
        }

        new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true);
        return new PoolingDataSource(connectionPool);
    } catch (Throwable ex) {
        LOGGER.log(EXCEPTION_OCCURED, ex, ex.getMessage());
        throw new SQLException(ex.getMessage());
    }
}

From source file:oobbit.orm.Users.java

/**
 * Creates a user and returns its id./*from   w  ww  . j a  va 2s.  c  o  m*/
 *
 * @param username Username for the user
 * @param email    Email for the user
 * @param password Hashed password
 *
 * @return ID of the registered user
 *
 * @throws SQLException
 */
public int registerNewUser(String username, String email, String password) throws SQLException {
    PreparedStatement statement = getConnection().prepareStatement(
            "INSERT INTO `oobbit`.`users` (`user_id`, `username`, `email`, `password`, `access_level`, `create_time`) VALUES (NULL, ?, ?, ?, '1', CURRENT_TIMESTAMP);",
            Statement.RETURN_GENERATED_KEYS);
    statement.setString(1, username);
    statement.setString(2, email);
    statement.setString(3, hash(password));

    statement.executeUpdate();
    ResultSet rs = statement.getGeneratedKeys();
    if (rs.next()) {
        return rs.getInt(1);
    }

    throw new SQLException("Could not add user!");
}

From source file:com.taobao.adfs.database.tdhsocket.client.util.ConvertUtil.java

public static BigDecimal getBigDecimalFromString(String stringVal, int scale) throws SQLException {
    BigDecimal bdVal;//  ww w .jav  a2s.c o  m

    if (stringVal != null) {
        if (stringVal.length() == 0) {
            bdVal = new BigDecimal("0");
            try {
                return bdVal.setScale(scale);
            } catch (ArithmeticException ex) {
                try {
                    return bdVal.setScale(scale, BigDecimal.ROUND_HALF_UP);
                } catch (ArithmeticException arEx) {
                    throw new SQLException(
                            "ResultSet.Bad_format_for_BigDecimal: value=" + stringVal + ",scale=" + scale);
                }
            }
        }
        try {
            try {
                return new BigDecimal(stringVal).setScale(scale);
            } catch (ArithmeticException ex) {
                try {
                    return new BigDecimal(stringVal).setScale(scale, BigDecimal.ROUND_HALF_UP);
                } catch (ArithmeticException arEx) {
                    throw new SQLException(
                            "ResultSet.Bad_format_for_BigDecimal: value=" + stringVal + ",scale=" + scale);
                }
            }
        } catch (NumberFormatException ex) {
            throw new SQLException(
                    "ResultSet.Bad_format_for_BigDecimal: value=" + stringVal + ",scale=" + scale);
        }
    }

    return null;
}

From source file:de.iew.stagediver.fx.database.hibernate.HBConnectionProvider.java

@Override
public void closeConnection(Connection conn) throws SQLException {
    log.debug("HBConnectionProvider.closeConnection({})", conn);
    final ConnectionService connectionService = LOOKUP.lookup(ConnectionService.class);
    try {//w w  w  .ja va2s .  com
        connectionService.releaseConnection(conn);
    } catch (ConnectionReleaseException e) {
        throw new SQLException(e);
    }
}

From source file:com.treasuredata.jdbc.TDDatabaseMetaData.java

public ResultSet getAttributes(String catalog, String schemaPattern, String typeNamePattern,
        String attributeNamePattern) throws SQLException {
    throw new SQLException("Unsupported TDDatabaseMetaData#getAttributes(String, String, String, String)");
}

From source file:es.tid.fiware.rss.exceptionhandles.FactoryResponseTest.java

/**
 * // w  w w. j  a  v  a 2  s.com
 */
@Test
public void catchConnectionJDBCJson() throws Exception {
    UriInfo mockUriInfo = Mockito.mock(UriInfo.class);
    Mockito.when(mockUriInfo.getBaseUri()).thenReturn(new URI("http://www.test.com/go"));
    JDBCConnectionException exception = new JDBCConnectionException("sql", new SQLException("reason"));
    Response bean = FactoryResponse.catchConnectionJDBCJson(rssProps, mockUriInfo, exception, "resource",
            "txId");
    Assert.assertTrue(true);
}