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.ml.database.internal.MLDatabaseService.java

@Override
public long getDatasetVersionIdOfModel(long modelId) throws DatabaseHandlerException {

    Connection connection = null;
    ResultSet result = null;
    PreparedStatement statement = null;
    try {//from   w w  w  . j  av  a2s. c  om
        connection = dbh.getDataSource().getConnection();
        statement = connection.prepareStatement(SQLQueries.SELECT_DATASET_VERSION_ID_OF_MODEL);
        statement.setLong(1, modelId);
        result = statement.executeQuery();
        if (result.first()) {
            return result.getLong(1);
        } else {
            throw new DatabaseHandlerException(
                    "No dataset-version id associated with the model id: " + modelId);
        }
    } catch (SQLException e) {
        throw new DatabaseHandlerException(
                " An error has occurred while extracting dataset-version for model: " + modelId, e);
    } finally {
        // Close the database resources.
        MLDatabaseUtils.closeDatabaseResources(connection, statement, result);
    }
}

From source file:org.wso2.carbon.ml.database.internal.MLDatabaseService.java

@Override
public long getDatasetSchemaIdFromAnalysisId(long analysisId) throws DatabaseHandlerException {
    Connection connection = null;
    ResultSet result = null;
    PreparedStatement statement = null;
    try {/*  ww w  .ja  va2 s.  c  o m*/
        connection = dbh.getDataSource().getConnection();
        statement = connection.prepareStatement(SQLQueries.GET_DATASET_SCHEMA_ID_FROM_ANALYSIS);
        statement.setLong(1, analysisId);
        result = statement.executeQuery();
        if (result.first()) {
            return result.getLong(1);
        } else {
            return -1;
        }
    } catch (SQLException e) {
        throw new DatabaseHandlerException(String
                .format(" An error has occurred while extracting dataset id of [analysis] %s ", analysisId), e);
    } finally {
        // Close the database resources.
        MLDatabaseUtils.closeDatabaseResources(connection, statement, result);
    }
}

From source file:org.wso2.carbon.ml.database.internal.MLDatabaseService.java

/**
 * Retrieve the datasetID of a given version set 
 *///ww  w. j  a v a 2s  . c o m
@Override
public long getDatasetId(long datasetVersionId) throws DatabaseHandlerException {

    Connection connection = null;
    ResultSet result = null;
    PreparedStatement statement = null;
    try {
        connection = dbh.getDataSource().getConnection();
        statement = connection.prepareStatement(SQLQueries.GET_DATASET_ID_FROM_DATASET_VERSION);
        statement.setLong(1, datasetVersionId);
        result = statement.executeQuery();
        if (result.first()) {
            return result.getLong(1);
        } else {
            throw new DatabaseHandlerException(
                    "No dataset id is associated with dataset version id: " + datasetVersionId);
        }
    } catch (SQLException e) {
        throw new DatabaseHandlerException(
                " An error has occurred while extracting dataset id for dataset version id: "
                        + datasetVersionId,
                e);
    } finally {
        // Close the database resources.
        MLDatabaseUtils.closeDatabaseResources(connection, statement, result);
    }
}

From source file:org.wso2.carbon.ml.database.internal.MLDatabaseService.java

/**
 * Retrieves the path of the value-set having the given ID, from the database.
 *
 * @param datasetId Unique Identifier of the value-set
 * @return Absolute path of a given value-set
 * @throws DatabaseHandlerException//w  w  w  .  j a  v a  2  s  .co m
 */
