Example usage for java.sql ResultSet first

List of usage examples for java.sql ResultSet first

Introduction

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

Prototype

boolean first() throws SQLException;

Source Link

Document

Moves the cursor to the first row in this ResultSet object.

Usage

From source file:org.wso2.carbon.dashboard.portal.core.datasource.DataBaseHandler.java

/**
 * To check whether a dashboard related usage information is updated in database
 *
 * @param tenantId    Id of the tenant which the dashboard belongs to
 * @param dashboardId Id of the dashboard
 * @return whether a dashboard related information exist in database
 * @throws DashboardPortalException//  www  .jav  a2 s  . co  m
 */
public boolean checkDashboard(int tenantId, String dashboardId) throws DashboardPortalException {
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    try {
        connection = dataBaseInitializer.getDBConnection();
        preparedStatement = connection.prepareStatement(DataSourceConstants.SQL_CHECK_DASHBOARD_OPERATION);
        preparedStatement.setInt(1, tenantId);
        preparedStatement.setString(2, dashboardId);
        resultSet = preparedStatement.executeQuery();
        if (!connection.getAutoCommit()) {
            connection.commit();
        }
        if (resultSet.first()) {
            return true;
        }
    } catch (SQLException e) {
        log.error("Cannot insert the gadget usage info ", e);
    } finally {
        closeDatabaseResources(connection, preparedStatement, null);
    }
    return false;
}

From source file:it.anyplace.sync.repository.repo.SqlRepository.java

