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 MLAnalysis getAnalysis(int tenantId, String userName, long analysisId) throws DatabaseHandlerException {
    Connection connection = null;
    ResultSet result = null;
    PreparedStatement statement = null;
    try {/*  w  w  w. java2 s .c o  m*/
        connection = dbh.getDataSource().getConnection();
        statement = connection.prepareStatement(SQLQueries.GET_ANALYSIS_BY_ID);
        statement.setLong(1, analysisId);
        statement.setInt(2, tenantId);
        statement.setString(3, userName);
        result = statement.executeQuery();
        if (result.first()) {
            MLAnalysis analysis = new MLAnalysis();
            analysis.setId(analysisId);
            analysis.setName(result.getString(1));
            analysis.setProjectId(result.getLong(2));
            analysis.setComments(MLDatabaseUtils.toString(result.getClob(3)));
            analysis.setTenantId(tenantId);
            analysis.setUserName(userName);
            return analysis;
        } else {
            return null;
        }
    } catch (SQLException e) {
        throw new DatabaseHandlerException(
                " An error has occurred while retrieving analysis with Id: " + analysisId, e);
    } finally {
        // Close the database resources.
        MLDatabaseUtils.closeDatabaseResources(connection, statement, result);
    }
}

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

@Override
public MLProject getProject(int tenantId, String userName, String projectName) throws DatabaseHandlerException {
    Connection connection = null;
    ResultSet result = null;
    PreparedStatement statement = null;
    try {// ww  w  .jav  a  2 s  .  co m
        connection = dbh.getDataSource().getConnection();
        statement = connection.prepareStatement(SQLQueries.GET_PROJECT);
        statement.setString(1, projectName);
        statement.setInt(2, tenantId);
        statement.setString(3, userName);
        result = statement.executeQuery();
        if (result.first()) {
            MLProject project = new MLProject();
            project.setName(projectName);
            project.setId(result.getLong(1));
            project.setDescription(result.getString(2));
            project.setDatasetId(result.getLong(3));
            project.setTenantId(tenantId);
            project.setUserName(userName);
            project.setCreatedTime(result.getString(4));
            if (project.getDatasetId() != 0) {
                MLDataset dataset = getDataset(tenantId, userName, project.getDatasetId());
                project.setDatasetName(dataset.getName());
            }
            return project;
        } else {
            return null;
        }
    } catch (SQLException e) {
        throw new DatabaseHandlerException(
                " An error has occurred while extracting project for project name:" + projectName, e);
    } finally {
        // Close the database resources.
        MLDatabaseUtils.closeDatabaseResources(connection, statement, result);
    }
}

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