public String getDatasetUri(long datasetId) throws DatabaseHandlerException {

    Connection connection = null;
    ResultSet result = null;
    PreparedStatement getStatement = null;
    try {
        connection = dbh.getDataSource().getConnection();
        connection.setAutoCommit(true);
        getStatement = connection.prepareStatement(SQLQueries.GET_DATASET_LOCATION);
        getStatement.setLong(1, datasetId);
        result = getStatement.executeQuery();
        if (result.first()) {
            return result.getNString(1);
        } else {
            logger.error("Invalid value set ID: " + datasetId);
            throw new DatabaseHandlerException("Invalid value set ID: " + datasetId);
        }
    } catch (SQLException e) {
        throw new DatabaseHandlerException("An error occurred while reading the Value set " + datasetId
                + " from the database: " + e.getMessage(), e);
    } finally {
        // Close the database resources.
        MLDatabaseUtils.closeDatabaseResources(connection, getStatement, result);
    }
}

From source file:org.wso2.carbon.ml.database.internal.MLDatabaseService.java

/**
 * Retrieves the path of the value-set having the given ID, from the database.
 *
 * @param datasetVersionId Unique Identifier of the value-set
 * @return Absolute path of a given value-set
 * @throws DatabaseHandlerException/*from  w  ww  .j a  v a  2s  .  c  o m*/
 */
public String getDatasetVersionUri(long datasetVersionId) throws DatabaseHandlerException {

    Connection connection = null;
    ResultSet result = null;
    PreparedStatement getStatement = null;
    try {
        connection = dbh.getDataSource().getConnection();
        connection.setAutoCommit(true);
        getStatement = connection.prepareStatement(SQLQueries.GET_DATASET_VERSION_LOCATION);
        getStatement.setLong(1, datasetVersionId);
        result = getStatement.executeQuery();
        if (result.first()) {
            return result.getNString(1);
        } else {
            logger.error("Invalid value set ID: " + datasetVersionId);
            throw new DatabaseHandlerException("Invalid value set ID: " + datasetVersionId);
        }
    } catch (SQLException e) {
        throw new DatabaseHandlerException("An error occurred while reading the Value set " + datasetVersionId
                + " from the database: " + e.getMessage(), e);
    } finally {
        // Close the database resources.
        MLDatabaseUtils.closeDatabaseResources(connection, getStatement, result);
    }
}

From source file:org.wso2.carbon.ml.database.internal.MLDatabaseService.java

@Override
public boolean isValidModelId(int tenantId, String userName, long modelId) throws DatabaseHandlerException {

    Connection connection = null;
    ResultSet result = null;
    PreparedStatement statement = null;
    try {//w  w  w. ja v  a2  s  . com
        connection = dbh.getDataSource().getConnection();
        statement = connection.prepareStatement(SQLQueries.GET_ML_MODEL_NAME);
        statement.setLong(1, modelId);
        statement.setInt(2, tenantId);
        statement.setString(3, userName);
        result = statement.executeQuery();
        if (result.first()) {
            return true;
        } else {
            return false;
        }
    } catch (SQLException e) {
        throw new DatabaseHandlerException(
                " An error has occurred while extracting model name for model id: " + modelId, e);
    } finally {
        // Close the database resources.
        MLDatabaseUtils.closeDatabaseResources(connection, statement, result);
    }
}

From source file:org.wso2.carbon.ml.database.internal.MLDatabaseService.java

@Override
public long getDatasetId(String datasetName, int tenantId, String userName) throws DatabaseHandlerException {

    Connection connection = null;
    ResultSet result = null;
    PreparedStatement statement = null;
    try {/*from w w  w  .j a v  a 2s .c o  m*/
        connection = dbh.getDataSource().getConnection();
        statement = connection.prepareStatement(SQLQueries.GET_DATASET_ID);
        statement.setString(1, datasetName);
        statement.setInt(2, tenantId);
        statement.setString(3, userName);
        result = statement.executeQuery();
        if (result.first()) {
            return result.getLong(1);
        } else {
            return -1;
        }
    } catch (SQLException e) {
        throw new DatabaseHandlerException(
                " An error has occurred while extracting dataset name: " + datasetName, e);
    } finally {
        // Close the database resources.
        MLDatabaseUtils.closeDatabaseResources(connection, statement, result);
    }
}

From source file:org.wso2.carbon.ml.database.internal.MLDatabaseService.java