private @Nullable FolderStats doFindFolderStats(String folder) {
    try (Connection connection = getConnection();
            PreparedStatement prepareStatement = connection
                    .prepareStatement("SELECT * FROM folder_stats WHERE folder=?")) {
        prepareStatement.setString(1, folder);
        ResultSet resultSet = prepareStatement.executeQuery();
        if (resultSet.first()) {
            return readFolderStats(resultSet);
        } else {//from www . j av  a  2 s  .  com
            return null;
        }
    } catch (SQLException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:org.wso2.carbon.ml.dataset.internal.DatabaseHandler.java

/**
 * Returns model id associated with given workflow id
 * //from ww w. ja  v  a 2s. c  o m
 * @param workflowId    Unique identifier of the work-flow
 * @return model id     Unique identifier of the model associated with the work-flow
 * @throws              DatabaseHandlerException
 */
protected String getModelId(String workflowId) throws DatabaseHandlerException {
    Connection connection = null;
    PreparedStatement model = null;
    ResultSet result = null;

    try {
        connection = dataSource.getConnection();
        model = connection.prepareStatement(SQLQueries.GET_MODEL_ID);
        model.setString(1, workflowId);
        result = model.executeQuery();
        if (!result.first()) {
            // need to query ML_MODEL table, just before model building process is started
            // to overcome building same model two (or more) times.
            // hence, null will be checked in UI.
            return null;
        }
        return result.getString(1);

    } catch (SQLException e) {
        throw new DatabaseHandlerException(
                "An error occurred white retrieving model associated with workflow id " + workflowId + ":"
                        + e.getMessage(),
                e);
    } finally {
        // Close the database resources
        MLDatabaseUtils.closeDatabaseResources(connection, model, result);
    }
}

From source file:it.anyplace.sync.repository.repo.SqlRepository.java

private @Nullable IndexInfo doFindIndexInfoByDeviceAndFolder(final String deviceId, final String folder) {
    try (Connection connection = getConnection();
            PreparedStatement prepareStatement = connection
                    .prepareStatement("SELECT * FROM folder_index_info WHERE device_id=? AND folder=?")) {
        prepareStatement.setString(1, deviceId);
        prepareStatement.setString(2, folder);
        ResultSet resultSet = prepareStatement.executeQuery();
        if (resultSet.first()) {
            return readFolderIndexInfo(resultSet);
        } else {/*w ww.  j a  v  a  2  s  . c  om*/
            return null;
        }
    } catch (SQLException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:it.anyplace.sync.repository.repo.SqlRepository.java

public @Nullable @Override FileInfo findFileInfo(String folder, String path) {
    try (Connection connection = getConnection();
            PreparedStatement prepareStatement = connection
                    .prepareStatement("SELECT * FROM file_info WHERE folder=? AND path=?")) {
        prepareStatement.setString(1, folder);
        prepareStatement.setString(2, path);
        ResultSet resultSet = prepareStatement.executeQuery();
        if (resultSet.first()) {
            return readFileInfo(resultSet);
        } else {/*from   w w w . j  a  v  a 2 s .co  m*/
            return null;
        }
    } catch (SQLException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:it.anyplace.sync.repository.repo.SqlRepository.java

public @Nullable @Override Date findFileInfoLastModified(String folder, String path) {
    try (Connection connection = getConnection();
            PreparedStatement prepareStatement = connection
                    .prepareStatement("SELECT last_modified FROM file_info WHERE folder=? AND path=?")) {
        prepareStatement.setString(1, folder);
        prepareStatement.setString(2, path);
        ResultSet resultSet = prepareStatement.executeQuery();
        if (resultSet.first()) {
            return new Date(resultSet.getLong("last_modified"));
        } else {/*from w  w  w  . j  a va 2s. c  o  m*/
            return null;
        }
    } catch (SQLException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:it.anyplace.sync.repository.repo.SqlRepository.java

public @Nullable @Override FileInfo findNotDeletedFileInfo(String folder, String path) {
    try (Connection connection = getConnection();
            PreparedStatement prepareStatement = connection.prepareStatement(
                    "SELECT * FROM file_info WHERE folder=? AND path=? AND is_deleted=FALSE")) {
        prepareStatement.setString(1, folder);
        prepareStatement.setString(2, path);
        ResultSet resultSet = prepareStatement.executeQuery();
        if (resultSet.first()) {
            return readFileInfo(resultSet);
        } else {//from  w w w  .jav  a 2s  . co  m
            return null;
        }
    } catch (SQLException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:it.anyplace.sync.repository.repo.SqlRepository.java

public @Nullable @Override FileBlocks findFileBlocks(String folder, String path) {
    try (Connection connection = getConnection();
            PreparedStatement prepareStatement = connection
                    .prepareStatement("SELECT * FROM file_blocks WHERE folder=? AND path=?")) {
        prepareStatement.setString(1, folder);
        prepareStatement.setString(2, path);
        ResultSet resultSet = prepareStatement.executeQuery();
        if (resultSet.first()) {
            return readFileBlocks(resultSet);
        } else {//from www.j av  a2s . c o  m
            return null;
        }
    } catch (SQLException | InvalidProtocolBufferException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:it.anyplace.sync.repository.repo.SqlRepository.java

@Override
public long countFileInfoBySearchTerm(String query) {
    checkArgument(!isBlank(query));//from   w ww  . j av  a 2 s .c  om
    try (Connection connection = getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(
                    "SELECT COUNT(*) FROM file_info WHERE LOWER(file_name) REGEXP ? AND is_deleted=FALSE")) {
        //        try (Connection connection = getConnection(); PreparedStatement preparedStatement = connection.prepareStatement("SELECT COUNT(*) FROM file_info")) {
        preparedStatement.setString(1, query.trim().toLowerCase());
        ResultSet resultSet = preparedStatement.executeQuery();
        checkArgument(resultSet.first());
        return resultSet.getLong(1);
    } catch (SQLException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:it.anyplace.sync.repository.repo.SqlRepository.java

@Override
public byte[] popTempData(String key) {
    checkNotNull(emptyToNull(key));//  w ww. j av  a2s.  com
    try (Connection connection = getConnection()) {
        byte[] data;
        try (PreparedStatement statement = connection
                .prepareStatement("SELECT record_data FROM temporary_data WHERE record_key = ?")) {
            statement.setString(1, key);
            ResultSet resultSet = statement.executeQuery();
            checkArgument(resultSet.first());
            data = resultSet.getBytes(1);
        }
        try (PreparedStatement statement = connection
                .prepareStatement("DELETE FROM temporary_data WHERE record_key = ?")) {
            statement.setString(1, key);
            int count = statement.executeUpdate();
            checkArgument(count == 1);
        }
        checkNotNull(data);
        return data;
    } catch (SQLException ex) {
        throw new RuntimeException(ex);
    }
}