@Override
public MLModelData getModel(int tenantId, String userName, long modelId) throws DatabaseHandlerException {
    Connection connection = null;
    ResultSet result = null;
    PreparedStatement statement = null;
    try {/* w w w. j a v  a  2s . com*/
        connection = dbh.getDataSource().getConnection();
        statement = connection.prepareStatement(SQLQueries.GET_ML_MODEL_FROM_ID);
        statement.setLong(1, modelId);
        statement.setInt(2, tenantId);
        statement.setString(3, userName);
        result = statement.executeQuery();
        if (result.first()) {
            MLModelData model = new MLModelData();
            model.setId(modelId);
            model.setName(result.getString(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.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 id: " + modelId, e);
    } finally {
        // Close the database resources.
        MLDatabaseUtils.closeDatabaseResources(connection, statement, result);
    }
}

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

/**
 * Returns project object for a given project ID from the database.
 *
 * @param tenantId ID of the tenant//from   w  w  w.  ja  va 2s. c om
 * @param userName Username of the tenant
 * @param projectId ID of the project
 * @return MLProject object
 * @throws DatabaseHandlerException
 */
@Override
public MLProject getProject(int tenantId, String userName, long projectId) throws DatabaseHandlerException {
    Connection connection = null;
    ResultSet result = null;
    PreparedStatement statement = null;
    try {
        connection = dbh.getDataSource().getConnection();
        statement = connection.prepareStatement(SQLQueries.GET_PROJECT_BY_ID);
        statement.setLong(1, projectId);
        statement.setInt(2, tenantId);
        statement.setString(3, userName);
        result = statement.executeQuery();
        if (result.first()) {
            MLProject project = new MLProject();
            project.setId(result.getLong(1));
            project.setName(result.getString(2));
            project.setDescription(result.getString(3));
            project.setDatasetId(result.getLong(4));
            project.setCreatedTime(result.getString(5));
            project.setTenantId(tenantId);
            project.setUserName(userName);
            if (project.getDatasetId() != 0) {
                MLDataset dataset = getDataset(tenantId, userName, project.getDatasetId());
                project.setDatasetName(dataset.getName());
            }
            return project;
        } else {
            return null;
        }
    } catch (SQLException e) {
        throw new DatabaseHandlerException(
                "An error has occurred while extracting project for project ID:" + projectId, 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 getVersionsetId(String datasetVersionName, int tenantId, String userName)
        throws DatabaseHandlerException {

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

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

/**
 * Retrieve a dataset from ID//w  w  w  .j av a  2 s.c  o m
 */
@Override
public MLDataset getDataset(int tenantId, String userName, long datasetId) throws DatabaseHandlerException {
    Connection connection = null;
    ResultSet result = null;
    PreparedStatement statement = null;
    try {
        connection = dbh.getDataSource().getConnection();
        statement = connection.prepareStatement(SQLQueries.GET_DATASET_USING_ID);
        statement.setInt(1, tenantId);
        statement.setString(2, userName);
        statement.setLong(3, datasetId);
        result = statement.executeQuery();
        if (result.first()) {
            MLDataset dataset = new MLDataset();
            dataset.setId(result.getLong(1));
            dataset.setName(result.getString(2));
            dataset.setComments(MLDatabaseUtils.toString(result.getClob(3)));
            dataset.setDataSourceType(result.getString(4));
            dataset.setDataTargetType(result.getString(5));
            dataset.setDataType(result.getString(6));
            dataset.setTenantId(tenantId);
            dataset.setUserName(userName);
            if (dataset.getId() != 0) {
                List<MLDatasetVersion> datasetVersions = getAllVersionsetsOfDataset(tenantId, userName,
                        dataset.getId());
                if (datasetVersions.size() > 0) {
                    String datasetStatus = MLDBUtil.getDatasetStatus(datasetVersions);
                    dataset.setStatus(datasetStatus);
                }
            }
            return dataset;
        } else {
            return null;
        }
    } catch (SQLException e) {
        throw new DatabaseHandlerException(
                "An error has occurred while extracting a dataset with [id] " + datasetId, e);
    } finally {
        // Close the database resources.
        MLDatabaseUtils.closeDatabaseResources(connection, statement, result);
    }
}

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

@Override
public void insertHyperParameters(long analysisId, List<MLHyperParameter> hyperParameters, String algorithmName)
        throws DatabaseHandlerException {

    Connection connection = null;
    PreparedStatement insertStatement = null;
    PreparedStatement getStatement = null;
    PreparedStatement deleteStatement = null;
    ResultSet result = null;
    try {//from  ww  w  .j  a v a2  s .  c  om
        // Insert the hyper parameter to the database
        connection = dbh.getDataSource().getConnection();
        connection.setAutoCommit(false);

        getStatement = connection.prepareStatement(SQLQueries.GET_EXISTING_ALGORITHM);
        getStatement.setLong(1, analysisId);
        result = getStatement.executeQuery();

        if (result.first() && algorithmName != null && !algorithmName.equals(result.getString(1))) {
            deleteStatement = connection.prepareStatement(SQLQueries.DELETE_HYPER_PARAMETERS);
            deleteStatement.setLong(1, analysisId);
            deleteStatement.execute();
        }

        for (MLHyperParameter mlHyperParameter : hyperParameters) {
            String name = mlHyperParameter.getKey();
            String value = mlHyperParameter.getValue();
            getStatement = connection.prepareStatement(SQLQueries.GET_EXISTING_HYPER_PARAMETER);
            getStatement.setLong(1, analysisId);
            getStatement.setString(2, name);
            result = getStatement.executeQuery();
            if (result.first()) {
                insertStatement = connection.prepareStatement(SQLQueries.UPDATE_HYPER_PARAMETER);
                insertStatement.setString(1, algorithmName);
                insertStatement.setString(2, value);
                insertStatement.setLong(3, analysisId);
                insertStatement.setString(4, name);
            } else {
                insertStatement = connection.prepareStatement(SQLQueries.INSERT_HYPER_PARAMETER);
                insertStatement.setLong(1, analysisId);
                insertStatement.setString(2, algorithmName);
                insertStatement.setString(3, name);
                insertStatement.setString(4, value);
            }
            insertStatement.execute();
        }

        connection.commit();
        if (logger.isDebugEnabled()) {
            logger.debug("Successfully inserted the hyper parameter");
        }
    } catch (SQLException e) {
        // Roll-back the changes.
        MLDatabaseUtils.rollBack(connection);
        throw new DatabaseHandlerException(
                "An error occurred while inserting hyper parameter " + " to the database: " + e.getMessage(),
                e);
    } finally {
        // Enable auto commit.
        MLDatabaseUtils.enableAutoCommit(connection);
        // Close the database resources.
        MLDatabaseUtils.closeDatabaseResources(getStatement);
        MLDatabaseUtils.closeDatabaseResources(deleteStatement);
        MLDatabaseUtils.closeDatabaseResources(connection, insertStatement);
    }
}

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

/**
 * Returns the number of features of a given data-set version
 *
 * @param datasetSchemaId Unique identifier of the data-set version
 * @return Number of features in the data-set version
 * @throws DatabaseHandlerException//from   ww  w .  j a v  a  2 s. c  om
 */
public int getFeatureCount(long datasetSchemaId) throws DatabaseHandlerException {

    Connection connection = null;
    PreparedStatement getFeatues = null;
    ResultSet result = null;
    int featureCount = 0;
    try {
        connection = dbh.getDataSource().getConnection();
        connection.setAutoCommit(true);
        // Create a prepared statement and extract data-set configurations.
        getFeatues = connection.prepareStatement(SQLQueries.GET_FEATURE_COUNT);
        getFeatues.setLong(1, datasetSchemaId);
        result = getFeatues.executeQuery();
        if (result.first()) {
            featureCount = result.getInt(1);
        }
        return featureCount;
    } catch (SQLException e) {
        throw new DatabaseHandlerException("An error occurred while retrieving feature count of the dataset "
                + datasetSchemaId + ": " + e.getMessage(), e);
    } finally {
        // Close the database resources
        MLDatabaseUtils.closeDatabaseResources(connection, getFeatues, result);
    }
}

From source file:com.github.woonsan.jdbc.jcr.impl.JcrJdbcResultSetTest.java

@SuppressWarnings("deprecation")
@Test//from   w  ww  . ja v a2s  . com
public void testResultSetWhenClosed() throws Exception {
    Statement statement = getConnection().createStatement();
    ResultSet rs = statement.executeQuery(SQL_EMPS);

    rs.close();

    try {
        rs.isBeforeFirst();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.isAfterLast();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.isFirst();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.isLast();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.beforeFirst();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.afterLast();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.first();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.last();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.next();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getRow();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getType();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getConcurrency();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.rowUpdated();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.rowDeleted();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.rowInserted();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getStatement();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.wasNull();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getString(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getString("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getBoolean(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getBoolean("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getByte(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getByte("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getShort(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getShort("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getInt(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getInt("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getLong(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getLong("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getFloat(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getFloat("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getDouble(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getDouble("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getBigDecimal(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getBigDecimal("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getBytes(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getBytes("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getDate(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getDate(1, null);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getDate("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getDate("col1", null);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getTime(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getTime(1, null);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getTime("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getTime("col1", null);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getTimestamp(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getTimestamp(1, null);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getTimestamp("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getTimestamp("col1", null);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getAsciiStream(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getAsciiStream("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getUnicodeStream(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getUnicodeStream("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getBinaryStream(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getBinaryStream("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getCharacterStream(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getCharacterStream("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getMetaData();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.setFetchDirection(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getFetchDirection();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.setFetchSize(100);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getFetchSize();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getHoldability();
        fail();
    } catch (SQLException ignore) {
    }

    statement.close();
}

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

@Override
public MLDatasetVersion getVersionSetWithVersion(long datasetId, String version, int tenantId, String userName)
        throws DatabaseHandlerException {

    Connection connection = null;
    ResultSet result = null;
    PreparedStatement statement = null;
    try {/* w ww.j  a v  a  2  s .c  o  m*/
        connection = dbh.getDataSource().getConnection();
        statement = connection.prepareStatement(SQLQueries.GET_DATASETVERSION_ID);
        statement.setLong(1, datasetId);
        statement.setInt(2, tenantId);
        statement.setString(3, userName);
        statement.setString(4, version);
        result = statement.executeQuery();
        if (result.first()) {
            MLDatasetVersion versionset = new MLDatasetVersion();
            versionset.setId(result.getLong(1));
            versionset.setName(result.getString(2));
            versionset.setTargetPath(result.getString(3) == null ? null : result.getString(3));
            if (result.getBinaryStream(4) != null) {
                versionset.setSamplePoints(MLDBUtil.getSamplePointsFromInputStream(result.getBinaryStream(4)));
            }
            versionset.setTenantId(tenantId);
            versionset.setUserName(userName);
            versionset.setVersion(version);
            return versionset;
        } else {
            return null;
        }
    } catch (Exception e) {
        throw new DatabaseHandlerException(String.format(
                " An error has occurred while extracting dataset version id of [dataset] %s [version] %s",
                datasetId, version), e);
    } finally {
        // Close the database resources.
        MLDatabaseUtils.closeDatabaseResources(connection, statement, result);
    }
}