@Override
public boolean isValidModelStatus(long modelId, int tenantId, String userName) throws DatabaseHandlerException {
    Connection connection = null;
    ResultSet result = null;
    PreparedStatement statement = null;
    try {/*from   w w w. j  a v  a 2  s .c  o  m*/
        connection = dbh.getDataSource().getConnection();
        statement = connection.prepareStatement(SQLQueries.GET_MODEL_STATUS);
        statement.setLong(1, modelId);
        statement.setInt(2, tenantId);
        statement.setString(3, userName);
        result = statement.executeQuery();
        if (result.first()) {
            if (MLConstants.MODEL_STATUS_COMPLETE.equalsIgnoreCase(result.getString(1))) {
                return true;
            }
        } else {
            throw new DatabaseHandlerException("Failed to find the model for model id " + modelId);
        }
    } catch (SQLException e) {
        throw new DatabaseHandlerException(
                "An error has occurred while fetching the status of the model for model id: " + modelId + ": "
                        + e.getMessage(),
                e);
    } finally {
        MLDatabaseUtils.closeDatabaseResources(connection, statement, result);
    }
    // Consider anything other than "Complete" status as an invalid model.
    return false;
}

From source file:org.wso2.carbon.ml.database.internal.MLDatabaseService.java

/**
 * Retrieve and returns summary statistics for a given feature of a given dataset
 *
 * @param datasetId     Unique identifier of a dataset
 * @param featureName   Name of the feature of which summary statistics are needed
 * @return JSON string containing summary statistics
 * @throws DatabaseHandlerException/*from ww w  .j a  v a  2  s. co m*/
 */
@Override
public String getSummaryStats(long datasetId, String featureName) throws DatabaseHandlerException {

    Connection connection = null;
    PreparedStatement getSummaryStatement = null;
    ResultSet result = null;
    try {
        connection = dbh.getDataSource().getConnection();
        connection.setAutoCommit(true);
        getSummaryStatement = connection.prepareStatement(SQLQueries.GET_SUMMARY_STATS_OF_DATASET);
        getSummaryStatement.setLong(1, datasetId);
        getSummaryStatement.setString(2, featureName);
        result = getSummaryStatement.executeQuery();
        result.first();
        return result.getString(1);
    } catch (SQLException e) {
        throw new DatabaseHandlerException("An error occurred while retrieving summary "
                + "statistics for the dataset: " + datasetId + ": " + e.getMessage(), e);
    } finally {
        // Close the database resources
        MLDatabaseUtils.closeDatabaseResources(connection, getSummaryStatement, result);
    }
}

From source file:org.wso2.carbon.ml.database.internal.MLDatabaseService.java

@Override
public MLModelData getModel(int tenantId, String userName, String modelName) throws DatabaseHandlerException {
    Connection connection = null;
    ResultSet result = null;
    PreparedStatement statement = null;
    try {/*from  ww  w.j a  va 2 s .  c  om*/
        connection = dbh.getDataSource().getConnection();
        statement = connection.prepareStatement(SQLQueries.GET_ML_MODEL);
        statement.setString(1, modelName);
        statement.setInt(2, tenantId);
        statement.setString(3, userName);
        result = statement.executeQuery();
        if (result.first()) {
            MLModelData model = new MLModelData();
            model.setId(result.getLong(1));
            model.setAnalysisId(result.getLong(2));
            model.setVersionSetId(result.getLong(3));
            model.setCreatedTime(result.getString(4));
            model.setStorageType(result.getString(5));
            model.setStorageDirectory(result.getString(6));
            model.setName(modelName);
            model.setTenantId(tenantId);
            model.setUserName(userName);
            model.setStatus(result.getString(7));
            return model;
        } else {
            return null;
        }
    } catch (SQLException e) {
        throw new DatabaseHandlerException(
                " An error has occurred while extracting the model with model name: " + modelName, e);
    } finally {
        // Close the database resources.
        MLDatabaseUtils.closeDatabaseResources(connection, statement, result);
    }
}